Home  >  Article  >  Backend Development  >  How to read photo exif information in PHP, _PHP tutorial

How to read photo exif information in PHP, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:20:311081browse

How to read photo exif information in PHP,

First let’s understand what is the Exif information of images

Exif is an image file format, and its data storage is exactly the same as the JPEG format. In fact, the Exif format inserts digital photo information into the header of the JPEG format, including various shooting conditions such as aperture, shutter, white balance, ISO, focal length, date and time, as well as camera brand, model, color coding, shooting Recorded sounds as well as Global Positioning System (GPS), thumbnails, etc. Simply put, Exif=JPEG+shooting parameters. Therefore, you can use any image viewing software that can view JPEG files to view photos in Exif format, but not all graphics programs can handle Exif information.

The above is quoted from Baidu Encyclopedia.

Reading the EXIF ​​information of photos is not necessary in many cases, but compared to some sites that discuss photography technology, reading the EXIF ​​information of photos is particularly important, such as the photography forum Hummingbird.

Screenshot from Hummingbird Forum, the red circle information is the exif information of the photo read by the program. We download the image locally and use Light and Shadow Magic to open the image to see its Exif information. Of course, besides Light and Shadow, there are many tools that can check the Exif value of the image.

Except for the lens value in the Exif information which cannot be read out, all other values ​​can be read correctly.

Enable PHP module

By default, the PHP module for reading image Exif information is not enabled. We need to enable this module first.

Opening the Exif module requires mbstring support, so install mbstring first. The following is based on the Linux environment as an example. Other environments are similar.

Install mbstring module

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

Copy code The code is 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. If it exists, it means the installation is successful.

Copy code 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 root 414405 June 12 2012 eaccelerator.so
-rwxr-xr-x. 1 root root 1091242 September 23 2011 imagick.so
-rwxr-xr-x. 1 root root 5285 February 20 15:07 mbstring.so
-rwxr-xr-x. 1 root root 246752 September 23 2011 memcache.so
-rwxr-xr-x. 1 root root 154252 September 23 2011 pdo_mysql.so

Install exif module

Similar to installing the mbstring module, first find the source code location, cd into it and configure the installation. The specific parameters depend on your own environment.

Copy code 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]#

Enter the extensions directory to verify whether the installation is successful

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

exif.so module already exists.

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

Copy code The code is as follows:
extension = "mbstring.so"
extension = "exif.so"

And confirm 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 location of my extensions prompted is
Copy the code The code is as follows:
/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626 /

Then the extension_dir in your php.ini must point to the correct directory
Copy the code The code is as follows:
extension_dir="/usr/local/webserver/php/lib/php/extensions/no-debug-non- zts-20090626/"

Save php.ini and restart the webserver.
Open phpinfo() and find the corresponding properties to see if they are working properly

Normally you will see the following two module information

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

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

Copy code The code is as follows:

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

Parameters:

filename: The image path to read the exif information of the image. This cannot be a URL
sections: is a comma-separated list of sections that need to exist in the file to produce the result array. The return value is FALSE if the requested section is not found.

FILE FileName, FileSize, FileDateTime, SectionsFound
COMPUTED html,Width,Height,IsColor,可能有更多其它的。Height 和 Width 是用和 getimagesize() 一样的方法计算的,因此它们的值不能是任何返回的头信息的部分。此外 html 是一个 height/width 的文本字符串可以用于普通的HTML 中。
ANY_TAG 任何包含有标记的信息,例如 IFD0,EXIF,...
IFD0 所有 IFD0 的标记数据。在标准的图像文件中这包含了图像大小及其它。
THUMBNAIL 如果有第二个 IFD,文件应该包含有缩略图。所有有关嵌入缩略图的标记信息都存储在本区。
COMMENT JPEG 图像的注释头信息。
EXIF EXIF 区段是 IFDO 的子区,包含有图像的更多详细信息。大多数内容都是数码相机相关的。

arrays: Specifies whether each section becomes an array. sections COMPUTED, THUMBNAIL and COMMENT sections always become arrays because they contain names that conflict with other sections.

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

Let’s try reading the exif information of an image

Copy code The code is as follows:

$exif = getExif('a.jpg');
echo '
';<br>
print_r($exif);<br>
echo '
';

Execution result:
Copy code The code is 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="600" 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
)

如果提示:

复制代码 代码如下:

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

则表示模块没有打开,可能是你配置哪一块没有配置好,重新配置就好。

从Exif信息读取结果中取出有用的信息

从以上的执行结果我们发现图片Exif很多,我们只需要从中过滤掉垃圾信息剩下有用的就好。本例就以常用的参数为前提写一个PHP函数。常用的参数包括快门,器材名称,光圈,感光度,焦距:

复制代码 代码如下:

/**
* Read Exif information of jpeg images
* $img is the image path
*
* Qiongtai Blog
​*/

function getExif($img){
     
$exif = exif_read_data($img, 'IFD0');

Return array (
          'File Name' => $exif['FileName'],
          'Equipment Brand' => $exif['Make'],
        'Equipment' => $exif['Model'],
         'Shutter' => $exif['ExposureTime'],
         'Aperture' => $exif['FNumber'],
         'Focal length' => $exif['FocalLength'],
         'Sensitivity' => $exif['ISOSpeedRatings']
);

}

Read photos

Copy code The code is as follows:

$exifInfo = getExif('a.jpg');
echo '
';<br>
print_r($exifInfo);<br>
echo '
';

Execution result:
Copy code The code is as follows:

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

Other instructions

The Exif value of the picture can be modified through the corresponding tool, so using the program to read the Exif value of the picture can only be used as a reference and not as a real basis.

Interested friends can also visit the online Exif information reading website http://exif.cn to have fun

The Exif information read through the PHP module is occasionally wrong or the information is incomplete. In this case, we can use third-party tools. Then use php to execute the system linux command to read

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/866676.htmlTechArticleHow to read photo exif information in PHP. First, let’s understand what is the Exif information of a picture. Exif is a kind of picture. Like file format, its data storage is exactly the same as JPEG format. Actually Exi...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn