


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.
Get image type information in EXIF
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" // }
Get complete EXIF information
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"]=> // …… // ……
Get 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 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 ) // …… // …… // ……
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 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� // …… // ……
Summary
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
