PHP获取照片exif信息

在这个没图说个屁的年代,照片还是很重要的。如果照片上传后会自动加上 照片相关信息,那用户体验的确会好很多,本着这个想法,今天尝试了下 PHP获取照片exif信息,重要的是 获取图片的GPS信息,如果再加上 google maps API 的话,就可以轻松得到位置信息


<?php
if(function_exists('exif_read_data'))
{
 $exif=exif_read_data('./test/test1.jpg');  //图片地址
 //var_dump($exif);

 //echo $data['timeline']=$exif['DateTimeOriginal'];
 $data['size']=$size=$exif['FileSize']/1024;
 if($size>1024)
 {
 //单位为MB
 //echo ceil($size/1024).'MB<br>';
 }else{
 //单位为KB

//echo ceil($size).'KB<br>';
 }

 //设备信息
 $data['device']=$exif['Make'].''.$exif['Model'];

 if($exif['GPSLatitudeRef']=='N'&&$exif['GPSLongitudeRef']=='E')
 {
 //地图坐标信息

 //纬度
 $data['latitude'] = getGps($exif['GPSLatitude'],$exif['GPSLongitudeRef']);
 //经度
 $data['longitude'] = getGps($exif['GPSLongitude'],$exif['GPSLongitudeRef']);

 }

 //eval('echo 1+3;');
 var_dump($data);

 echo 'http://ditu.google.cn/maps?q=('.$data['latitude'].','.$data['longitude'].')';

}
/*

取得EXIF的內容

$exif = exif_read_data('my.jpg', 0, true);

$latitude = $exif['GPS']['GPSLatitude']; //经度

$longitude = $exif['GPS']['GPSLongitude']; //纬度

都是各有三個个元素的array 各代表度,分,秒

把上面2个值分别调用如下方法,就可以得到gps经纬度了

 $banqiu = $exif['GPS'][GPSLatitudeRef]
 或者
 $banqiu = $exif['GPS'][GPSLongitudeRef]
 */
function getGps($exifCoord,$banqiu)

{

$degrees= count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0;

$minutes= count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0;

$seconds= count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0;


//normalize

$minutes+= 60 * ($degrees- floor($degrees));

$degrees= floor($degrees);
 $seconds+= 60 * ($minutes- floor($minutes));

$minutes= floor($minutes);
 //extra normalization, probably not necessary unless you get weird data

if($seconds>= 60)

{

$minutes+= floor($seconds/60.0);

$seconds-= 60*floor($seconds/60.0);

}
 if($minutes>= 60)

{

$degrees+= floor($minutes/60.0);

$minutes-= 60*floor($minutes/60.0);

}
 $lng_lat = $degrees + $minutes/60 + $seconds/60/60;
 if(strtoupper($banqiu) == 'W' || strtoupper($banqiu) == 'S'){
 //如果是南半球 或者 西半球 乘以-1
 $lng_lat = $lng_lat * -1;
 }
 return $lng_lat;
 //return array('degrees'=> $degrees, 'minutes'=> $minutes, 'seconds'=> $seconds);
}
/*
取得EXIF的內容
分数 转 小数
*/
function gps2Num($coordPart)

{

$parts= explode('/', $coordPart);
 if(count($parts) <= 0)

return 0;

if(count($parts) == 1)

return $parts[0];
 return floatval($parts[0]) / floatval($parts[1]);

}