Get EXIF information of image file using PHP

Time:2021-9-17

In our photos and various image files, there are still some information that can not be seen intuitively, such as location information, type and size of pictures when taking photos by mobile phone. These information is called EXIF information. Generally, image files such as JPG and TIFF will have such information. EXIF is actually customized for this kind of digital photos. It is specially used to record the attribute information and shooting data of digital photos. It was originally formulated by Japan. In fact, it’s easy to understand that Japan basically monopolizes the camera industry, so of course, such standards are formulated by them!

EXIF has native support in Windows system. Right click the picture to open the menu, then click Properties and switch to details to directly see the EXIF information of the picture file. Because these information can be edited at will, they can be used as some references and cannot be obtained as the determined value of some functional attributes. For example, do not fully trust the information in EXIF.

Get the picture type information in exif

First, let’s take a look at the types of images viewed through EXIF.

var_dump(exif_imagetype($png)); // int(3)
echo exif_ imagetype($png) == IMAGETYPE_ PNG ? $ PNG. 'is a PNG picture': $PNG. 'is not a PNG picture', PHP_ EOL;
//.. / img / 1.png is a PNG picture

var_dump(exif_imagetype($jpg)); // int(2)
echo exif_ imagetype($jpg) == IMAGETYPE_ JPEG ? $ Jpg. 'it's a JPG picture': $jpg. 'it's not a JPG picture', PHP_ EOL;
//.. / img / 2.jpg is a JPG picture

Use EXIF directly_ The imagetype () function will return a constant of image type, that is, imagetype_ The picture type represented by the constant information at the beginning. It also includes many other types. Here we just demonstrate the acquisition of the most common JPG and PNG image types.

It is the same as the third attribute returned by getimagesize() function, that is, the attribute with subscript 2. In getimagesize() function, 0 and 1 represent width and height, and 2 represents the type of image.

var_dump(getimagesize($jpg));
// array(7) {
//     [0]=>
//     int(300)
//     [1]=>
//     int(244)
//     [2]=>
//     int(2)
//     [3]=>
//     string(24) "width="300" height="244""
//     ["bits"]=>
//     int(8)
//     ["channels"]=>
//     int(3)
//     ["mime"]=>
//     string(10) "image/jpeg"
//   }

Get complete EXIF information

All EXIF information in the picture is through EXIF_ read_ Data() function.

var_dump(exif_read_data($png));
// PHP Warning:  exif_ read_ Data (1. PNG): file not supported in / users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202011 / source / 11. Use PHP to obtain EXIF information of image file.php on line 14

// Warning: exif_ read_ Data (1. PNG): file not supported in / users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202011 / source / 11. Use PHP to obtain EXIF information of image file.php on line 14

// bool(false)

var_dump(exif_read_data($jpg));
// array(8) {
//     ["FileName"]=>
//     string(5) "2.jpg"
//     ["FileDateTime"]=>
//     int(1605061174)
//     ["FileSize"]=>
//     int(19075)
//     ["FileType"]=>
//     int(2)
// ……
// ……

As mentioned earlier, EXIF information only exists in JPG, tiff and other image formats, so PNG images cannot obtain EXIF information. If EXIF is used for PNG pictures_ read_ Data() will give a warning. For JPG, the full EXIF content will be returned. Here, we only intercepted a part. There are a lot of contents, not only width, height, type and compression ratio. If it is taken by a mobile phone, we can also see the mobile phone manufacturer, geographical location, shutter parameters, aperture parameters, etc. of course, this also has a lot to do with the camera you use. Some manufacturers may have less data. You can take a picture yourself, and then use this function to check it for yourself.

In addition, there is an alias function read_ exif_ Data() and exif_ read_ The function of data () is similar, that is, it is an alias, and has been marked as outdated syntax since php7. You can learn about it.

var_dump(read_exif_data($jpg));
// PHP Deprecated:  Function read_ exif_ Data () is deprecated in / users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202011 / source / 11. Use PHP to obtain EXIF information of image file. PHP on line 17

// Deprecated: Function read_ exif_ Data () is deprecated in / users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202011 / source / 11. Use PHP to obtain EXIF information of image file. PHP on line 17
// array(8) {
//   ["FileName"]=>
//   string(5) "2.jpg"
//   ["FileDateTime"]=>
//   int(1605061174)
//   ["FileSize"]=>
// ……
// ……

