Reading EXIF Data From Images in PHP

Discussion in 'PHP' started by pradeep, Dec 23, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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.

    Usage



    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:

    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
    file_put_contents($path_to_thumb_image,$thumb_image);

    // 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:

    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);
                break;
            case 
    3:
                
    $image_obj imagerotate($image_obj,180,0);
                break;
            case 
    6:
                
    $image_obj imagerotate($image_obj,-90,0);
                break;
        }
    }
    ?>
    Now, the image will be rotated automatically, refer to http://sylvana.net/jpegcrop/exif_orientation.html for the list of orientation values.
     
  2. Alex.Gabriel

    Alex.Gabriel New Member

    Joined:
    Oct 23, 2011
    Messages:
    86
    Likes Received:
    7
    Trophy Points:
    0
    Occupation:
    Linux system administrator
    Location:
    Italy
    Home Page:
    http://blog.evilcoder.net
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice