Ways to read EXIF information in PHP _php tips

Source: Internet
Author: User
Tags phpinfo zts zend

First to understand what is the image of EXIF information

EXIF is an image file format that has exactly the same data storage as the JPEG format. In fact, the EXIF format is a digital photo inserted in the header of the JPEG format. Including the aperture, shutter, white balance, ISO, focal length, date and time, and other shooting conditions as well as camera brand, model, color coding, recording sound and the Global Positioning System (GPS), thumbnails and so on. Simply put, the exif=jpeg+ takes a shot of parameters. As a result, you can view images in EXIF format with any viewing software that can look at JPEG files, but not all graphics programs can handle EXIF information.

Above quoted from Baidu Encyclopedia.

It is not necessary to read EXIF in photos, but it is particularly important to read the EXIF information of photos, such as the hummingbird of the photography forum, compared to some sites that discuss photography.

Screenshots from the Hummingbird Forum, the red Circle information is part of the program to read photos of EXIF information. We download the picture to the local, use the Light and light magic hand to open the picture to see its Exif information, when the BG ran in addition to light and light there are many tools can view the image of EXIF value.

Except for the lens value in the EXIF information, the rest of the value can be read correctly.

Open PHP Module

By default, PHP reads the image EXIF information module is not open, we need to first open this module.

To open the EXIF module requires mbstring support, so first to install mbstring, the following is a Linux environment for example, the other environment similar.

Installing the Mbstring module

First find the PHP source package location, directly into the ext/mbstring, execute the following command installation, specific parameters to see their own environment.

Copy Code code as follows:

[Root@lee ext]# cd/data0/software/php/ext/mbstring
[Root@lee mbstring]#/usr/local/webserver/php/bin/phpize
Configuring for:
PHP Api version:20090626
Zend Module Api no:20090626
Zend Extension Api no:220090626
[Root@lee exif]#./configure--with-php-config=/usr/local/webserver/php/bin/php-config
[Root@lee mbstring]# make && make install
Installing Shared extensions:/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
Installing header files:/usr/local/webserver/php/include/php/
[Root@lee mbstring]#

After installation, we can enter the extensions directory to see if the module exists, indicating that the installation was successful.

Copy Code code as follows:

[Root@lee mbstring]# cd/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
[Root@lee no-debug-non-zts-20090626]# LL
Total dosage 1880
-rwxr-xr-x. 1 root 414405 June eaccelerator.so
-rwxr-xr-x. 1 root 1091242 September imagick.so
-rwxr-xr-x. 1 root root 5285 February 15:07 mbstring.so
-rwxr-xr-x. 1 root 246752 September memcache.so
-rwxr-xr-x. 1 root 154252 September pdo_mysql.so

Install EXIF module

Similar to the installation of Mbstring module, first find the source location and CD in and configure the installation, specific parameters to see their own environment.

Copy Code code as follows:

[Root@lee exif]# Cd/data0/software/php-5.3.13/ext/exif
[Root@lee exif]#./configure--with-php-config=/usr/local/webserver/php/bin/php-config
[Root@lee exif]# make && make install
Installing Shared extensions:/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
[Root@lee exif]#

Enter Extensions directory to verify that the installation is successful

Copy Code code as follows:

[Root@lee exif]# cd/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
[Root@lee no-debug-non-zts-20090626]# LL
Total dosage 2036
-rwxr-xr-x. 1 root 414405 June eaccelerator.so
-rwxr-xr-x. 1 root root 158554 February 15:25 exif.so
-rwxr-xr-x. 1 root 1091242 September imagick.so
-rwxr-xr-x. 1 root root 5285 February 15:07 mbstring.so
-rwxr-xr-x. 1 root 246752 September memcache.so
-rwxr-xr-x. 1 root 154252 September pdo_mysql.so
[Root@lee no-debug-non-zts-20090626]#

The exif.so module already exists.

adding modules to PHP.ini
Open PHP.ini Add the following two lines

Copy Code code as follows:
Extension = "mbstring.so"
Extension = "exif.so"