Gets the header name of the specified index

The index header corresponds to the field name in EXIF. We can view all currently supported index header information, which contains a lot of content. You can also get the attribute name according to the index header, and then find the corresponding attribute information in EXIF.

echo "256: " . exif_tagname(256) . PHP_EOL;
// 256: ImageWidth
for ($id = 1; $id <= 65535; $id++) {
    if (exif_tagname($id) != "") {
        echo $id . ' ( ' . exif_tagname($id) . ' )', PHP_EOL;
    }
}
// 11 ( ACDComment )
// 254 ( NewSubFile )
// 255 ( SubFile )
// 256 ( ImageWidth )
// 257 ( ImageLength )
// 258 ( BitsPerSample )
// 259 ( Compression )
// ……
// ……
// ……

Read thumbnails embedded in JPG files

Many people may not know that a thumbnail can be saved in EXIF. And the photos taken by everyone’s mobile phone basically have this thumbnail. In Windows system, if the picture has thumbnails, the thumbnails in EXIF will also be used directly. If not, a thumbnails.db file will be automatically generated in the directory, that is, a thumbnail database.

var_dump(exif_thumbnail('../img/3.jpeg'));
// string(14369) "�����

//                         !"$��@"���

// }!1AQa"q2��#B��R��$3br�
// %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������

// w!1AQaq"2B����  #3R�br�
// $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������
//                                                                                                                           ?�b�������������?J�l�2

file_put_contents('../img/3-thumbnail.jpeg', exif_thumbnail('../img/3.jpeg'));

In PHP, you can use EXIF_ The thumbnail () function directly obtains the thumbnail information saved in EXIF in JPG file. After we get it in the test code, we save it as a formal picture, and you can see the real appearance of the thumbnail. And we use EXIF_ read_ Data () to read this file, you can also see the information saved by the thumbnail.

var_dump(exif_read_data('../img/3.jpeg'));
// array(56) {
// ……
// ……
// ……
//     ["Make"]=>
//     string(6) "Xiaomi"
//     ["THUMBNAIL"]=>
//     array(9) {
//       ["JPEGInterchangeFormat"]=>
//       int(5504)
//       ["Orientation"]=>
//       int(6)
//       ["JPEGInterchangeFormatLength"]=>
//       int(14369)
//       ["Compression"]=>
//       int(6)
//       ["ResolutionUnit"]=>
//       int(2)
//       ["XResolution"]=>
//       string(4) "72/1"
//       ["YResolution"]=>
//       string(4) "72/1"
//       ["ExifImageLength"]=>
//       int(240)
//       ["ExifImageWidth"]=>
//       int(320)
//     }
//     ["UndefinedTag:0x9AAA"]=>
//     string(4480) "1y�L�=w%�s_�&��v��oJ��$Gdz|d�9n�
//   �������~��+9����2V:+�о�Qn]�۲͐� ��U��nwF��w;f�h�k���i*w�bd+�D0�=\o��y����x\�,��BS��#/d�9���˓
//                                                                                             ,%C�3���eIZ~��������oLܰܡ~�}#�y:4Ӥ}�    ��dȻGI*Y��
//                                                                                                                                             ��m�)��x#a�6Jʈ7��ݪG�T&,� Ie�%�
  
//   ?��S���§�9��6s��8LT&o
//   Vn�Џ�R��c6��Ϝ p�G�f#��/�o����_�@�$�%#
//                                        � � Ҡ ��5v��~���� Ȣ) � ڽ A I: 3xkm CIE 5& / gap R + he} B Ґ �?
// ……
// ……
// ……

summary

Have a basic understanding of the EXIF of the picture. Some points to note in today’s study are: first, EXIF is only available in JPG and TIFF image formats, second, they are modifiable, and third, a thumbnail can be saved in this thing. Sure enough, learning is a bottomless pit. I opened my eyes when I was not careful. Come on, boys!

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202011/source/11. Get EXIF information of image file using php.php

Reference documents:

https://www.php.net/manual/zh/book.exif.php

https://baike.baidu.com/item/Exif/422825?fr=aladdin

http://www.360doc.com/content/13/0223/14/9290626_267432131.shtml

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532