php获取图片gps经纬度等exif信息,centos安装exif模块
浏览数 184784
赞
(0)
先来了解什么是图片的Exif信息
Exif是一种图象文件格式,它的数据存储与JPEG格式是完全相同的。实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈、快门、白平衡、ISO、焦距、日期时间等各种和拍摄条件以及相机品牌、型号、色彩编码、拍摄时录制的声音以及全球定位系统(GPS)、缩略图等。简单地说,Exif=JPEG+拍摄参数。因此,你可以利用任何可以查看JPEG文件的看图软件浏览Exif格式的照片,但并不是所有的图形程序都能处理Exif信息。
开启PHP模块
默认情况下,PHP读取图片Exif信息模块是不开启的,我们需要先开启这个模块。
开启Exif模块需要mbstring支持,所以先来安装mbstring,以下是以Linux环境为例,其它环境类似。
安装mbstring模块
首先找到php源码包位置,直接进入ext/mbstring,执行以下命令安装,具体参数得看自己的环境。
如果不知道源码包在哪里可以全盘搜索
find / -name "mbstring"
安装好以后,我们可以进入extensions目录看看模块是否存在,存在表示安装成功。
安装exif模块
同安装mbstring模块类似,先找到源码位置并cd进去并配置安装,具体参数得看自己的环境。
进入extensions目录验证是否安装成功
在php.ini中添加模块
打开php.ini添加以下两行
并且确认你的extension_dir值与你安装模块时提示的Installing shared extensions值一致,比如我安装模块时提示我的extensions位置是
/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
那么你的php.ini里的extension_dir要指向正确目录
如果你的多个模块的目录不一样,也可以在引入时填写绝对地址
保存php.ini,重启webserver。
打开phpinfo()找到相应属性看看是否已正常工作
正常情况下你会看到 mbstring 和 exif 两个模块信息


使用exif_read_data()读取图片的exif信息
支持读取exif信息的图片类型在phpinfo里已经写明了,只能是jpeg或者tiff类型,其中jpeg是常用类型,这就已经足够。
我们来上传一张图片获取它的exif信息试试。
执行结果:
如果提示以下信息则表示模块没有打开,可能是你配置哪一块没有配置好,重新配置就好:
其中,经纬度获取的是含有三个元素数组的数组,分别为:度、分、秒,都用分数表示;
在GPS设备中对于坐标单位有不同的显示方法,有度分秒(DMS)和度(DDD)。
例如:南京玄武区板仓街世界之窗创业园的GPS坐标如下:
度分秒(DMS):东经E 118度48分54.152秒 北纬N 32度04分10.461秒
度(DDD):E 118.815033度 N 32.069723度
如何将度(DDD):: 118.815033度换算成度分秒(DMS)东经E 118度48分53.652秒?转换方法是将118.815033整数位不变取118(度),用0.815033*60=48.90198,取整数位48(分),0.90198*60=54.1188再取整数位54(秒),故转化为118度48分54秒.
同样将度分秒(DMS):东经E 118度48分54.152秒 换算成度(DDD)的方法如下:118度48分54.152秒=118+(48/60)+(54.152/3600)=118.81504度
因为计算时小数位保留的原因,导致正反计算存在一定误差,但误差影响不是很大。1秒的误差就是几米的样子。各位GPS玩友可以用上述方法换算成自己需要的单位坐标。
以下只能获取到文件类型信息:
这是使用fileinfo模块的功能
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
var_dump($mimetype);
结果:
string(10) "image/jpeg"
Exif是一种图象文件格式,它的数据存储与JPEG格式是完全相同的。实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈、快门、白平衡、ISO、焦距、日期时间等各种和拍摄条件以及相机品牌、型号、色彩编码、拍摄时录制的声音以及全球定位系统(GPS)、缩略图等。简单地说,Exif=JPEG+拍摄参数。因此,你可以利用任何可以查看JPEG文件的看图软件浏览Exif格式的照片,但并不是所有的图形程序都能处理Exif信息。
开启PHP模块
默认情况下,PHP读取图片Exif信息模块是不开启的,我们需要先开启这个模块。
开启Exif模块需要mbstring支持,所以先来安装mbstring,以下是以Linux环境为例,其它环境类似。
安装mbstring模块
首先找到php源码包位置,直接进入ext/mbstring,执行以下命令安装,具体参数得看自己的环境。
如果不知道源码包在哪里可以全盘搜索
find / -name "mbstring"
[root@lee ext]# cd /software/php/ext/mbstring [root@lee mbstring]# /usr/local/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/php/bin/php-config [root@lee mbstring]# make && make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ Installing header files: /usr/local/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@localhost php]# cd /home/dzthink/package/php-5.6.22/ext/exif [root@localhost exif]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@localhost exif]# ./configure --with-php-config=/usr/local/php/bin/php-config checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for a sed that does not truncate output... /bin/sed ... creating libtool appending configuration tag "CXX" to libtool configure: creating ./config.status config.status: creating config.h [sys@iZ22pyio4djZ exif]$ make & make install [1] 15563 /bin/sh /usr/local/download/php-5.6.23/ext/exif/libtool --mode=compile cc -I. -I/usr/local/download/php-5.6.23/ext/exif -DPHP_ATOM_INC -I/usr/local/download/php-5.6.23/ext/exif/include -I/usr/local/download/php-5.6.23/ext/exif/main -I/usr/local/download/php-5.6.23/ext/exif -I/usr/local/webserver/php-5.6.23/include/php -I/usr/local/webserver/php-5.6.23/include/php/main -I/usr/local/webserver/php-5.6.23/include/php/TSRM -I/usr/local/webserver/php-5.6.23/include/php/Zend -I/usr/local/webserver/php-5.6.23/include/php/ext -I/usr/local/webserver/php-5.6.23/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /usr/local/download/php-5.6.23/ext/exif/exif.c -o exif.lo ... If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the `LD_RUN_PATH' environment variable during linking - use the `-Wl,--rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to `/etc/ld.so.conf' See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- Build complete. Don't forget to run 'make test'. Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/ [root@localhost exif]# |
进入extensions目录验证是否安装成功
[root@localhost exif]# cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/ [root@localhost no-debug-non-zts-20131226]# ll 总用量 6900 -rwxr-xr-x 1 root root 161640 8月 28 15:42 exif.so -rwxr-xr-x 1 root root 3340651 8月 7 01:09 mongodb.so -rw-r--r-- 1 root root 1862622 8月 9 15:36 mongo.so -rwxr-xr-x. 1 root root 1105624 6月 4 2016 opcache.a -rwxr-xr-x. 1 root root 586784 6月 4 2016 opcache.so [root@localhost no-debug-non-zts-20131226]# [root@localhost no-debug-non-zts-20131226]# ls exif.so mongodb.so mongo.so opcache.a opcache.so [root@localhost no-debug-non-zts-20131226]# |
在php.ini中添加模块
打开php.ini添加以下两行
extension = "mbstring.so" extension = "exif.so" |
并且确认你的extension_dir值与你安装模块时提示的Installing shared extensions值一致,比如我安装模块时提示我的extensions位置是
/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
那么你的php.ini里的extension_dir要指向正确目录
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226" |
如果你的多个模块的目录不一样,也可以在引入时填写绝对地址
extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/exif.so" |
保存php.ini,重启webserver。
[root@localhost data]# pkill php-fpm [root@localhost data]# ps -ef | grep php root 13065 1627 0 16:21 pts/0 00:00:00 grep php [root@localhost data]# /etc/init.d/php5.6-fpm start Starting php-fpm daemon: php-fpm. [root@localhost data]# |
打开phpinfo()找到相应属性看看是否已正常工作
正常情况下你会看到 mbstring 和 exif 两个模块信息


