PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/includes/js/mootools-filemanager/Assets/Connector/Assets/getid3/module.graphic.jpg.php

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