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


How to retrive geolocation information from JPEG EXIF data?

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

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

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

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