PageRenderTime 73ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/anqh/vendor/exif/makers/gps.php

https://github.com/anqh/anqh
PHP | 218 lines | 142 code | 34 blank | 42 comment | 59 complexity | a3c92bc709bb11135b767559a6366a21 MD5 | raw file
  1. <?php //================================================================================================
  2. //================================================================================================
  3. //================================================================================================
  4. /*
  5. Exifer
  6. Extracts EXIF information from digital photos.
  7. Copyright © 2003 Jake Olefsky
  8. http://www.offsky.com/software/exif/index.php
  9. jake@olefsky.com
  10. Please see exif.php for the complete information about this software.
  11. ------------
  12. This program is free software; you can redistribute it and/or modify it under the terms of
  13. the GNU General Public License as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  16. without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. See the GNU General Public License for more details. http://www.gnu.org/copyleft/gpl.html
  18. */
  19. //================================================================================================
  20. //================================================================================================
  21. //================================================================================================
  22. //=================
  23. // Looks up the name of the tag
  24. //====================================================================
  25. function lookup_GPS_tag($tag) {
  26. switch($tag) {
  27. case "0000": $tag = "Version";break;
  28. case "0001": $tag = "Latitude Reference";break; //north or south
  29. case "0002": $tag = "Latitude";break; //dd mm.mm or dd mm ss
  30. case "0003": $tag = "Longitude Reference";break; //east or west
  31. case "0004": $tag = "Longitude";break; //dd mm.mm or dd mm ss
  32. case "0005": $tag = "Altitude Reference";break; //sea level or below sea level
  33. case "0006": $tag = "Altitude";break; //positive rational number
  34. case "0007": $tag = "Time";break; //three positive rational numbers
  35. case "0008": $tag = "Satellite";break; //text string up to 999 bytes long
  36. case "0009": $tag = "ReceiveStatus";break; //in progress or interop
  37. case "000a": $tag = "MeasurementMode";break; //2D or 3D
  38. case "000b": $tag = "MeasurementPrecision";break; //positive rational number
  39. case "000c": $tag = "SpeedUnit";break; //KPH, MPH, knots
  40. case "000d": $tag = "ReceiverSpeed";break; //positive rational number
  41. case "000e": $tag = "MovementDirectionRef";break; //true or magnetic north
  42. case "000f": $tag = "MovementDirection";break; //positive rational number
  43. case "0010": $tag = "ImageDirectionRef";break; //true or magnetic north
  44. case "0011": $tag = "ImageDirection";break; //positive rational number
  45. case "0012": $tag = "GeodeticSurveyData";break; //text string up to 999 bytes long
  46. case "0013": $tag = "DestLatitudeRef";break; //north or south
  47. case "0014": $tag = "DestinationLatitude";break; //three positive rational numbers
  48. case "0015": $tag = "DestLongitudeRef";break; //east or west
  49. case "0016": $tag = "DestinationLongitude";break; //three positive rational numbers
  50. case "0017": $tag = "DestBearingRef";break; //true or magnetic north
  51. case "0018": $tag = "DestinationBearing";break; //positive rational number
  52. case "0019": $tag = "DestDistanceRef";break; //km, miles, knots
  53. case "001a": $tag = "DestinationDistance";break; //positive rational number
  54. case "001b": $tag = "ProcessingMethod";break;
  55. case "001c": $tag = "AreaInformation";break;
  56. case "001d": $tag = "Datestamp";break; //text string 10 bytes long
  57. case "001e": $tag = "DifferentialCorrection";break; //integer in range 0-65535
  58. default: $tag = "unknown:".$tag;break;
  59. }
  60. return $tag;
  61. }
  62. //=================
  63. // Formats Data for the data type
  64. //====================================================================
  65. function formatGPSData($type,$tag,$intel,$data) {
  66. if($type=="ASCII") {
  67. if($tag=="0001" || $tag=="0003"){ // Latitude Reference, Longitude Reference
  68. $data = ($data{1} == $data{2} && $data{1} == $data{3}) ? $data{0} : $data;
  69. }
  70. } else if($type=="URATIONAL" || $type=="SRATIONAL") {
  71. if($tag=="0002" || $tag=="0004" || $tag=='0007') { //Latitude, Longitude, Time
  72. $datum = array();
  73. for ($i=0;$i<strlen($data);$i=$i+8) {
  74. array_push($datum,substr($data, $i, 8));
  75. }
  76. $hour = unRational($datum[0],$type,$intel);
  77. $minutes = unRational($datum[1],$type,$intel);
  78. $seconds = unRational($datum[2],$type,$intel);
  79. if($tag=="0007") { //Time
  80. $data = $hour.":".$minutes.":".$seconds;
  81. } else {
  82. $data = $hour+$minutes/60+$seconds/3600;
  83. }
  84. } else {
  85. $data = unRational($data,$type,$intel);
  86. if($tag=="0006"){
  87. $data .= 'm';
  88. }
  89. }
  90. } else if($type=="USHORT" || $type=="SSHORT" || $type=="ULONG" || $type=="SLONG" || $type=="FLOAT" || $type=="DOUBLE") {
  91. $data = rational($data,$type,$intel);
  92. } else if($type=="UNDEFINED") {
  93. } else if($type=="UBYTE") {
  94. $data = bin2hex($data);
  95. if($intel==1) $num = intel2Moto($data);
  96. if($tag=="0000") { // VersionID
  97. $data = hexdec(substr($data,0,2)) .
  98. ".". hexdec(substr($data,2,2)) .
  99. ".". hexdec(substr($data,4,2)) .
  100. ".". hexdec(substr($data,6,2));
  101. } else if($tag=="0005"){ // Altitude Reference
  102. if($data == "00000000"){ $data = '+'; }
  103. else if($data == "01000000"){ $data = '-'; }
  104. }
  105. } else {
  106. $data = bin2hex($data);
  107. if($intel==1) $data = intel2Moto($data);
  108. }
  109. return $data;
  110. }
  111. //=================
  112. // GPS Special data section
  113. // Useful websites
  114. // http://drewnoakes.com/code/exif/sampleOutput.html
  115. // http://www.geosnapper.com
  116. //====================================================================
  117. function parseGPS($block,&$result,$offset,$seek, $globalOffset) {
  118. if($result['Endien']=="Intel") $intel=1;
  119. else $intel=0;
  120. $v = fseek($seek,$globalOffset+$offset); //offsets are from TIFF header which is 12 bytes from the start of the file
  121. if($v==-1) {
  122. $result['Errors'] = $result['Errors']++;
  123. }
  124. $num = bin2hex(fread( $seek, 2 ));
  125. if($intel==1) $num = intel2Moto($num);
  126. $num=hexdec($num);
  127. $result['GPS']['NumTags'] = $num;
  128. if ($num == 0) {
  129. return;
  130. }
  131. $block = fread( $seek, $num*12 );
  132. $place = 0;
  133. //loop thru all tags Each field is 12 bytes
  134. for($i=0;$i<$num;$i++) {
  135. //2 byte tag
  136. $tag = bin2hex(substr($block,$place,2));$place+=2;
  137. if($intel==1) $tag = intel2Moto($tag);
  138. $tag_name = lookup_GPS_tag($tag);
  139. //2 byte datatype
  140. $type = bin2hex(substr($block,$place,2));$place+=2;
  141. if($intel==1) $type = intel2Moto($type);
  142. lookup_type($type,$size);
  143. //4 byte number of elements
  144. $count = bin2hex(substr($block,$place,4));$place+=4;
  145. if($intel==1) $count = intel2Moto($count);
  146. $bytesofdata = $size*hexdec($count);
  147. //4 byte value or pointer to value if larger than 4 bytes
  148. $value = substr($block,$place,4);$place+=4;
  149. if($bytesofdata<=4) {
  150. $data = $value;
  151. } else {
  152. if (strpos('unknown',$tag_name) !== false || $bytesofdata > 1024) {
  153. $result['Errors'] = $result['Errors']++;
  154. $data = '';
  155. $type = 'ASCII';
  156. } else {
  157. $value = bin2hex($value);
  158. if($intel==1) $value = intel2Moto($value);
  159. $v = fseek($seek,$globalOffset+hexdec($value)); //offsets are from TIFF header which is 12 bytes from the start of the file
  160. if($v==0) {
  161. $data = fread($seek, $bytesofdata);
  162. } else {
  163. $result['Errors'] = $result['Errors']++;
  164. $data = '';
  165. $type = 'ASCII';
  166. }
  167. }
  168. }
  169. if($result['VerboseOutput']==1) {
  170. $result['GPS'][$tag_name] = formatGPSData($type,$tag,$intel,$data);
  171. $result['GPS'][$tag_name."_Verbose"]['RawData'] = bin2hex($data);
  172. $result['GPS'][$tag_name."_Verbose"]['Type'] = $type;
  173. $result['GPS'][$tag_name."_Verbose"]['Bytes'] = $bytesofdata;
  174. } else {
  175. $result['GPS'][$tag_name] = formatGPSData($type,$tag,$intel,$data);
  176. }
  177. }
  178. }
  179. ?>