Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Reading EXIF Data From Images in PHP (http://www.go4expert.com/articles/reading-exif-data-images-php-t29367/)

pradeep 24Dec2012 04:41

Reading EXIF Data From Images in PHP
EXIF stands for Exchangeable image file format is standard by which metadata is stored in image & sound file formats, these are sometimes added by digital camera / software / or manually added by a user. These standards metadata tags help people track / store info, for example Flickr shows the camera used to take the photo using the EXIF tags, and allows you to see all images taken using a specific make/model, thereby helping in cataloguing the photos.

In this article I'll be showing what you would need to read EXIF info from images, and how to go about doing it, and a example practical usage of it.

Installating EXIF Extension

To enable EXIF support you'll need to configure PHP with --enable-exif, and in case you are using Windows you'll need to enable php_exif.dll and php_mbstring.dll in php.ini, and also make sure that php_mbstring.dll loads before php_exif.dll.


With a handful of functions provided by the EXIF extension you can detect image format, get specific tags, like thumbnails and other, or read them all at once. The example below will help you understand the usage of the functions:

Code: PHP

// determine image format
if (exif_imagetype('image.jpg') == IMAGETYPE_JPEG) {
    print "Hey, this is a JPEG image";

// extra thumbnail from EXIF data, this sometimes help and you
// do not need to generate your own thumbnails, most digital
// cameras provide a thumbnail
$thumb_image = exif_thumbnail($original_file, $width, $height, $type);

print "Height: $height \nWidth: $width \nType: $type";

// save thumbnail to a file

// read all available tags into an associative array
$exif_data = exif_read_data($original_file, 'ANY_TAG');

foreach ($exif_data as $key => $section_data) {
    foreach ($section_data as $name => $val) {
        print "$key.$name : $val \n";

An example practical application would be where where the image is automatically rotated by the script using the orientation information provided in the EXIF, see the example below:

Code: PHP

$exif_data = exif_read_data($original_file);
$image_obj = imagecreatefromstring(file_get_contents($original_file));
$orientation = $exif['IFD0']['Orientation'];

if($orientation) {
    switch($orientation) {
        case 8:
            $image_obj = imagerotate($image_obj,90,0);
        case 3:
            $image_obj = imagerotate($image_obj,180,0);
        case 6:
            $image_obj = imagerotate($image_obj,-90,0);

Now, the image will be rotated automatically, refer to http://sylvana.net/jpegcrop/exif_orientation.html for the list of orientation values.

Alex.Gabriel 13Jan2013 17:54

Re: Reading EXIF Data From Images in PHP
Thank you , i update my upload forms. I use few functions to check if an image is really a valid image but this is something that will prevent future attacks. Actually few hosters have a strict file checking but this is made by checking file extension in most of the cases.

All times are GMT +5.5. The time now is 05:42.