PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wimpy.getid3/module.graphic.jpg.php

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