如何从 JPEG EXIF 数据中检索地理位置信息?
How to retrive geolocation information from JPEG EXIF data?
我正在使用 php gd 库进行图像处理。刚才大神出现告诉我可以从jpeg,tiff中提取exif数据images.But,他没告诉我怎么做!
我试着浏览了一下,发现了一些关于检索数据的帖子。在我尝试获取地理位置数据之前,地球上一切都很好。我找不到任何解决方案来获取该数据。
我在评论里提到了exif_read_data。既然我坐在办公桌前,我可以详细说明一下。我前段时间创建了一个函数来做这个:
// get geo-data from image
function get_image_location($file) {
if (is_file($file)) {
$info = exif_read_data($file);
if ($info !== false) {
$direction = array('N', 'S', 'E', 'W');
if (isset($info['GPSLatitude'], $info['GPSLongitude'], $info['GPSLatitudeRef'], $info['GPSLongitudeRef']) &&
in_array($info['GPSLatitudeRef'], $direction) && in_array($info['GPSLongitudeRef'], $direction)) {
$lat_degrees_a = explode('/',$info['GPSLatitude'][0]);
$lat_minutes_a = explode('/',$info['GPSLatitude'][1]);
$lat_seconds_a = explode('/',$info['GPSLatitude'][2]);
$lng_degrees_a = explode('/',$info['GPSLongitude'][0]);
$lng_minutes_a = explode('/',$info['GPSLongitude'][1]);
$lng_seconds_a = explode('/',$info['GPSLongitude'][2]);
$lat_degrees = $lat_degrees_a[0] / $lat_degrees_a[1];
$lat_minutes = $lat_minutes_a[0] / $lat_minutes_a[1];
$lat_seconds = $lat_seconds_a[0] / $lat_seconds_a[1];
$lng_degrees = $lng_degrees_a[0] / $lng_degrees_a[1];
$lng_minutes = $lng_minutes_a[0] / $lng_minutes_a[1];
$lng_seconds = $lng_seconds_a[0] / $lng_seconds_a[1];
$lat = (float) $lat_degrees + ((($lat_minutes * 60) + ($lat_seconds)) / 3600);
$lng = (float) $lng_degrees + ((($lng_minutes * 60) + ($lng_seconds)) / 3600);
$lat = number_format($lat, 7);
$lng = number_format($lng, 7);
//If the latitude is South, make it negative.
//If the longitude is west, make it negative
$lat = $info['GPSLatitudeRef'] == 'S' ? $lat * -1 : $lat;
$lng = $info['GPSLongitudeRef'] == 'W' ? $lng * -1 : $lng;
return array(
'lat' => $lat,
'lng' => $lng
);
}
}
}
return false;
}
此函数用于文件上传,例如:
if (($geo = get_image_location($_FILES['file']['tmp_name'])) && !empty($geo)) {
// upload file
} else {
// file does not appear to contain any location information
}
这应该会给你一个好的开始。
我正在使用 php gd 库进行图像处理。刚才大神出现告诉我可以从jpeg,tiff中提取exif数据images.But,他没告诉我怎么做!
我试着浏览了一下,发现了一些关于检索数据的帖子。在我尝试获取地理位置数据之前,地球上一切都很好。我找不到任何解决方案来获取该数据。
我在评论里提到了exif_read_data。既然我坐在办公桌前,我可以详细说明一下。我前段时间创建了一个函数来做这个:
// get geo-data from image
function get_image_location($file) {
if (is_file($file)) {
$info = exif_read_data($file);
if ($info !== false) {
$direction = array('N', 'S', 'E', 'W');
if (isset($info['GPSLatitude'], $info['GPSLongitude'], $info['GPSLatitudeRef'], $info['GPSLongitudeRef']) &&
in_array($info['GPSLatitudeRef'], $direction) && in_array($info['GPSLongitudeRef'], $direction)) {
$lat_degrees_a = explode('/',$info['GPSLatitude'][0]);
$lat_minutes_a = explode('/',$info['GPSLatitude'][1]);
$lat_seconds_a = explode('/',$info['GPSLatitude'][2]);
$lng_degrees_a = explode('/',$info['GPSLongitude'][0]);
$lng_minutes_a = explode('/',$info['GPSLongitude'][1]);
$lng_seconds_a = explode('/',$info['GPSLongitude'][2]);
$lat_degrees = $lat_degrees_a[0] / $lat_degrees_a[1];
$lat_minutes = $lat_minutes_a[0] / $lat_minutes_a[1];
$lat_seconds = $lat_seconds_a[0] / $lat_seconds_a[1];
$lng_degrees = $lng_degrees_a[0] / $lng_degrees_a[1];
$lng_minutes = $lng_minutes_a[0] / $lng_minutes_a[1];
$lng_seconds = $lng_seconds_a[0] / $lng_seconds_a[1];
$lat = (float) $lat_degrees + ((($lat_minutes * 60) + ($lat_seconds)) / 3600);
$lng = (float) $lng_degrees + ((($lng_minutes * 60) + ($lng_seconds)) / 3600);
$lat = number_format($lat, 7);
$lng = number_format($lng, 7);
//If the latitude is South, make it negative.
//If the longitude is west, make it negative
$lat = $info['GPSLatitudeRef'] == 'S' ? $lat * -1 : $lat;
$lng = $info['GPSLongitudeRef'] == 'W' ? $lng * -1 : $lng;
return array(
'lat' => $lat,
'lng' => $lng
);
}
}
}
return false;
}
此函数用于文件上传,例如:
if (($geo = get_image_location($_FILES['file']['tmp_name'])) && !empty($geo)) {
// upload file
} else {
// file does not appear to contain any location information
}
这应该会给你一个好的开始。