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.
[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.
[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.
[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
[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
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
Then the extension_dir in your php.ini must point to the correct directory
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
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
$exif = getExif('a.jpg');
echo '
';<br> print_r($exif);<br> echo '';
Execution result:
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
$exifInfo = getExif('a.jpg');
echo '
';<br> print_r($exifInfo);<br> echo '';
Execution result:
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

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor