PHP: Geotags aus Bildern auslesen und in Google Maps anzeigen

Geschrieben am 4. April 2010

Auf meiner Bilderseite (http://wishpictures.net) werden nun auch Geotags ausgelesen, und eine entsprechende Karte angezeigt. Vorrausgesetzt im Bild sind überhaupt Geotags gespeichert.
Falls du dich jetzt fragst was Geotags sind, es handelt sich dabei um GPS Koordinaten die in Bildern Gespeichert werden. die geschieht entweder mit Kameras die ein GPS Modul eingebaut haben, oder hinterher per Software. Da ich bereits anfragen erhalten habe wie das auslesen denn Funktioniert habe ich 4 kleine Funktionen geschrieben die ihr nutzen könnt.

Wer nicht nur die Zahlen auf seiner Seite ausgeben möchte sondern auch eine Karte von Google Maps benötigt noch etwas JavaScript und ein API Key. Den API Key bekommt ihr Kostenlos auf http://code.google.com/intl/de-DE/apis/maps/signup.html

Der Javascript Code muss in den Head Bereich, in den Body Tag. außerdem wird ein wenig HTML im Body Bereich benötigt.

Head:
Es muss eine Datei vom Google Server geladen werden. hier mus der API Key mit angegeben werden.
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=DEIN_API_KEY&amp;sensor=false" type="text/javascript"></script>
zudem noch nachfolgendes, bei den die Latidude und die Longitude angegeben werden müssen
<script type="text/javascript">
//<![CDATA[
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('map'));
map.setCenter(new GLatLng(LATIDUDE_DEZ,LONGITUDE_DEZ), 18, G_HYBRID_MAP);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new GScaleControl());
map.addControl(new GOverviewMapControl());
//var infoTabs = [
//new GInfoWindowTab('Info', '<font color=\'#000000\'>Die Aufnahme wurde an dieser Position aufgenommen</font>'),
//new GInfoWindowTab('Koordinaten','<font color=\'#000000\'>Breitengrad: LATIDUDE_DEZ<br>Längengrad: LONGITUDE_DEZ</font>')
//];
var marker = new GMarker(map.getCenter());
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowTabsHtml(infoTabs);
});
map.addOverlay(marker);
marker.openInfoWindowTabsHtml(infoTabs);
}
}
//]]>
</script>

in den Body Tag muss folgendes:
onload="initialize()" onunload="GUnload()"
An der Stelle an der dann die Karte erscheinen soll muss ein DIV belibiger größe mit der ID "map"
<div id='map' style='width: 100%; height: 400px'></div>