and verify that your Extension_dir value is the same as the installing shared extensions value that you prompted when you installed the module, such as when I installed the module and prompted me to extensions position is
Copy Code code as follows:
/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/

So the extension_dir in your php.ini will point to the correct directory.
Copy Code code as follows:
Extension_dir= "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/"

Save PHP.ini, restart webserver.
Open Phpinfo () Find the property to see if it is working properly

Normally you will see the following two module information

Use Exif_read_data () to read EXIF information for a picture

The type of picture that supports the read EXIF information is already stated in Phpinfo, only JPEG or TIFF type, where JPEG is a common type, which is sufficient.
Let's look at the manual for the Exif_read_data () function.

Copy Code code as follows:

Array Exif_read_data (String $filename [, String $sections = NULL [, bool $arrays = False [, bool $thumbnail = false]]] )

Parameters:

FileName: To read the picture of EXIF information image path, this can not be a URL
Sections: is a comma-delimited list of extents that need to exist in a file to produce an array of results. If the requested section is not found, the return value is FALSE.

FILE FileName, FileSize, FileDateTime, Sectionsfound
Computed Html,width,height,iscolor, there may be more. Height and Width are computed in the same way as getimagesize (), so their values cannot be part of any returned header information. In addition, HTML is a height/width text string that can be used in plain HTML .
Any_tag Any information that contains a mark, such as Ifd0,exif, ...
IFD0 Tag data for all IFD0. In a standard image file This contains the image size and other.
THUMBNAIL If you have a second IFD, the file should contain a thumbnail image. All markup information about embedded thumbnails is stored in this area.
COMMENT The annotation header information for the JPEG image.
Exif The EXIF section is a ifdo that contains more details about the image. Most of the content is digital camera related.

Arrays: Specifies whether each section becomes an array. The sections Computed,thumbnail and comment sections are always arrays because they contain names that conflict with other sections.

Thumbnail: When set to TRUE, the thumbnail itself is read. Otherwise, only the tagged data is read.

Let's try to read the EXIF information of a picture.

Copy Code code as follows:

<?php
$exif = getexif (' a.jpg ');
Echo ' <pre> ';
Print_r ($EXIF);
Echo ' </pre> ';

Execution results:
Copy Code code as follows:

