如何从JPEG EXIF数据中检索地理定位信息?

时间:2016-05-11 16:06:02

标签: php geolocation gd exif

我正在使用php gd库进行图像处理。刚才上帝出现并告诉我,我们可以从jpeg,tiff images中检索exif数据。但是,他没有告诉我怎么做!

我试着浏览一下,发现一些关于检索数据的帖子。在我尝试获取地理位置数据之前,地球上的一切都很好。我无法找到任何解决方案来获取这些数据。

1 个答案:

答案 0 :(得分:6)

我在评论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
}

这应该会给你一个良好的开端。