exif.inc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. $plugin = array(
  3. 'title' => t("Image/exif"),
  4. 'description' => t('Get a location from an image that was taken with a GPS enabled phone or camera'),
  5. 'callback' => 'geocoder_exif',
  6. 'field_types' => array('file', 'image'),
  7. 'field_callback' => 'geocoder_exif_field',
  8. );
  9. function geocoder_exif($url, $options = array()) {
  10. // Invoke hook_file_download to check permissions
  11. // We are essentially duplicating the logic found in file.inc file_download()
  12. foreach (module_implements('file_download') as $module) {
  13. $function = $module . '_file_download';
  14. $result = $function($url);
  15. if ($result == -1) {
  16. drupal_set_message(t('You do not have permission to access this file'), 'error');
  17. return FALSE;
  18. }
  19. }
  20. // The user has permission to access the file. Geocode it.
  21. geophp_load();
  22. if ($data = exif_read_data($url)) {
  23. if (!isset($data['GPSLatitudeRef'])) return FALSE;
  24. $lat = geocoder_exif_from($data['GPSLatitudeRef'], $data['GPSLatitude']);
  25. $lon = geocoder_exif_from($data['GPSLongitudeRef'], $data['GPSLongitude']);
  26. $point = new Point($lon, $lat);
  27. return $point;
  28. }
  29. else return FALSE;
  30. }
  31. function geocoder_exif_field($field, $field_item) {
  32. if ($field_item['fid']) {
  33. $file = file_load($field_item['fid']);
  34. return geocoder_exif($file->uri);
  35. }
  36. }
  37. function geocoder_exif_from($dir, $data) {
  38. foreach ($data as $k => $item) {
  39. list($deg, $pct) = explode('/', $item);
  40. if ($pct) $data[$k] = $deg / $pct;
  41. }
  42. $point = (float) $data[0] + ($data[1] / 60) + ($data[2] / 3600);
  43. if (in_array($dir, array('S', 'W'))) $point = $point * -1;
  44. return $point;
  45. }