使用exif_read_data()读取图片的exif信息
支持读取exif信息的图片类型在phpinfo里已经写明了,只能是jpeg或者tiff类型,其中jpeg是常用类型,这就已经足够。
我们来上传一张图片获取它的exif信息试试。
$file = $_FILES["img_upload"]; var_dump(read_exif_data($file['tmp_name'])); |
执行结果:
array(6) { ["FILE"]=> array(6) { ["FileName"]=> string(9) "phpUpOhbL" ["FileDateTime"]=> int(1503919035) ["FileSize"]=> int(1790767) ["FileType"]=> int(2) ["MimeType"]=> string(10) "image/jpeg" ["SectionsFound"]=> string(35) "ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS" } ["COMPUTED"]=> array(8) { ["html"]=> string(26) "width="4032" height="3024"" ["Height"]=> int(3024) ["Width"]=> int(4032) ["IsColor"]=> int(1) ["ByteOrderMotorola"]=> int(1) ["ApertureFNumber"]=> string(5) "f/2.2" ["Thumbnail.FileType"]=> int(2) ["Thumbnail.MimeType"]=> string(10) "image/jpeg" } ["IFD0"]=> array(11) { ["Make"]=> string(5) "Apple" ["Model"]=> string(9) "iPhone 6s" ["Orientation"]=> int(1) ["XResolution"]=> string(4) "72/1" ["YResolution"]=> string(4) "72/1" ["ResolutionUnit"]=> int(2) ["Software"]=> string(6) "10.3.3" ["DateTime"]=> string(19) "2017:08:28 16:28:16" ["YCbCrPositioning"]=> int(1) ["Exif_IFD_Pointer"]=> int(206) ["GPS_IFD_Pointer"]=> int(1682) } ["THUMBNAIL"]=> array(6) { ["Compression"]=> int(6) ["XResolution"]=> string(4) "72/1" ["YResolution"]=> string(4) "72/1" ["ResolutionUnit"]=> int(2) ["JPEGInterchangeFormat"]=> int(2086) ["JPEGInterchangeFormatLength"]=> int(9228) } ["EXIF"]=> array(33) { ["ExposureTime"]=> string(4) "1/33" ["FNumber"]=> string(4) "11/5" ["ExposureProgram"]=> int(2) ["ISOSpeedRatings"]=> int(100) ["ExifVersion"]=> string(4) "0221" ["DateTimeOriginal"]=> string(19) "2017:08:28 16:28:16" ["DateTimeDigitized"]=> string(19) "2017:08:28 16:28:16" ["ComponentsConfiguration"]=> string(4) "" ["ShutterSpeedValue"]=> string(8) "2297/454" ["ApertureValue"]=> string(9) "7983/3509" ["BrightnessValue"]=> string(10) "21377/9329" ["ExposureBiasValue"]=> string(3) "0/1" ["MeteringMode"]=> int(5) ["Flash"]=> int(16) ["FocalLength"]=> string(5) "83/20" ["SubjectLocation"]=> array(4) { [0]=> int(2014) [1]=> int(1511) [2]=> int(2217) [3]=> int(1328) } ["MakerNote"]=> string(888) "Apple iOS" ["SubSecTimeOriginal"]=> string(3) "634" ["SubSecTimeDigitized"]=> string(3) "634" ["FlashPixVersion"]=> string(4) "0100" ["ColorSpace"]=> int(1) ["ExifImageWidth"]=> int(4032) ["ExifImageLength"]=> int(3024) ["SensingMethod"]=> int(2) ["SceneType"]=> string(1) "" ["ExposureMode"]=> int(0) ["WhiteBalance"]=> int(0) ["DigitalZoomRatio"]=> string(7) "756/671" ["FocalLengthIn35mmFilm"]=> int(33) ["SceneCaptureType"]=> int(0) ["UndefinedTag:0xA432"]=> array(4) { [0]=> string(5) "83/20" [1]=> string(5) "83/20" [2]=> string(4) "11/5" [3]=> string(4) "11/5" } ["UndefinedTag:0xA433"]=> string(5) "Apple" ["UndefinedTag:0xA434"]=> string(34) "iPhone 6s back camera 4.15mm f/2.2" } ["GPS"]=> array(15) { ["GPSLatitudeRef"]=> string(1) "N" ["GPSLatitude"]=> array(3) { [0]=> string(4) "22/1" [1]=> string(4) "32/1" [2]=> string(8) "5852/100" } ["GPSLongitudeRef"]=> string(1) "E" ["GPSLongitude"]=> array(3) { [0]=> string(5) "113/1" [1]=> string(4) "58/1" [2]=> string(8) "5427/100" } ["GPSAltitudeRef"]=> string(1) "*" ["GPSAltitude"]=> string(10) "54710/1473" ["GPSTimeStamp"]=> array(3) { [0]=> string(3) "8/1" [1]=> string(4) "27/1" [2]=> string(8) "5940/100" } ["GPSSpeedRef"]=> string(1) "K" ["GPSSpeed"]=> string(3) "0/1" ["GPSImgDirectionRef"]=> string(1) "T" ["GPSImgDirection"]=> string(9) "28969/153" ["GPSDestBearingRef"]=> string(1) "T" ["GPSDestBearing"]=> string(9) "28969/153" ["GPSDateStamp"]=> string(10) "2017:08:28" ["UndefinedTag:0x001F"]=> string(10) "123893/449" } } |
如果提示以下信息则表示模块没有打开,可能是你配置哪一块没有配置好,重新配置就好:
Fatal error: Call to undefined function exif_read_data() in /data/www/exif/index.php on line 2 |
echo "GET GPS INFO: \n"; $exif = exif_read_data('P70418-104719.jpg',0, true); if($exif===false){ echo 'No header data found!'; }else{ $latitude = $exif['GPS']['GPSLatitude']; //纬度 $longitude = $exif['GPS']['GPSLongitude']; //经度 $GPSLatitudeRef = $exif['GPS']['GPSLatitudeRef']; //南半球 S 北半球 N $GPSLongitudeRef = $exif['GPS']['GPSLongitudeRef']; //东半球 S 西半球 N var_dump($exif['GPS']); } |
其中,经纬度获取的是含有三个元素数组的数组,分别为:度、分、秒,都用分数表示;
在GPS设备中对于坐标单位有不同的显示方法,有度分秒(DMS)和度(DDD)。
例如:南京玄武区板仓街世界之窗创业园的GPS坐标如下:
度分秒(DMS):东经E 118度48分54.152秒 北纬N 32度04分10.461秒
度(DDD):E 118.815033度 N 32.069723度
如何将度(DDD):: 118.815033度换算成度分秒(DMS)东经E 118度48分53.652秒?转换方法是将118.815033整数位不变取118(度),用0.815033*60=48.90198,取整数位48(分),0.90198*60=54.1188再取整数位54(秒),故转化为118度48分54秒.
同样将度分秒(DMS):东经E 118度48分54.152秒 换算成度(DDD)的方法如下:118度48分54.152秒=118+(48/60)+(54.152/3600)=118.81504度
因为计算时小数位保留的原因,导致正反计算存在一定误差,但误差影响不是很大。1秒的误差就是几米的样子。各位GPS玩友可以用上述方法换算成自己需要的单位坐标。
以下只能获取到文件类型信息:
这是使用fileinfo模块的功能
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
var_dump($mimetype);
结果:
string(10) "image/jpeg"