How to Read photo exif information in PHP

Source: Internet
Author: User
This article describes how to read the exif information of a photo in PHP. This article describes how to read the exif information, how to use PHP, how to install it, and how to use code examples, for more information, see First, let's take a look at the Exif information of an image.

Exif is an image file format, and its data storage is exactly the same as JPEG format. In fact, the Exif format is to insert digital photo information in the JPEG format header, including the aperture, shutter, white balance, ISO, focal length, date, and other shooting conditions during shooting, as well as the camera brand, model, color encoding, recording sound during shooting, and the global positioning system (GPS) and thumbnails. In short, Exif = JPEG + shooting parameters. Therefore, you can use any image viewer software that can view JPEG files to browse photos in Exif format, but not all graphics programs can process Exif information.

The above is taken from Baidu Encyclopedia.

Reading the exif of a photo is not necessary in many cases. However, reading the exif information of a photo is especially important for some sites that discuss photography technology, such as the hummingbird in the photography forum.

From the hummingbird Forum, the red circle information is the exif information of the photos read by the program. We will download the image to a local device and use the magic hand to open the image to see its Exif information. when BG, there are still many tools besides light and shade to view the Exif value of the image.

The values except the Exif information cannot be read.

Enable PHP module

By default, PHP does not enable the Exif module for reading images. we need to enable this module first.

Enabling the Exif module requires mbstring support. Therefore, install mbstring first. the following uses the Linux environment as an example. Other environments are similar.

Install the mbstring module

First, find the location of the php source code package, directly go to ext/mbstring, and execute the following command to install the package. for specific parameters, see your environment.

The code is as follows:


[Root @ lee ext] # cd/data0/software/php/ext/mbstring
[Root @ lee mbstring] #/usr/local/webserver/php/bin/phpize
Processing ING:
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 the installation, you can go to the extensions directory to check whether the module exists. If yes, the installation is successful.

The code is 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 usage 1880
-Rwxr-xr-x. 1 root 414405 August June 12 2012 eaccelerator. so
-Rwxr-xr-x. 1 root 1091242 September 23 2011 imagick. so
-Rwxr-xr-x. 1 root 5285 15:07 mbstring. so
-Rwxr-xr-x. 1 root 246752 August September 23 2011 memcache. so
-Rwxr-xr-x. 1 root 154252 August September 23 2011 pdo_mysql.so

Install the exif module

Similar to installing the mbstring module, find the source code and install it on cd. for specific parameters, see your environment.

The code is 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] #

Go to the extensions directory to verify whether the installation is successful.

The code is 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 usage 2036
-Rwxr-xr-x. 1 root 414405 August June 12 2012 eaccelerator. so
-Rwxr-xr-x. 1 root 158554 February 20 15:25 exif. so
-Rwxr-xr-x. 1 root 1091242 September 23 2011 imagick. so
-Rwxr-xr-x. 1 root 5285 15:07 mbstring. so
-Rwxr-xr-x. 1 root 246752 August September 23 2011 memcache. so
-Rwxr-xr-x. 1 root 154252 August September 23 2011 pdo_mysql.so
[Root @ lee no-debug-non-zts-20090626] #

The exif. so module already exists.

Add a module to php. ini
Open php. ini and add the following two lines

The code is as follows:

Extension = "mbstring. so"
Extension = "exif. so"


Make sure that your extension_dir value is consistent with the Installing shared extensions value prompted when you install the module. for example, when I install the module, the system prompts that my extensions location is

The code is as follows:

/Usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/


The extension_dir in your php. ini should point to the correct directory.

The code is as follows:

Extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626 /"


Save php. ini and restart webserver.
Open phpinfo () and find the corresponding property to see if it is working properly.

Normally, you will see the following two modules

Use exif_read_data () to read the exif information of an image.

The image types that support reading exif information have been written in phpinfo and can only be jpeg or tiff. jpeg is a common type, which is sufficient.
Let's take a look at the user manual of the exif_read_data () function.

The code is as follows:


