Home >Backend Development >PHP Tutorial >A brief discussion on how to use PHP to read the EXIF information of images (with code)
How to use PHP to read the EXIF information of images? The following article will introduce to you how to use PHP to obtain the EXIF information of image files. I hope it will be helpful to you!
In the photos we take and various image files, there is actually some information stored that cannot be seen directly, such as the location information when taking pictures with a mobile phone. The type, size, etc. of the image, this information is called EXIF information. Generally, image files such as JPG and TIFF will have such information. EXIF is actually specially 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 in Japan. This is actually easy to understand. Japan basically has a monopoly on the camera industry, so of course these standards are set by them!
EXIF has native support in Windows systems. Right-click the image to open the menu, then click Properties and switch to Details to directly see the EXIF information of the image file. Since this information can be edited at will, they can be used as some references and cannot be obtained as definite values of certain functional attributes. For example, do not completely trust the information in EXIF, such as width and height.
First, let’s look at the type of image through EXIF.
var_dump(exif_imagetype($png)); // int(3) echo exif_imagetype($png) == IMAGETYPE_PNG ? $png . '是 PNG 图片' : $png . '不是 PNG 图片', PHP_EOL; // ../img/1.png是 PNG 图片 var_dump(exif_imagetype($jpg)); // int(2) echo exif_imagetype($jpg) == IMAGETYPE_JPEG ? $jpg . '是 jpg 图片' : $jpg . '不是 JPG 图片', PHP_EOL; // ../img/2.jpg是 jpg 图片
Using the exif_imagetype() function directly will return a constant of the image type, which is the image type represented by the constant information starting with IMAGETYPE_. It also includes many other types. Here we only demonstrate the acquisition of our most common jpg and png image types.
It is the same as the third attribute returned by the getimagesize() function, that is, the content of the attribute with the subscript 2. In the getimagesize() function, 0 and 1 represent the width and height. 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" // }
All complete EXIF information in the picture is obtained through the exif_read_data() function.
var_dump(exif_read_data($png)); // PHP Warning: exif_read_data(1.png): File not supported in /Users/zhangyue/MyDoc/博客文章/dev-blog/php/202011/source/11.使用PHP获取图像文件的EXIF信息.php on line 14 // Warning: exif_read_data(1.png): File not supported in /Users/zhangyue/MyDoc/博客文章/dev-blog/php/202011/source/11.使用PHP获取图像文件的EXIF信息.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 before, EXIF information only exists in image formats such as JPG and TIFF, so PNG images cannot obtain EXIF information. A warning will be issued if exif_read_data() is used on PNG images. For JPG, the complete EXIF content will be returned. Here we have only intercepted part of it. There is a lot of content, not just width, height, type, compression ratio, etc. If it is taken with a mobile phone, you 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 of this and use this function to check it out for yourself.
In addition, there is an alias function read_exif_data(), which has a similar function to exif_read_data(). It is an alias of it and has been marked as outdated syntax after PHP7. Everyone can find out.
var_dump(read_exif_data($jpg)); // PHP Deprecated: Function read_exif_data() is deprecated in /Users/zhangyue/MyDoc/博客文章/dev-blog/php/202011/source/11.使用PHP获取图像文件的EXIF信息.php on line 17 // Deprecated: Function read_exif_data() is deprecated in /Users/zhangyue/MyDoc/博客文章/dev-blog/php/202011/source/11.使用PHP获取图像文件的EXIF信息.php on line 17 // array(8) { // ["FileName"]=> // string(5) "2.jpg" // ["FileDateTime"]=> // int(1605061174) // ["FileSize"]=> // …… // ……
The index header corresponds to the field name in EXIF. We can view all currently supported index header information, which has a lot of content. You can also obtain the attribute name based on the index header, and then search for 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 ) // …… // …… // ……
Many people may not know that a thumbnail can be saved in EXIF. And the photos taken by everyone’s mobile phone will basically have this thumbnail. In Windows systems, if the image has a thumbnail, the thumbnail in EXIF will be used directly. If not, a Thumbs.db file will be automatically generated in the directory, which is a thumbnail database.
var_dump(exif_thumbnail('../img/3.jpeg')); file_put_contents('../img/3-thumbnail.jpeg', exif_thumbnail('../img/3.jpeg'));
In PHP, you can directly obtain the thumbnail information saved in EXIF in the JPG file through the exif_thumbnail() function. In the test code, we obtain it and then save it as a formal picture, so that everyone can see what the thumbnail actually looks like. And if we use exif_read_data() to read this file, we can also see the information saved in 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� // …… // ……
Now you have a basic understanding of the EXIF of pictures. A few points to note when learning today are: First, EXIF is only available in image formats such as JPG and TIFF, second, they are modifiable, and third, this thing can even save a thumbnail. Sure enough, learning is a bottomless pit, and I accidentally opened my eyes again. Come on, boys!
Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/202011/source/11. Use PHP to obtain the EXIF information of the image file.php
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of A brief discussion on how to use PHP to read the EXIF information of images (with code). For more information, please follow other related articles on the PHP Chinese website!