PHP Graphics Reading EXIF Data - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Graphics Reading EXIF Data - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, June 24, 2019

PHP Graphics Reading EXIF Data

PHP Graphics



Reading EXIF Data

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.

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] =>
       )

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 popular way of providing rich data in photo galleries.

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');

Use the html value to directly embed height and width attributes 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 $thumb;
       } else {
            print "Sorry. No thumbnail.";
       }

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;
       }


No comments:

Post a Comment

Post Top Ad