module.graphic.jpg.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // See readme.txt for more details //
  9. /////////////////////////////////////////////////////////////////
  10. // //
  11. // module.graphic.jpg.php //
  12. // module for analyzing JPEG Image files //
  13. // dependencies: PHP compiled with --enable-exif (optional) //
  14. // module.tag.xmp.php (optional) //
  15. // ///
  16. /////////////////////////////////////////////////////////////////
  17. class getid3_jpg extends getid3_handler
  18. {
  19. public function Analyze() {
  20. $info = &$this->getid3->info;
  21. $info['fileformat'] = 'jpg';
  22. $info['video']['dataformat'] = 'jpg';
  23. $info['video']['lossless'] = false;
  24. $info['video']['bits_per_sample'] = 24;
  25. $info['video']['pixel_aspect_ratio'] = (float) 1;
  26. $this->fseek($info['avdataoffset']);
  27. $imageinfo = array();
  28. //list($width, $height, $type) = getid3_lib::GetDataImageSize($this->fread($info['filesize']), $imageinfo);
  29. list($width, $height, $type) = getimagesize($info['filenamepath'], $imageinfo); // http://www.getid3.org/phpBB3/viewtopic.php?t=1474
  30. if (isset($imageinfo['APP13'])) {
  31. // http://php.net/iptcparse
  32. // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html
  33. $iptc_parsed = iptcparse($imageinfo['APP13']);
  34. if (is_array($iptc_parsed)) {
  35. foreach ($iptc_parsed as $iptc_key_raw => $iptc_values) {
  36. list($iptc_record, $iptc_tagkey) = explode('#', $iptc_key_raw);
  37. $iptc_tagkey = intval(ltrim($iptc_tagkey, '0'));
  38. foreach ($iptc_values as $key => $value) {
  39. $IPTCrecordName = $this->IPTCrecordName($iptc_record);
  40. $IPTCrecordTagName = $this->IPTCrecordTagName($iptc_record, $iptc_tagkey);
  41. if (isset($info['iptc']['comments'][$IPTCrecordName][$IPTCrecordTagName])) {
  42. $info['iptc']['comments'][$IPTCrecordName][$IPTCrecordTagName][] = $value;
  43. } else {
  44. $info['iptc']['comments'][$IPTCrecordName][$IPTCrecordTagName] = array($value);
  45. }
  46. }
  47. }
  48. }
  49. }
  50. $returnOK = false;
  51. switch ($type) {
  52. case IMG_JPG:
  53. $info['video']['resolution_x'] = $width;
  54. $info['video']['resolution_y'] = $height;
  55. if (isset($imageinfo['APP1'])) {
  56. if (function_exists('exif_read_data')) {
  57. if (substr($imageinfo['APP1'], 0, 4) == 'Exif') {
  58. //$info['warning'][] = 'known issue: https://bugs.php.net/bug.php?id=62523';
  59. //return false;
  60. $info['jpg']['exif'] = exif_read_data($info['filenamepath'], null, true, false);
  61. } else {
  62. $info['warning'][] = 'exif_read_data() cannot parse non-EXIF data in APP1 (expected "Exif", found "'.substr($imageinfo['APP1'], 0, 4).'")';
  63. }
  64. } else {
  65. $info['warning'][] = 'EXIF parsing only available when '.(GETID3_OS_ISWINDOWS ? 'php_exif.dll enabled' : 'compiled with --enable-exif');
  66. }
  67. }
  68. $returnOK = true;
  69. break;
  70. default:
  71. break;
  72. }
  73. $cast_as_appropriate_keys = array('EXIF', 'IFD0', 'THUMBNAIL');
  74. foreach ($cast_as_appropriate_keys as $exif_key) {
  75. if (isset($info['jpg']['exif'][$exif_key])) {
  76. foreach ($info['jpg']['exif'][$exif_key] as $key => $value) {
  77. $info['jpg']['exif'][$exif_key][$key] = $this->CastAsAppropriate($value);
  78. }
  79. }
  80. }
  81. if (isset($info['jpg']['exif']['GPS'])) {
  82. if (isset($info['jpg']['exif']['GPS']['GPSVersion'])) {
  83. for ($i = 0; $i < 4; $i++) {
  84. $version_subparts[$i] = ord(substr($info['jpg']['exif']['GPS']['GPSVersion'], $i, 1));
  85. }
  86. $info['jpg']['exif']['GPS']['computed']['version'] = 'v'.implode('.', $version_subparts);
  87. }
  88. if (isset($info['jpg']['exif']['GPS']['GPSDateStamp'])) {
  89. $explodedGPSDateStamp = explode(':', $info['jpg']['exif']['GPS']['GPSDateStamp']);
  90. $computed_time[5] = (isset($explodedGPSDateStamp[0]) ? $explodedGPSDateStamp[0] : '');
  91. $computed_time[3] = (isset($explodedGPSDateStamp[1]) ? $explodedGPSDateStamp[1] : '');
  92. $computed_time[4] = (isset($explodedGPSDateStamp[2]) ? $explodedGPSDateStamp[2] : '');
  93. $computed_time = array(0=>0, 1=>0, 2=>0, 3=>0, 4=>0, 5=>0);
  94. if (isset($info['jpg']['exif']['GPS']['GPSTimeStamp']) && is_array($info['jpg']['exif']['GPS']['GPSTimeStamp'])) {
  95. foreach ($info['jpg']['exif']['GPS']['GPSTimeStamp'] as $key => $value) {
  96. $computed_time[$key] = getid3_lib::DecimalizeFraction($value);
  97. }
  98. }
  99. $info['jpg']['exif']['GPS']['computed']['timestamp'] = gmmktime($computed_time[0], $computed_time[1], $computed_time[2], $computed_time[3], $computed_time[4], $computed_time[5]);
  100. }
  101. if (isset($info['jpg']['exif']['GPS']['GPSLatitude']) && is_array($info['jpg']['exif']['GPS']['GPSLatitude'])) {
  102. $direction_multiplier = ((isset($info['jpg']['exif']['GPS']['GPSLatitudeRef']) && ($info['jpg']['exif']['GPS']['GPSLatitudeRef'] == 'S')) ? -1 : 1);
  103. foreach ($info['jpg']['exif']['GPS']['GPSLatitude'] as $key => $value) {
  104. $computed_latitude[$key] = getid3_lib::DecimalizeFraction($value);
  105. }
  106. $info['jpg']['exif']['GPS']['computed']['latitude'] = $direction_multiplier * ($computed_latitude[0] + ($computed_latitude[1] / 60) + ($computed_latitude[2] / 3600));
  107. }
  108. if (isset($info['jpg']['exif']['GPS']['GPSLongitude']) && is_array($info['jpg']['exif']['GPS']['GPSLongitude'])) {
  109. $direction_multiplier = ((isset($info['jpg']['exif']['GPS']['GPSLongitudeRef']) && ($info['jpg']['exif']['GPS']['GPSLongitudeRef'] == 'W')) ? -1 : 1);
  110. foreach ($info['jpg']['exif']['GPS']['GPSLongitude'] as $key => $value) {
  111. $computed_longitude[$key] = getid3_lib::DecimalizeFraction($value);
  112. }
  113. $info['jpg']['exif']['GPS']['computed']['longitude'] = $direction_multiplier * ($computed_longitude[0] + ($computed_longitude[1] / 60) + ($computed_longitude[2] / 3600));
  114. }
  115. if (isset($info['jpg']['exif']['GPS']['GPSAltitudeRef'])) {
  116. $info['jpg']['exif']['GPS']['GPSAltitudeRef'] = ord($info['jpg']['exif']['GPS']['GPSAltitudeRef']); // 0 = above sea level; 1 = below sea level
  117. }
  118. if (isset($info['jpg']['exif']['GPS']['GPSAltitude'])) {
  119. $direction_multiplier = (!empty($info['jpg']['exif']['GPS']['GPSAltitudeRef']) ? -1 : 1); // 0 = above sea level; 1 = below sea level
  120. $info['jpg']['exif']['GPS']['computed']['altitude'] = $direction_multiplier * getid3_lib::DecimalizeFraction($info['jpg']['exif']['GPS']['GPSAltitude']);
  121. }
  122. }
  123. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.xmp.php', __FILE__, true);
  124. if (isset($info['filenamepath'])) {
  125. $image_xmp = new Image_XMP($info['filenamepath']);
  126. $xmp_raw = $image_xmp->getAllTags();
  127. foreach ($xmp_raw as $key => $value) {
  128. if (strpos($key, ':')) {
  129. list($subsection, $tagname) = explode(':', $key);
  130. $info['xmp'][$subsection][$tagname] = $this->CastAsAppropriate($value);
  131. } else {
  132. $info['warning'][] = 'XMP: expecting "<subsection>:<tagname>", found "'.$key.'"';
  133. }
  134. }
  135. }
  136. if (!$returnOK) {
  137. unset($info['fileformat']);
  138. return false;
  139. }
  140. return true;
  141. }
  142. public function CastAsAppropriate($value) {
  143. if (is_array($value)) {
  144. return $value;
  145. } elseif (preg_match('#^[0-9]+/[0-9]+$#', $value)) {
  146. return getid3_lib::DecimalizeFraction($value);
  147. } elseif (preg_match('#^[0-9]+$#', $value)) {
  148. return getid3_lib::CastAsInt($value);
  149. } elseif (preg_match('#^[0-9\.]+$#', $value)) {
  150. return (float) $value;
  151. }
  152. return $value;
  153. }
  154. public function IPTCrecordName($iptc_record) {
  155. // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html
  156. static $IPTCrecordName = array();
  157. if (empty($IPTCrecordName)) {
  158. $IPTCrecordName = array(
  159. 1 => 'IPTCEnvelope',
  160. 2 => 'IPTCApplication',
  161. 3 => 'IPTCNewsPhoto',
  162. 7 => 'IPTCPreObjectData',
  163. 8 => 'IPTCObjectData',
  164. 9 => 'IPTCPostObjectData',
  165. );
  166. }
  167. return (isset($IPTCrecordName[$iptc_record]) ? $IPTCrecordName[$iptc_record] : '');
  168. }
  169. public function IPTCrecordTagName($iptc_record, $iptc_tagkey) {
  170. // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html
  171. static $IPTCrecordTagName = array();
  172. if (empty($IPTCrecordTagName)) {
  173. $IPTCrecordTagName = array(
  174. 1 => array( // IPTC EnvelopeRecord Tags
  175. 0 => 'EnvelopeRecordVersion',
  176. 5 => 'Destination',
  177. 20 => 'FileFormat',
  178. 22 => 'FileVersion',
  179. 30 => 'ServiceIdentifier',
  180. 40 => 'EnvelopeNumber',
  181. 50 => 'ProductID',
  182. 60 => 'EnvelopePriority',
  183. 70 => 'DateSent',
  184. 80 => 'TimeSent',
  185. 90 => 'CodedCharacterSet',
  186. 100 => 'UniqueObjectName',
  187. 120 => 'ARMIdentifier',
  188. 122 => 'ARMVersion',
  189. ),
  190. 2 => array( // IPTC ApplicationRecord Tags
  191. 0 => 'ApplicationRecordVersion',
  192. 3 => 'ObjectTypeReference',
  193. 4 => 'ObjectAttributeReference',
  194. 5 => 'ObjectName',
  195. 7 => 'EditStatus',
  196. 8 => 'EditorialUpdate',
  197. 10 => 'Urgency',
  198. 12 => 'SubjectReference',
  199. 15 => 'Category',
  200. 20 => 'SupplementalCategories',
  201. 22 => 'FixtureIdentifier',
  202. 25 => 'Keywords',
  203. 26 => 'ContentLocationCode',
  204. 27 => 'ContentLocationName',
  205. 30 => 'ReleaseDate',
  206. 35 => 'ReleaseTime',
  207. 37 => 'ExpirationDate',
  208. 38 => 'ExpirationTime',
  209. 40 => 'SpecialInstructions',
  210. 42 => 'ActionAdvised',
  211. 45 => 'ReferenceService',
  212. 47 => 'ReferenceDate',
  213. 50 => 'ReferenceNumber',
  214. 55 => 'DateCreated',
  215. 60 => 'TimeCreated',
  216. 62 => 'DigitalCreationDate',
  217. 63 => 'DigitalCreationTime',
  218. 65 => 'OriginatingProgram',
  219. 70 => 'ProgramVersion',
  220. 75 => 'ObjectCycle',
  221. 80 => 'By-line',
  222. 85 => 'By-lineTitle',
  223. 90 => 'City',
  224. 92 => 'Sub-location',
  225. 95 => 'Province-State',
  226. 100 => 'Country-PrimaryLocationCode',
  227. 101 => 'Country-PrimaryLocationName',
  228. 103 => 'OriginalTransmissionReference',
  229. 105 => 'Headline',
  230. 110 => 'Credit',
  231. 115 => 'Source',
  232. 116 => 'CopyrightNotice',
  233. 118 => 'Contact',
  234. 120 => 'Caption-Abstract',
  235. 121 => 'LocalCaption',
  236. 122 => 'Writer-Editor',
  237. 125 => 'RasterizedCaption',
  238. 130 => 'ImageType',
  239. 131 => 'ImageOrientation',
  240. 135 => 'LanguageIdentifier',
  241. 150 => 'AudioType',
  242. 151 => 'AudioSamplingRate',
  243. 152 => 'AudioSamplingResolution',
  244. 153 => 'AudioDuration',
  245. 154 => 'AudioOutcue',
  246. 184 => 'JobID',
  247. 185 => 'MasterDocumentID',
  248. 186 => 'ShortDocumentID',
  249. 187 => 'UniqueDocumentID',
  250. 188 => 'OwnerID',
  251. 200 => 'ObjectPreviewFileFormat',
  252. 201 => 'ObjectPreviewFileVersion',
  253. 202 => 'ObjectPreviewData',
  254. 221 => 'Prefs',
  255. 225 => 'ClassifyState',
  256. 228 => 'SimilarityIndex',
  257. 230 => 'DocumentNotes',
  258. 231 => 'DocumentHistory',
  259. 232 => 'ExifCameraInfo',
  260. ),
  261. 3 => array( // IPTC NewsPhoto Tags
  262. 0 => 'NewsPhotoVersion',
  263. 10 => 'IPTCPictureNumber',
  264. 20 => 'IPTCImageWidth',
  265. 30 => 'IPTCImageHeight',
  266. 40 => 'IPTCPixelWidth',
  267. 50 => 'IPTCPixelHeight',
  268. 55 => 'SupplementalType',
  269. 60 => 'ColorRepresentation',
  270. 64 => 'InterchangeColorSpace',
  271. 65 => 'ColorSequence',
  272. 66 => 'ICC_Profile',
  273. 70 => 'ColorCalibrationMatrix',
  274. 80 => 'LookupTable',
  275. 84 => 'NumIndexEntries',
  276. 85 => 'ColorPalette',
  277. 86 => 'IPTCBitsPerSample',
  278. 90 => 'SampleStructure',
  279. 100 => 'ScanningDirection',
  280. 102 => 'IPTCImageRotation',
  281. 110 => 'DataCompressionMethod',
  282. 120 => 'QuantizationMethod',
  283. 125 => 'EndPoints',
  284. 130 => 'ExcursionTolerance',
  285. 135 => 'BitsPerComponent',
  286. 140 => 'MaximumDensityRange',
  287. 145 => 'GammaCompensatedValue',
  288. ),
  289. 7 => array( // IPTC PreObjectData Tags
  290. 10 => 'SizeMode',
  291. 20 => 'MaxSubfileSize',
  292. 90 => 'ObjectSizeAnnounced',
  293. 95 => 'MaximumObjectSize',
  294. ),
  295. 8 => array( // IPTC ObjectData Tags
  296. 10 => 'SubFile',
  297. ),
  298. 9 => array( // IPTC PostObjectData Tags
  299. 10 => 'ConfirmedObjectSize',
  300. ),
  301. );
  302. }
  303. return (isset($IPTCrecordTagName[$iptc_record][$iptc_tagkey]) ? $IPTCrecordTagName[$iptc_record][$iptc_tagkey] : $iptc_tagkey);
  304. }
  305. }