Array
(
[FileName] => a.jpg
[FileDateTime] => 1361340032
[FileSize] => 69170
[FileType] => 2
[MimeType] => image/jpeg
[Sectionsfound] => Any_tag, IFD0, THUMBNAIL, EXIF, GPS, INTEROP
[Computed] => Array
(
[HTML] => width= "height=" 397 "
[Height] => 397
[Width] => 600
[IsColor] => 1
[Byteordermotorola] => 1
[Aperturefnumber] => f/13.0
[Focusdistance] => 3.76m
[Usercomment] =>
[Usercommentencoding] => ASCII
[Copyright] =>
[Thumbnail.filetype] => 2
[Thumbnail.mimetype] => image/jpeg
)
[ImageWidth] => 4928
[Imagelength] => 3264
[BitsPerSample] => Array
(
[0] => 8
[1] => 8
[2] => 8
)
[Photometricinterpretation] => 2
[Make] => NIKON CORPORATION
[Model] => NIKON D7000
[Orientation] => 1
[Samplesperpixel] => 3
[XResolution] => 3000000/10000
[YResolution] => 3000000/10000
[Resolutionunit] => 2
[Software] => Adobe Photoshop CS5 Windows
[DateTime] => 2013:02:18 20:50:46
[Whitepoint] => Array
(
[0] => 313/1000
[1] => 329/1000
)
[Primarychromaticities] => Array
(
[0] => 64/100
[1] => 33/100
[2] => 21/100
[3] => 71/100
[4] => 15/100
[5] => 6/100
)
[Ycbcrcoefficients] => Array
(
[0] => 299/1000
[1] => 587/1000
[2] => 114/1000
)
[Ycbcrpositioning] => 2
[Copyright] =>
[Exif_ifd_pointer] => 500
[Gps_ifd_pointer] => 1248
[THUMBNAIL] => Array
(
[Compression] => 6
[XResolution] => 72/1
[YResolution] => 72/1
[Resolutionunit] => 2
[Jpeginterchangeformat] => 1362
[Jpeginterchangeformatlength] => 4784
)
[Exposuretime] => 40/10
[Fnumber] => 130/10
[Exposureprogram] => 1
[Isospeedratings] => 1000
[undefinedtag:0x8830] => 2
[Exifversion] => 0230
[Datetimeoriginal] => 2013:02:14 21:12:08
[datetimedigitized] => 2013:02:14 21:12:08
[Componentsconfiguration] =>
[Compressedbitsperpixel] => 4/1
[Shutterspeedvalue] => -2/1
[Aperturevalue] => 7400879/1000000
[Exposurebiasvalue] => 2/6
[Maxaperturevalue] => 36/10
[Subjectdistance] => 376/100
[Meteringmode] => 3
[LightSource] => 0
[Flash] => 16
[Focallength] => 180/10
[Usercomment] => ASCII
[Subsectime] => 10
[Subsectimeoriginal] => 10
[Subsectimedigitized] => 10
[Flashpixversion] => 0100
[ColorSpace] => 65535
[Exifimagewidth] => 600
[Exifimagelength] => 397
[Interoperabilityoffset] => 1216
[Sensingmethod] => 2
[Filesource] =>
[Scenetype] =>
[Cfapattern] =>
[customrendered] => 0
[Exposuremode] => 1
[Whitebalance] => 0
[Digitalzoomratio] => 1/1
[Focallengthin35mmfilm] => 27
[Scenecapturetype] => 0
[Gaincontrol] => 2
[Contrast] => 0
[Saturation] => 0
[Sharpness] => 0
[Subjectdistancerange] => 0
[undefinedtag:0xa500] => 22/10
[Gpsversion] =>
[Interoperabilityindex] => R03
[Interoperabilityversion] => 0100
)

If prompted:

Copy Code code as follows:

Fatal error:call to undefined function exif_read_data () in/data0/htdocs/www/exif/index.php in line 2

It means that the module is not open, it may be that you configure which piece is not configured well, reconfigure it.

Extract useful information from EXIF information read results

From the above results we found that the pictures exif a lot, we just need to filter out the rubbish information is left to useful. This example writes a PHP function on the premise of commonly used parameters. Commonly used parameters include shutter, device name, aperture, sensitivity, focal length:

Copy code code as follows:

<?php
/**
 * read EXIF information for JPEG images
 * $img for picture paths
 *
 * Jones blog
 */
 
Function getexif ($img) {
    
    $exif = Exif_read_data ($img, ' IFD0 ');
 
    return Array (
        ' filename ' => $exif [' Fi] Lename '],
        ' equipment brand ' => $exif [' Make '],
    & nbsp;   ' equipment ' => $exif [' Model '],
        ' shutter ' => $exif [' Exposuretime '],
        ' aperture ' => $exif [' Fnumber '],
   & nbsp;    ' focal length ' => $exif [' focallength '],
        ' sensitivity ' => $ exif[' isospeedratings ']
   );
 
}

Read photos

Copy Code code as follows:

<?php
$exifInfo = getexif (' a.jpg ');
Echo ' <pre> ';
Print_r ($exifInfo);
Echo ' </pre> ';

Execution results:
Copy Code code as follows:

Array
(
[FileName] => 25556306.jpg
[Equipment Brand] => NIKON CORPORATION
[Equipment] => NIKON D3100
[Shutter] => 10/32000
[Aperture] => 18/10
[Focal length] => 350/10
[ISO] => 100
)

Other Notes

The EXIF value of the picture can be modified by the corresponding tool, so use the program to read the image EXIF value can only be used as a reference, do not make the true basis.

Interested friends can also visit the online Read EXIF information site http://exif.cn play

The EXIF information read through the PHP module is occasionally wrong or incomplete, and in this case we can use a third-party tool. And then use PHP to execute system linux commands to read

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.