PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/getid3/module.graphic.jpg.php

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