module.graphic.jpg.php 13 KB

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