Recipe 17.8. Reading EXIF Data


17.8.2. Problem

You want to extract metainformation from an image file. This lets you find out when the photo was taken, the image size, and the MIME type.

17.8.3. Solution

Use the exif_read_data( ) function:

$exif = exif_read_data('/beth-and-seth.jpeg'); print_r($exif); Array (     [FileName] => beth-and-seth.jpg     [FileDateTime] => 1096055414     [FileSize] => 182080     [FileType] => 2     [MimeType] => image/jpeg     [SectionsFound] => APP12     [COMPUTED] => Array         (             [html] => width="642" height="855"             [Height] => 855             [Width] => 642             [IsColor] => 1         )     [Company] => Ducky     [Info] => ) 

17.8.4. Discussion

The Exchangeable Image File Format (EXIF) is a standard for embedding metadata inside of pictures. Most digital cameras use EXIF, so it's an increasingly popular way of providing rich data in photo galleries such as Flickr .

PHP has a number of EXIF functions. They don't require external libraries, but must be enabled by passing the --enable-exif configuration flag.

The easiest way to extract data is through the exif_read_data( ) method. It returns an array of metadata, including the creation date of the photo, the MIME type (which you can use to help serve up the image), and the image dimensions:

$exif = exif_read_data('/beth-and-seth.jpeg'); print_r($exif); Array (     [FileName] => beth-and-seth.jpg     [FileDateTime] => 1096055414     [FileSize] => 182080     [FileType] => 2     [MimeType] => image/jpeg     [SectionsFound] => APP12     [COMPUTED] => Array         (             [html] => width="642" height="855"             [Height] => 855             [Width] => 642             [IsColor] => 1         )     [Company] => Ducky     [Info] => ) 

Use the html value to directly embed within an <img> source tag.

You can also use the EXIF functions to retrieve a thumbnail image associated with the picture. To access this, call exif_thumbnail( ):

$thumb = exif_thumbnail('beth-and-seth.jpeg', $width, $height, $type);

The exif_thumbnail( ) function takes four parameters. The first is the filename. The last three are variables passed by reference where the width, height, and image type will be stored. The function returns the thumbnail image as a binary string, or false on failure.

To serve up the image directly, use the image_type_to_mime_type( ) to get the correct MIME type. Pass that along as an HTTP header and then display the image:

$thumb = exif_thumbnail('beth-and-seth.jpeg', $width, $height, $type); if ($thumb != false) {     $mime = image_type_to_mime_type($type);     header("Content-type: $mime");     print $image; } 

Alternatively, you can create an <img> link:

$file = 'beth-and-seth.jpeg'; $thumb = exif_thumbnail($file, $width, $height, $type); if ($thumb != false) {     $img = "<img src=\"$file\" alt=\"Beth and Seth\"                  width=\"$width\" height=\"$height\" />     print $img; } 

17.8.5. See Also

Documentation on exif_read_data( ) at http://www.php.net/exif-read-data and on exif_thumbnail( ) at http://www.php.net/exif-thumbnail.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net