3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.graphic.jpg.php //
12 // module for analyzing JPEG Image files //
13 // dependencies: PHP compiled with --enable-exif (optional) //
14 // module.tag.xmp.php (optional) //
16 /////////////////////////////////////////////////////////////////
19 class getid3_jpg
extends getid3_handler
24 public function Analyze() {
25 $info = &$this->getid3
->info
;
27 $info['fileformat'] = 'jpg';
28 $info['video']['dataformat'] = 'jpg';
29 $info['video']['lossless'] = false;
30 $info['video']['bits_per_sample'] = 24;
31 $info['video']['pixel_aspect_ratio'] = (float) 1;
33 $this->fseek($info['avdataoffset']);
36 //list($width, $height, $type) = getid3_lib::GetDataImageSize($this->fread($info['filesize']), $imageinfo);
37 list($width, $height, $type) = getimagesize($info['filenamepath'], $imageinfo); // https://www.getid3.org/phpBB3/viewtopic.php?t=1474
40 if (isset($imageinfo['APP13'])) {
41 // http://php.net/iptcparse
42 // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html
43 $iptc_parsed = iptcparse($imageinfo['APP13']);
44 if (is_array($iptc_parsed)) {
45 foreach ($iptc_parsed as $iptc_key_raw => $iptc_values) {
46 list($iptc_record, $iptc_tagkey) = explode('#', $iptc_key_raw);
47 $iptc_tagkey = intval(ltrim($iptc_tagkey, '0'));
48 foreach ($iptc_values as $key => $value) {
49 $IPTCrecordName = $this->IPTCrecordName($iptc_record);
50 $IPTCrecordTagName = $this->IPTCrecordTagName($iptc_record, $iptc_tagkey);
51 if (isset($info['iptc']['comments'][$IPTCrecordName][$IPTCrecordTagName])) {
52 $info['iptc']['comments'][$IPTCrecordName][$IPTCrecordTagName][] = $value;
54 $info['iptc']['comments'][$IPTCrecordName][$IPTCrecordTagName] = array($value);
64 $info['video']['resolution_x'] = $width;
65 $info['video']['resolution_y'] = $height;
67 if (isset($imageinfo['APP1'])) {
68 if (function_exists('exif_read_data')) {
69 if (substr($imageinfo['APP1'], 0, 4) == 'Exif') {
70 //$this->warning('known issue: https://bugs.php.net/bug.php?id=62523');
72 set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
73 if (!(error_reporting() & $errno)) {
74 // error is not specified in the error_reporting setting, so we ignore it
78 $errcontext['info']['warning'][] = 'Error parsing EXIF data ('.$errstr.')';
81 $info['jpg']['exif'] = exif_read_data($info['filenamepath'], null, true, false);
83 restore_error_handler();
85 $this->warning('exif_read_data() cannot parse non-EXIF data in APP1 (expected "Exif", found "'.substr($imageinfo['APP1'], 0, 4).'")');
88 $this->warning('EXIF parsing only available when '.(GETID3_OS_ISWINDOWS ?
'php_exif.dll enabled' : 'compiled with --enable-exif'));
99 $cast_as_appropriate_keys = array('EXIF', 'IFD0', 'THUMBNAIL');
100 foreach ($cast_as_appropriate_keys as $exif_key) {
101 if (isset($info['jpg']['exif'][$exif_key])) {
102 foreach ($info['jpg']['exif'][$exif_key] as $key => $value) {
103 $info['jpg']['exif'][$exif_key][$key] = $this->CastAsAppropriate($value);
109 if (isset($info['jpg']['exif']['GPS'])) {
111 if (isset($info['jpg']['exif']['GPS']['GPSVersion'])) {
112 $version_subparts = array();
113 for ($i = 0; $i < 4; $i++
) {
114 $version_subparts[$i] = ord(substr($info['jpg']['exif']['GPS']['GPSVersion'], $i, 1));
116 $info['jpg']['exif']['GPS']['computed']['version'] = 'v'.implode('.', $version_subparts);
119 if (isset($info['jpg']['exif']['GPS']['GPSDateStamp'])) {
120 $explodedGPSDateStamp = explode(':', $info['jpg']['exif']['GPS']['GPSDateStamp']);
121 $computed_time[5] = (isset($explodedGPSDateStamp[0]) ?
$explodedGPSDateStamp[0] : '');
122 $computed_time[3] = (isset($explodedGPSDateStamp[1]) ?
$explodedGPSDateStamp[1] : '');
123 $computed_time[4] = (isset($explodedGPSDateStamp[2]) ?
$explodedGPSDateStamp[2] : '');
125 $computed_time = array(0=>0, 1=>0, 2=>0, 3=>0, 4=>0, 5=>0);
126 if (isset($info['jpg']['exif']['GPS']['GPSTimeStamp']) && is_array($info['jpg']['exif']['GPS']['GPSTimeStamp'])) {
127 foreach ($info['jpg']['exif']['GPS']['GPSTimeStamp'] as $key => $value) {
128 $computed_time[$key] = getid3_lib
::DecimalizeFraction($value);
131 $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]);
134 if (isset($info['jpg']['exif']['GPS']['GPSLatitude']) && is_array($info['jpg']['exif']['GPS']['GPSLatitude'])) {
135 $direction_multiplier = ((isset($info['jpg']['exif']['GPS']['GPSLatitudeRef']) && ($info['jpg']['exif']['GPS']['GPSLatitudeRef'] == 'S')) ?
-1 : 1);
136 $computed_latitude = array();
137 foreach ($info['jpg']['exif']['GPS']['GPSLatitude'] as $key => $value) {
138 $computed_latitude[$key] = getid3_lib
::DecimalizeFraction($value);
140 $info['jpg']['exif']['GPS']['computed']['latitude'] = $direction_multiplier * ($computed_latitude[0] +
($computed_latitude[1] / 60) +
($computed_latitude[2] / 3600));
143 if (isset($info['jpg']['exif']['GPS']['GPSLongitude']) && is_array($info['jpg']['exif']['GPS']['GPSLongitude'])) {
144 $direction_multiplier = ((isset($info['jpg']['exif']['GPS']['GPSLongitudeRef']) && ($info['jpg']['exif']['GPS']['GPSLongitudeRef'] == 'W')) ?
-1 : 1);
145 $computed_longitude = array();
146 foreach ($info['jpg']['exif']['GPS']['GPSLongitude'] as $key => $value) {
147 $computed_longitude[$key] = getid3_lib
::DecimalizeFraction($value);
149 $info['jpg']['exif']['GPS']['computed']['longitude'] = $direction_multiplier * ($computed_longitude[0] +
($computed_longitude[1] / 60) +
($computed_longitude[2] / 3600));
151 if (isset($info['jpg']['exif']['GPS']['GPSAltitudeRef'])) {
152 $info['jpg']['exif']['GPS']['GPSAltitudeRef'] = ord($info['jpg']['exif']['GPS']['GPSAltitudeRef']); // 0 = above sea level; 1 = below sea level
154 if (isset($info['jpg']['exif']['GPS']['GPSAltitude'])) {
155 $direction_multiplier = (!empty($info['jpg']['exif']['GPS']['GPSAltitudeRef']) ?
-1 : 1); // 0 = above sea level; 1 = below sea level
156 $info['jpg']['exif']['GPS']['computed']['altitude'] = $direction_multiplier * getid3_lib
::DecimalizeFraction($info['jpg']['exif']['GPS']['GPSAltitude']);
162 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.tag.xmp.php', __FILE__
, true);
163 if (isset($info['filenamepath'])) {
164 $image_xmp = new Image_XMP($info['filenamepath']);
165 $xmp_raw = $image_xmp->getAllTags();
166 foreach ($xmp_raw as $key => $value) {
167 if (strpos($key, ':')) {
168 list($subsection, $tagname) = explode(':', $key);
169 $info['xmp'][$subsection][$tagname] = $this->CastAsAppropriate($value);
171 $this->warning('XMP: expecting "<subsection>:<tagname>", found "'.$key.'"');
177 unset($info['fileformat']);
184 * @param mixed $value
188 public function CastAsAppropriate($value) {
189 if (is_array($value)) {
191 } elseif (preg_match('#^[0-9]+/[0-9]+$#', $value)) {
192 return getid3_lib
::DecimalizeFraction($value);
193 } elseif (preg_match('#^[0-9]+$#', $value)) {
194 return getid3_lib
::CastAsInt($value);
195 } elseif (preg_match('#^[0-9\.]+$#', $value)) {
196 return (float) $value;
202 * @param int $iptc_record
206 public function IPTCrecordName($iptc_record) {
207 // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html
208 static $IPTCrecordName = array();
209 if (empty($IPTCrecordName)) {
210 $IPTCrecordName = array(
212 2 => 'IPTCApplication',
213 3 => 'IPTCNewsPhoto',
214 7 => 'IPTCPreObjectData',
215 8 => 'IPTCObjectData',
216 9 => 'IPTCPostObjectData',
219 return (isset($IPTCrecordName[$iptc_record]) ?
$IPTCrecordName[$iptc_record] : '');
223 * @param int $iptc_record
224 * @param int $iptc_tagkey
228 public function IPTCrecordTagName($iptc_record, $iptc_tagkey) {
229 // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html
230 static $IPTCrecordTagName = array();
231 if (empty($IPTCrecordTagName)) {
232 $IPTCrecordTagName = array(
233 1 => array( // IPTC EnvelopeRecord Tags
234 0 => 'EnvelopeRecordVersion',
238 30 => 'ServiceIdentifier',
239 40 => 'EnvelopeNumber',
241 60 => 'EnvelopePriority',
244 90 => 'CodedCharacterSet',
245 100 => 'UniqueObjectName',
246 120 => 'ARMIdentifier',
249 2 => array( // IPTC ApplicationRecord Tags
250 0 => 'ApplicationRecordVersion',
251 3 => 'ObjectTypeReference',
252 4 => 'ObjectAttributeReference',
255 8 => 'EditorialUpdate',
257 12 => 'SubjectReference',
259 20 => 'SupplementalCategories',
260 22 => 'FixtureIdentifier',
262 26 => 'ContentLocationCode',
263 27 => 'ContentLocationName',
266 37 => 'ExpirationDate',
267 38 => 'ExpirationTime',
268 40 => 'SpecialInstructions',
269 42 => 'ActionAdvised',
270 45 => 'ReferenceService',
271 47 => 'ReferenceDate',
272 50 => 'ReferenceNumber',
275 62 => 'DigitalCreationDate',
276 63 => 'DigitalCreationTime',
277 65 => 'OriginatingProgram',
278 70 => 'ProgramVersion',
281 85 => 'By-lineTitle',
283 92 => 'Sub-location',
284 95 => 'Province-State',
285 100 => 'Country-PrimaryLocationCode',
286 101 => 'Country-PrimaryLocationName',
287 103 => 'OriginalTransmissionReference',
291 116 => 'CopyrightNotice',
293 120 => 'Caption-Abstract',
294 121 => 'LocalCaption',
295 122 => 'Writer-Editor',
296 125 => 'RasterizedCaption',
298 131 => 'ImageOrientation',
299 135 => 'LanguageIdentifier',
301 151 => 'AudioSamplingRate',
302 152 => 'AudioSamplingResolution',
303 153 => 'AudioDuration',
304 154 => 'AudioOutcue',
306 185 => 'MasterDocumentID',
307 186 => 'ShortDocumentID',
308 187 => 'UniqueDocumentID',
310 200 => 'ObjectPreviewFileFormat',
311 201 => 'ObjectPreviewFileVersion',
312 202 => 'ObjectPreviewData',
314 225 => 'ClassifyState',
315 228 => 'SimilarityIndex',
316 230 => 'DocumentNotes',
317 231 => 'DocumentHistory',
318 232 => 'ExifCameraInfo',
320 3 => array( // IPTC NewsPhoto Tags
321 0 => 'NewsPhotoVersion',
322 10 => 'IPTCPictureNumber',
323 20 => 'IPTCImageWidth',
324 30 => 'IPTCImageHeight',
325 40 => 'IPTCPixelWidth',
326 50 => 'IPTCPixelHeight',
327 55 => 'SupplementalType',
328 60 => 'ColorRepresentation',
329 64 => 'InterchangeColorSpace',
330 65 => 'ColorSequence',
332 70 => 'ColorCalibrationMatrix',
334 84 => 'NumIndexEntries',
335 85 => 'ColorPalette',
336 86 => 'IPTCBitsPerSample',
337 90 => 'SampleStructure',
338 100 => 'ScanningDirection',
339 102 => 'IPTCImageRotation',
340 110 => 'DataCompressionMethod',
341 120 => 'QuantizationMethod',
343 130 => 'ExcursionTolerance',
344 135 => 'BitsPerComponent',
345 140 => 'MaximumDensityRange',
346 145 => 'GammaCompensatedValue',
348 7 => array( // IPTC PreObjectData Tags
350 20 => 'MaxSubfileSize',
351 90 => 'ObjectSizeAnnounced',
352 95 => 'MaximumObjectSize',
354 8 => array( // IPTC ObjectData Tags
357 9 => array( // IPTC PostObjectData Tags
358 10 => 'ConfirmedObjectSize',
363 return (isset($IPTCrecordTagName[$iptc_record][$iptc_tagkey]) ?
$IPTCrecordTagName[$iptc_record][$iptc_tagkey] : $iptc_tagkey);