Array exif_read_data (string $ filename [, string $ sections = NULL [, bool $ arrays = false [, bool $ thumbnail = false])

Parameters:

Filename: the path of the image to read the Image exif information. the URL cannot be used here.
Sections: a list of fields that must be separated by commas (,) in the file to generate a result array. If the requested segment is not found, the returned value is FALSE.

FILE FileName, FileSize, FileDateTime, SectionsFound
COMPUTED Html, Width, Height, IsColor, and more. Height and Width are calculated using the same method as getimagesize (), so their values cannot be part of any returned header information. In addition, html is a text string of height/width that can be used for common HTML.
ANY_TAG Any information that contains tags, such as IFD0, EXIF ,...
IFD0 Mark data of all IFD0 values. The standard image file contains the image size and others.
THUMBNAIL If there is a second IFD, the file should contain a thumbnail. All tag information about embedded thumbnails is stored in this area.
COMMENT The comment header of the JPEG image.
EXIF The EXIF section is a subset of IFDO and contains more details about the image. Most of the content is related to digital cameras.

Arrays: Specifies whether each segment is an array. Sections COMPUTED, THUMBNAIL, and COMMENT segments are always arrays, because their names conflict with other segments.

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

Let's read the exif information of an image.

The code is as follows:


<? Php
$ Exif = getExif('a.jpg ');
Echo'

';
print_r($exif);
echo '
';


Execution result:

The code is as follows:


Array
(
[FileName] => a.jpg
[FileDateTime] = & gt; 1361340032
[FileSize] = & gt; 69170
[FileType] => 2
[MimeType] => image/jpeg
[SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS, INTEROP
[COMPUTED] => Array
(
[Html] => width = "600" height = "397"
[Height] = & gt; 397
[Width] = & gt; 600
[IsColor] => 1
[ByteOrderMotorola] => 1
[ApertureFNumber] = & gt; f/13.0
[FocusDistance] = & gt; 3.76 m
[UserComment] =>
[UserCommentEncoding] => ASCII
[Copyright] =>
[Thumbnail. FileType] => 2
[Thumbnail. MimeType] => image/jpeg
)
[ImageWidth] = & gt; 4928
[ImageLength] = & gt; 3264
[BitsPerSample] => Array
(
[0] => 8
[1] => 8
[2] => 8
)
[PhotometricInterpretation] => 2
[Make] => NIKON CORPORATION
[Model] => NIKON D7000
[Orientation] => 1
[SamplesPerPixel] => 3
[XResolution] = & gt; 3000000/10000
[YResolution] = & gt; 3000000/10000
[ResolutionUnit] => 2
[Software] => Adobe Photoshop CS5 Windows
[DateTime] => 20:50:46
[WhitePoint] => Array
(
[0] = & gt; 313/1000
[1] => 329/1000.
)
[PrimaryChromaticities] => Array
(
[0] = & gt; 64/100
[1] => 33/100.
[2] => 21/100.
[3] = & gt; 71/100
[4] = & gt; 15/100
[5] = & gt; 6/100
)
[YCbCrCoefficients] => Array
(
[0] = & gt; 299/1000
[1] => 587/1000.
[2] => 114/1000.
)
[YCbCrPositioning] => 2
[Copyright] =>
[Exif_IFD_Pointer] = & gt; 500
[GPS_IFD_Pointer] = & gt; 1248
[THUMBNAIL] => Array
(
[Compression] => 6
[XResolution] = & gt; 72/1
[YResolution] = & gt; 72/1
[ResolutionUnit] => 2
[Required interchangeformat] => 1362
[Required interchangeformatlength] => 4784
)
[ExposureTime] = & gt; 40/10
[FNumber] = & gt; 130/10
[ExposureProgram] => 1
[ISOSpeedRatings] = & gt; 1000
[UndefinedTag: 0x8830] => 2
[ExifVersion] = & gt; 0230
[DateTimeOriginal] => 21:12:08
[DateTimeDigitized] => 21:12:08
[ComponentsConfiguration] =>
[CompressedBitsPerPixel] = & gt; 4/1
[ShutterSpeedValue] =>-2/1
[ApertureValue] = & gt; 7400879/1000000
[ExposureBiasValue] = & gt; 2/6
[MaxApertureValue] = & gt; 36/10
[SubjectDistance] = & gt; 376/100
[MeteringMode] => 3
[LightSource] => 0
[Flash] => 16
[FocalLength] = & gt; 180/10
[UserComment] => ASCII
[SubSecTime] => 10
[SubSecTimeOriginal] => 10
[SubSecTimeDigitized] => 10
[FlashPixVersion] = & gt; 0100
[ColorSpace] = & gt; 65535
[ExifImageWidth] = & gt; 600
[ExifImageLength] = & gt; 397
[InteroperabilityOffset] = & gt; 1216
[SensingMethod] => 2
[FileSource] =>
[SceneType] =>
[CFAPattern] =>
[CustomRendered] => 0
[ExposureMode] => 1
[WhiteBalance] => 0
[DigitalZoomRatio] = & gt; 1/1
[FocalLengthIn35mmFilm] => 27
[SceneCaptureType] => 0
[GainControl] => 2
[Contrast] => 0
[Saturation] => 0
[Sharpness] => 0
[SubjectDistanceRange] => 0
[UndefinedTag: 0xA500] = & gt; 22/10
[GPSVersion] =>
[InterOperabilityIndex] => R03
[InterOperabilityVersion] = & gt; 0100
)

If the prompt is:

The code is as follows:


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


It indicates that the module has not been opened. it may be that you have not configured any part of the module. you just need to reconfigure it.

Obtain useful information from the result of reading the Exif information

From the above execution results, we found that there are many Exif images, and we only need to filter out the junk information and the remaining useful ones. In this example, a PHP function is written based on common parameters. Common parameters include Shutter, device name, aperture, sensitivity, and focal length:

The code is as follows:


<? Php
/**
* Read the Exif information of jpeg images.
* $ Img indicates the image path
*
* Qiongtai blog
*/

Function getExif ($ img ){

$ Exif = exif_read_data ($ img, 'ifd0 ');

Return array (
'Filename '=> $ exif ['filename'],
'Device brands' => $ exif ['make'],
'Device '=> $ exif ['model'],
'Shutter '=> $ exif ['exposuretime'],
'Aperture '=> $ exif ['fnumber'],
'Focal Cid' => $ exif ['focallength '],
'Sensitivity '=> $ exif ['isospeedratings']
);

}

Read photo

The code is as follows:


<? Php
$ ExifInfo = getExif('a.jpg ');
Echo'

';
print_r($exifInfo);
echo '
';


Execution result:

The code is as follows:


Array
(
[File name] => 25556306.jpg
[Device brand] => NIKON CORPORATION
[Equipment] => NIKON D3100
[Shutter] => 10/32000
[Aperture] = & gt; 18/10
[Focal length] => 350/10
[Sensitivity] = & gt; 100
)

Other instructions

The Exif value of the image can be modified using corresponding tools. Therefore, you can only use the program to read the Exif value of the image as a reference, without any actual basis.

Interested friends can also visit online read Exif information website http://exif.cn play

The Exif information read through the PHP module is occasionally incorrect or incomplete. in this case, we can use a third-party tool. Then use php to execute the linux command 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.