PHP中读取照片exif信息的方法
这篇文章主要介绍了PHP中读取照片exif信息的方法,本文从什么是exif信息开始讲解,使用PHP需要什么扩展、及它的安装方法和使用代码示例等,需要的朋友可以参考下
先来了解什么是图片的Exif信息
Exif是一种图象文件格式,它的数据存储与JPEG格式是完全相同的。实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈、快门、白平衡、ISO、焦距、日期时间等各种和拍摄条件以及相机品牌、型号、色彩编码、拍摄时录制的声音以及全球定位系统(GPS)、缩略图等。简单地说,Exif=JPEG+拍摄参数。因此,你可以利用任何可以查看JPEG文件的看图软件浏览Exif格式的照片,但并不是所有的图形程序都能处理Exif信息。
以上引自百度百科。
读取照片的exif在很多时候都没有必要,但相对于一些探讨摄影技术的站点,那么读取照片的exif信息就显得尤为重要了,比如摄影论坛蜂鸟。
截图自蜂鸟论坛,红圈信息部分就是程序读取照片的exif信息。我们把图片下载到本地,使用光影魔术手打开图片看看它的Exif信息,当BG然除了光影还有很多工具都能查看图片的Exif值。
除了Exif信息里的镜头值读不出来以外其余的值都能正确读出来。
开启PHP模块
默认情况下,PHP读取图片Exif信息模块是不开启的,我们需要先开启这个模块。
开启Exif模块需要mbstring支持,所以先来安装mbstring,以下是以Linux环境为例,其它环境类似。
安装mbstring模块
首先找到php源码包位置,直接进入ext/mbstring,执行以下命令安装,具体参数得看自己的环境。
复制代码 代码如下:
[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]#
安装好以后,我们可以进入extensions目录看看模块是否存在,存在表示安装成功。
复制代码 代码如下:
[root@lee mbstring]# cd /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
[root@lee no-debug-non-zts-20090626]# ll
总用量 1880
-rwxr-xr-x. 1 root root 414405 6月 12 2012 eaccelerator.so
-rwxr-xr-x. 1 root root 1091242 9月 23 2011 imagick.so
-rwxr-xr-x. 1 root root 5285 2月 20 15:07 mbstring.so
-rwxr-xr-x. 1 root root 246752 9月 23 2011 memcache.so
-rwxr-xr-x. 1 root root 154252 9月 23 2011 pdo_mysql.so
安装exif模块
同安装mbstring模块类似,先找到源码位置并cd进去并配置安装,具体参数得看自己的环境。
复制代码 代码如下:
[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]#
进入extensions目录验证是否安装成功
复制代码 代码如下:
[root@lee exif]# cd /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
[root@lee no-debug-non-zts-20090626]# ll
总用量 2036
-rwxr-xr-x. 1 root root 414405 6月 12 2012 eaccelerator.so
-rwxr-xr-x. 1 root root 158554 2月 20 15:25 exif.so
-rwxr-xr-x. 1 root root 1091242 9月 23 2011 imagick.so
-rwxr-xr-x. 1 root root 5285 2月 20 15:07 mbstring.so
-rwxr-xr-x. 1 root root 246752 9月 23 2011 memcache.so
-rwxr-xr-x. 1 root root 154252 9月 23 2011 pdo_mysql.so
[root@lee no-debug-non-zts-20090626]#
exif.so模块已经存在。
在php.ini中添加模块
打开php.ini添加以下两行
复制代码 代码如下:
extension = "mbstring.so"
extension = "exif.so"
并且确认你的extension_dir值与你安装模块时提示的Installing shared extensions值一致,比如我安装模块时提示我的extensions位置是
复制代码 代码如下:
/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
那么你的php.ini里的extension_dir要指向正确目录
复制代码 代码如下:
extension_dir="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/"
保存php.ini,重启webserver。
打开phpinfo()找到相应属性看看是否已正常工作
正常情况下你会看到如下两个模块信息
使用exif_read_data()读取图片的exif信息
支持读取exif信息的图片类型在phpinfo里已经写明了,只能是jpeg或者tiff类型,其中jpeg是常用类型,这就已经足够。
我们来看看exif_read_data()函数的使用手册
复制代码 代码如下:
array exif_read_data ( string $filename [, string $sections = NULL [, bool $arrays = false [, bool $thumbnail = false ]]] )
参数:
filename :要读取图片exif信息的图片路径,这里不能是URL
sections:是需要存在于文件中的逗号分隔的区段列表用来产生结果数组。如果未找到所请求的区段则返回值为 FALSE。
FILE FileName, FileSize, FileDateTime, SectionsFound

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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