Nachfolgende der Code für die PHP Funktionen. Ihr dürft ihn uneingeschränkt nutzen, allerdings würde ich mich über ein Danke Freuen :)
$file ist der Pfad zu eurem Bild

  1. $exif_data = exif_read_data($file, 0, true);
  2. function GPSLongitude($exif_data) {
  3. if (isset($exif_data["GPS"]["GPSLongitudeRef"])) {
  4. $GPSLongitudeRef = $exif_data["GPS"]["GPSLongitudeRef"];
  5. if ($GPSLongitudeRef == "E") {
  6. $GPSLongfaktor = 1;
  7. } else {
  8. $GPSLongfaktor = -1;
  9. }
  10. } else {
  11. return False;
  12. }
  13. if (isset($exif_data["GPS"]["GPSLongitude"])) {
  14. $GPSLongitude = $exif_data["GPS"]["GPSLongitude"];
  15. $GPSLongitude_h = explode("/", $GPSLongitude[0]);
  16. $GPSLongitude_m = explode("/", $GPSLongitude[1]);
  17. $GPSLongitude_s = explode("/", $GPSLongitude[2]);
  18. $GPSLong_h = $GPSLongitude_h[0] / $GPSLongitude_h[1];
  19. $GPSLong_m = $GPSLongitude_m[0] / $GPSLongitude_m[1];
  20. $GPSLong_s = $GPSLongitude_s[0] / $GPSLongitude_s[1];
  21. $GPSLongitude = $GPSLongitudeRef . " " . $GPSLong_h . "° " . $GPSLong_m . "' " . $GPSLong_s;
  22. return $GPSLongitude;
  23. } else {
  24. return False;
  25. }
  26. }
  27.  
  28. function GPSLatitude($exif_data) {
  29. if (isset($exif_data["GPS"]["GPSLatitudeRef"])) {
  30. $GPSLatitudeRef = $exif_data["GPS"]["GPSLatitudeRef"];
  31. if ($GPSLatitudeRef == "N") {
  32. $GPSLatfaktor = 1;
  33. } else {
  34. $GPSLatfaktor = -1;
  35. }
  36. } else {
  37. return False;
  38. }
  39. if (isset($exif_data["GPS"]["GPSLongitude"])) {
  40. $GPSLatitude = $exif_data["GPS"]["GPSLatitude"];
  41. $GPSLatitude_h = explode("/", $GPSLatitude[0]);
  42. $GPSLatitude_m = explode("/", $GPSLatitude[1]);
  43. $GPSLatitude_s = explode("/", $GPSLatitude[2]);
  44. $GPSLat_h = $GPSLatitude_h[0] / $GPSLatitude_h[1];
  45. $GPSLat_m = $GPSLatitude_m[0] / $GPSLatitude_m[1];
  46. $GPSLat_s = $GPSLatitude_s[0] / $GPSLatitude_s[1];
  47. $GPSLatitude = $GPSLatitudeRef . " " . $GPSLat_h . "° " . $GPSLat_m . "' " . $GPSLat_s;
  48. return $GPSLatitude;
  49. } else {
  50. return False;
  51. }
  52. }
  53.  
  54. function GPSLongitudeDezi($exif_data) {
  55. if (isset($exif_data["GPS"]["GPSLongitudeRef"])) {
  56. $GPSLongitudeRef = $exif_data["GPS"]["GPSLongitudeRef"];
  57. if ($GPSLongitudeRef == "E") {
  58. $GPSLongfaktor = 1;
  59. } else {
  60. $GPSLongfaktor = -1;
  61. }
  62. } else {
  63. return False;
  64. }
  65. if (isset($exif_data["GPS"]["GPSLongitude"])) {
  66. $GPSLongitude = $exif_data["GPS"]["GPSLongitude"];
  67. $GPSLongitude_h = explode("/", $GPSLongitude[0]);
  68. $GPSLongitude_m = explode("/", $GPSLongitude[1]);
  69. $GPSLongitude_s = explode("/", $GPSLongitude[2]);
  70. $GPSLong_h = $GPSLongitude_h[0] / $GPSLongitude_h[1];
  71. $GPSLong_m = $GPSLongitude_m[0] / $GPSLongitude_m[1];
  72. $GPSLong_s = $GPSLongitude_s[0] / $GPSLongitude_s[1];
  73. $GPSLongGrad = $GPSLongfaktor * ($GPSLong_h + ($GPSLong_m + ($GPSLong_s / 60)) / 60);
  74. return $GPSLongGrad;
  75. } else {
  76. return False;
  77. }
  78. }
  79.  
  80. function GPSLatitudeDezi($exif_data) {
  81. if (isset($exif_data["GPS"]["GPSLatitudeRef"])) {
  82. $GPSLatitudeRef = $exif_data["GPS"]["GPSLatitudeRef"];
  83. if ($GPSLatitudeRef == "N") {
  84. $GPSLatfaktor = 1;
  85. } else {
  86. $GPSLatfaktor = -1;
  87. }
  88. } else {
  89. return False;
  90. }
  91. if (isset($exif_data["GPS"]["GPSLongitude"])) {
  92. $GPSLatitude = $exif_data["GPS"]["GPSLatitude"];
  93. $GPSLatitude_h = explode("/", $GPSLatitude[0]);
  94. $GPSLatitude_m = explode("/", $GPSLatitude[1]);
  95. $GPSLatitude_s = explode("/", $GPSLatitude[2]);
  96. $GPSLat_h = $GPSLatitude_h[0] / $GPSLatitude_h[1];
  97. $GPSLat_m = $GPSLatitude_m[0] / $GPSLatitude_m[1];
  98. $GPSLat_s = $GPSLatitude_s[0] / $GPSLatitude_s[1];
  99. $GPSLatGrad = $GPSLatfaktor * ($GPSLat_h + ($GPSLat_m + ($GPSLat_s / 60)) / 60);
  100. return $GPSLatGrad;
  101. } else {
  102. return False;
  103. }
  104. }