Chapter 11. Information

4 views
Skip to first unread message

Pterodactyl

unread,
Jul 14, 2007, 3:43:37 PM7/14/07
to Dive Into PHP 5: The Graphics
Chapter 11. Information

Table of Contents

Global Info

gd_info()
imagetypes()

Image Types

image_type_to_mime_type()

Image Colors

imageistruecolor()

Image Size

getimagesize()
imagesx()
imagesy()

This chapter describes how to get some information using GD functions.
[2][http://pterodactyl.l2p.net/book/php5image/page/
bi01.html#php_manual]

Global Info

The gd_info() function retrieves information about the currently
installed GD library and the imagetypes() function returns the image
types supported by this PHP build.

gd_info()

This function gets information about the version and capabilities of
the installed GD library.
array gd_info ( );

Return Values. An associative array is returned.

Table 11.1. Elements of array returned by gd_info() Attribute Type
Meaning Notes
GD Version string the installed libgd version
Freetype Support boolean true if Freetype Support is installed
Freetype Linkage string the way in which Freetype was linked (if
Freetype Support is true) "with freetype", "with TTF library", or
"with unknown library"
T1Lib Support boolean true if T1Lib support is included
GIF Read Support boolean true if support for reading GIF images is
included
GIF Create Support boolean true if support for creating GIF images
is included
JPG Support boolean true if JPG support is included
PNG Support boolean true if PNG support is included
WBMP Support boolean true if WBMP support is included
XPM Support boolean true if XPM support is included
XBM Support boolean true if XBM support is included
JIS-mapped Japanese Font Support boolean true if JIS-mapped Japanese
Font support is included

Example 11.1. The gd_info() function

var_dump(gd_info());

array(12) {
["GD Version"]=>
string(27) "bundled (2.0.28 compatible)"
["FreeType Support"]=>
bool(true)
["FreeType Linkage"]=>
string(13) "with freetype"
["T1Lib Support"]=>
bool(false)
["GIF Read Support"]=>
bool(true)
["GIF Create Support"]=>
bool(true)
["JPG Support"]=>
bool(true)
["PNG Support"]=>
bool(true)
["WBMP Support"]=>
bool(true)
["XPM Support"]=>
bool(false)
["XBM Support"]=>
bool(true)
["JIS-mapped Japanese Font Support"]=>
bool(false)
}

imagetypes()

This function returns the image types supported by the current PHP
installation.
int imagetypes ( );

Return Values. A bit-field corresponding to the image formats
supported by the version of GD linked into PHP is returned, where bits
are IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM.

Example 11.2. The imagetypes() function

print imagetypes();

15


Example 11.3. Checking for PNG support

<?php

if (imagetypes() & IMG_PNG) {
print 'PNG Support is enabled';
}

?>


PNG Support is enabled


Image Types

The image_type_to_mime_type() function gets Mime-Type for image type
returned by getimagesize() (see the section called "getimagesize()"),
exif_read_data(), exif_thumbnail(), and exif_imagetype() (see Chapter
9, Functions [http://pterodactyl.l2p.net/book/php5image/page/
ch09.html]).

image_type_to_mime_type()

This function will determine the Mime-Type for an IMAGETYPE constant.
string image_type_to_mime_type ( imagetype);
int imagetype;

imagetype. One of the IMAGETYPE_XXX constants.

Return Values. See the table below.

Table 11.2. Returned with image_type_to_mime_type() values imagetype
Returned value
IMAGETYPE_GIF image/gif
IMAGETYPE_JPEG image/jpeg
IMAGETYPE_PNG image/png
IMAGETYPE_SWF application/x-shockwave-flash
IMAGETYPE_PSD image/psd
IMAGETYPE_BMP image/bmp
IMAGETYPE_TIFF_II image/tiff
IMAGETYPE_TIFF_MM image/tiff
IMAGETYPE_JPC application/octet-stream
IMAGETYPE_JP2 image/jp2
IMAGETYPE_JPX application/octet-stream
IMAGETYPE_JB2 application/octet-stream
IMAGETYPE_SWC application/x-shockwave-flash
IMAGETYPE_IFF image/iff
IMAGETYPE_WBMP image/vnd.wap.wbmp
IMAGETYPE_XBM image/xbm

Example 11.4. The image_type_to_mime_type() function

print image_type_to_mime_type(IMAGETYPE_PNG);

image/png


Example 11.5. Using image_type_to_mime_type() in an HTTP header

header('Content-type: ' . image_type_to_mime_type(IMAGETYPE_PNG));

Content-type: image/png


[Note][http://pterodactyl.l2p.net/images/docbook/note.png] Note

The image_type_to_mime_type() function does not require the GD image
library.
Image Colors

The imageistruecolor() function finds whether an image is a truecolor
image.
imageistruecolor()

This function finds whether the image is a truecolor image.
bool imageistruecolor ( image);
resource image;

image. An image resource, returned by one of the image creation
functions (see Chapter 12, Creating Images [http://pterodactyl.l2p.net/
book/php5image/page/ch12.html]).

Return Values. Returns true if the image is truecolor, false
otherwise.

Example 11.6. The imageistruecolor() function

<?php

$url = 'http://pterodactyl.l2p.net/images/pterodactyl.jpg';
$image = imagecreatefromjpeg($url);
if (imageistruecolor($image)) {
print 'The image is truecolor.';
}

?>


The image is truecolor.


[Note][http://pterodactyl.l2p.net/images/docbook/note.png] Note

The imageistruecolor() function requires GD 2.0.1 or later (2.0.28 or
later is recommended).
Image Size

The getimagesize() function gets the size of an image, the imagesx()
function gets image width, and the imagesy() function gets image
height.
getimagesize()

This function will determine the size of any given image file and
return the dimensions along with the file type and a height/width text
string to be used inside a normal HTML <img> tag and the correspondent
HTTP content type. Can also return some more information in the
imageinfo parameter.
array getimagesize ( filename,
&imageinfo);
string filename;
array &imageinfo;

filename. The image file name or URL.

imageinfo. This optional parameter allows you to extract some extended
information from the image file.

Return Values. An array of elements is returned (see the table below)
or false on failure.
Table 11.3. Array elements returned with getimagesize() Index Meaning
0 the width of the image
1 the height of the image
2 one of the IMAGETYPE_XXX constants indicating the type of the image
3 a text string with the correct height="yyy" width="xxx" string that
can be used directly in an <img> tag
bits the number of bits for each color
channels usually will be 3 for RGB pictures and 4 for CMYK pictures
mime the correspondent MIME type of the image

Errors/Exceptions. If accessing the filename image is impossible, or
if it isn't a valid picture, getimagesize() will generate an error of
level E_WARNING.

Example 11.7. The getimagesize function

<?php

$url = 'http://pterodactyl.l2p.net/images/pterodactyl.jpg';
print_r(getimagesize($url));

?>


Array
(
[0] => 263
[1] => 211
[2] => 2
[3] => width="263" height="211"
[bits] => 8
[channels] => 1
[mime] => image/jpeg
)

Example 11.8. Using getimagesize in an HTTP header

<?php

$size = getimagesize('pterodactyl.jpg');
header('Content-type: ' . $size['mime']);

?>


Content-type: image/jpeg


Example 11.9. Using getimagesize in the HTML <img> tag

<?php

$filename = 'pterodactyl.jpg';
$size = getimagesize($filename);
print "<img src=\"$filename\" $size[3] alt=\"getimagesize() example\" /
>";

?>


<img src="pterodactyl.jpg" width="263" height="211"
alt="getimagesize() example" />


[Note][http://pterodactyl.l2p.net/images/docbook/note.png] Note

The getimagesize() function does not require the GD image library.
imagesx()

This function returns the width of the given image resource.
int imagesx ( image);
resource image;

image. An image resource, returned by one of the image creation
functions (see Chapter 12, Creating Images [http://pterodactyl.l2p.net/
book/php5image/page/ch12.html]).

Return Values. The width of the image or false on errors.

Example 11.10. The imagesx() function

<?php

$url = 'http://pterodactyl.l2p.net/images/pterodactyl.jpg';
$image = imagecreatefromjpeg($url);
print imagesx($image);

?>


263


imagesy()

This function returns the height of the given image resource.
int imagesy ( image);
resource image;

image. An image resource, returned by one of the image creation
functions (see Chapter 12, Creating Images [http://pterodactyl.l2p.net/
book/php5image/page/ch12.html]).

Return Values. The height of the image or false on errors.

Example 11.11. The imagesy() function

<?php

$url = 'http://pterodactyl.l2p.net/images/pterodactyl.jpg';
$image = imagecreatefromjpeg($url);
print imagesy($image);

?>


211

Reply all
Reply to author
Forward
0 new messages