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