Team LiB
Previous Section Next Section

Other Graphics Functions

Although this chapter focuses almost entirely on the GD extension, it is important to note that PHP also provides a number of functions that do not require the GD extension. Although these functions may rely on "extensions" in the purist form of the term, all are available without requiring any external libraries.

Although you have already been exposed to retrieving the width and height of an image using the GD extension via the imagesx() and imagesy() functions, a non-GD version getimagesize() is also provided. This enables you to retrieve the width and height of most images by providing the name of the file. The syntax for the getimagesize() function is as follows:

getimagesize($filename [, $imageinfo]);

$filename is the name of the image file on the file system to read and the optional parameter $imageinfo is a reference to an array to store any "extra information" (such as IPTC information) found in the image. When executed, this function attempts to open the provided image file and upon success returns an array containing the following values:

Array Index



The width of the image in pixels.


The height of the image in pixels.


An integer representing the image type.

As you can see, not only does the getimagesize() function return the width and height of the image, it also attempts to determine its type. Because each type is represented by a different integer in the array returned by this function, the following table has been provided for your reference (Table 27.1):

Table 27.1. Image Type Constants

PHP Constant

Image Type


GIF Image


JPEG Image


PNG Image


Shockwave (SWF)


Photoshop (PSD)


Bitmap (BMP) Image


TIFF Image (Intel)


TIFF Image (Motorola)


JPC Image




JPX Image


JB2 Image


SWC Image


IFF Image


WBMP Image


JPEG2000 Image


XBM Image

When displaying images to a browser, it is important that the appropriate HTTP content type is sent to indicate to the browser how the image should be rendered. Because this information can be tedious or constraining to determine manually within your scripts, PHP provides the image_type_to_mime_type() function. The syntax for this function is as follows:


$image_type is an integer constant (such as IMAGETYPE_JPEG) as outlined previously. When executed, this function returns the appropriate MIME type for that image, which can then be used in a HTTP Content-Type header.

EXIF Functions


The EXIF extension relies on data generated in JPEG and TIFF images by digital cameras. Unfortunately, because different digital cameras tend to store EXIF data differently, useful examples cannot be provided. For detailed information regarding the use of EXIF, consult the PHP manual.

EXIF functions provide yet another alternative to accomplishing certain tasks when working with images. Primarily, the EXIF extension allows your scripts to access the metadata contained within JPEG or TIFF images created using digital cameras (such as thumbnails, information about the image, and so on). For instance, the EXIF extension can be used to determine the image type in a similar way to the getimagesize() function using the exif_imagetype() function. The syntax for this function is as follows:


$filename is the filename of the image file for which you want to determine the type. Like getimagesize(), the exif_imagetype() function will return a integer constant representing the image type (such as IMAGETYPE_JPEG) or false on failure. When compared to its counterpart, the exif_imagetype() function is considerably faster when determining the image type and is recommended over the getimagesize() function when the width and height of the image is unnecessary.

As I have already stated, the primary use of the EXIF extension is to provide access to the metadata found within some JPEG or TIFF images. This functionality is broken into two functions: exif_read_data(), which reads the metadata itself, and exif_thumbnail(), which extracts the embedded thumbnail in the image if it exists. Starting the exif_read_data(), the syntax for this function is as follows:

exif_read_data($filename [, $sections [, $arrays [, $thumbnail]]]);

$filename is a JPEG or TIFF image to read the metadata from and the optional parameter $sections is a comma-separated list of the sections that must exist in the image. The final two optional parameters, $arrays and $thumbnail, are both Boolean values. The $arrays parameter determines whether each metadata section should be an array, whereas $thumbnail indicates whether the thumbnail image itself should also be read (instead of just the width/height/format of the thumbnail image).

Although the exif_read_data() function can be used to retrieve the thumbnail image within a given JPEG or TIFF, the EXIF extension also provides a function for this specific taskexif_thumbnail(). The syntax for this function is as follows:

exif_thumbnail($filename [, &$width [, &$height, [&$imagetype]]]);

$filename is the image file to extract the thumbnail from. The three optional additional parameters$width, $height, and $imagetypeallow you to pass by reference three variables in which to store the width, height, and type of image (such as IMAGETYPE_JPEG), respectively. When executed, the exif_thumbnail() function will both return a string containing the thumbnail image data and populate the three optional parameters with the appropriate values for that thumbnail image (if they were provided).

    Team LiB
    Previous Section Next Section