3c874f32b47b2e09e657e412193369c581500cd1
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.graphic.jpg.php
1 <?php
2
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 /////////////////////////////////////////////////////////////////
10 // //
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) //
15 // ///
16 /////////////////////////////////////////////////////////////////
17
18
19 class getid3_jpg extends getid3_handler
20 {
21 /**
22 * @return bool
23 */
24 public function Analyze() {
25 $info = &$this->getid3->info;
26
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;
32
33 $this->fseek($info['avdataoffset']);
34
35 $imageinfo = array();
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
38
39
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;
53 } else {
54 $info['iptc']['comments'][$IPTCrecordName][$IPTCrecordTagName] = array($value);
55 }
56 }
57 }
58 }
59 }
60
61 $returnOK = false;
62 switch ($type) {
63 case IMG_JPG:
64 $info['video']['resolution_x'] = $width;
65 $info['video']['resolution_y'] = $height;
66
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');
71 //return false;
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
75 return false;
76 }
77
78 $errcontext['info']['warning'][] = 'Error parsing EXIF data ('.$errstr.')';
79 });
80
81 $info['jpg']['exif'] = exif_read_data($info['filenamepath'], null, true, false);
82
83 restore_error_handler();
84 } else {
85 $this->warning('exif_read_data() cannot parse non-EXIF data in APP1 (expected "Exif", found "'.substr($imageinfo['APP1'], 0, 4).'")');
86 }
87 } else {
88 $this->warning('EXIF parsing only available when '.(GETID3_OS_ISWINDOWS ? 'php_exif.dll enabled' : 'compiled with --enable-exif'));
89 }
90 }
91 $returnOK = true;
92 break;
93
94 default:
95 break;
96 }
97
98
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);
104 }
105 }
106 }
107
108
109 if (isset($info['jpg']['exif']['GPS'])) {
110
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));
115 }
116 $info['jpg']['exif']['GPS']['computed']['version'] = 'v'.implode('.', $version_subparts);
117 }
118
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] : '');
124
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);
129 }
130 }
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]);
132 }
133
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);
139 }
140 $info['jpg']['exif']['GPS']['computed']['latitude'] = $direction_multiplier * ($computed_latitude[0] + ($computed_latitude[1] / 60) + ($computed_latitude[2] / 3600));
141 }
142
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);
148 }
149 $info['jpg']['exif']['GPS']['computed']['longitude'] = $direction_multiplier * ($computed_longitude[0] + ($computed_longitude[1] / 60) + ($computed_longitude[2] / 3600));
150 }
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
153 }
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']);
157 }
158
159 }
160
161
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);
170 } else {
171 $this->warning('XMP: expecting "<subsection>:<tagname>", found "'.$key.'"');
172 }
173 }
174 }
175
176 if (!$returnOK) {
177 unset($info['fileformat']);
178 return false;
179 }
180 return true;
181 }
182
183 /**
184 * @param mixed $value
185 *
186 * @return mixed
187 */
188 public function CastAsAppropriate($value) {
189 if (is_array($value)) {
190 return $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;
197 }
198 return $value;
199 }
200
201 /**
202 * @param int $iptc_record
203 *
204 * @return string
205 */
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(
211 1 => 'IPTCEnvelope',
212 2 => 'IPTCApplication',
213 3 => 'IPTCNewsPhoto',
214 7 => 'IPTCPreObjectData',
215 8 => 'IPTCObjectData',
216 9 => 'IPTCPostObjectData',
217 );
218 }
219 return (isset($IPTCrecordName[$iptc_record]) ? $IPTCrecordName[$iptc_record] : '');
220 }
221
222 /**
223 * @param int $iptc_record
224 * @param int $iptc_tagkey
225 *
226 * @return string
227 */
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',
235 5 => 'Destination',
236 20 => 'FileFormat',
237 22 => 'FileVersion',
238 30 => 'ServiceIdentifier',
239 40 => 'EnvelopeNumber',
240 50 => 'ProductID',
241 60 => 'EnvelopePriority',
242 70 => 'DateSent',
243 80 => 'TimeSent',
244 90 => 'CodedCharacterSet',
245 100 => 'UniqueObjectName',
246 120 => 'ARMIdentifier',
247 122 => 'ARMVersion',
248 ),
249 2 => array( // IPTC ApplicationRecord Tags
250 0 => 'ApplicationRecordVersion',
251 3 => 'ObjectTypeReference',
252 4 => 'ObjectAttributeReference',
253 5 => 'ObjectName',
254 7 => 'EditStatus',
255 8 => 'EditorialUpdate',
256 10 => 'Urgency',
257 12 => 'SubjectReference',
258 15 => 'Category',
259 20 => 'SupplementalCategories',
260 22 => 'FixtureIdentifier',
261 25 => 'Keywords',
262 26 => 'ContentLocationCode',
263 27 => 'ContentLocationName',
264 30 => 'ReleaseDate',
265 35 => 'ReleaseTime',
266 37 => 'ExpirationDate',
267 38 => 'ExpirationTime',
268 40 => 'SpecialInstructions',
269 42 => 'ActionAdvised',
270 45 => 'ReferenceService',
271 47 => 'ReferenceDate',
272 50 => 'ReferenceNumber',
273 55 => 'DateCreated',
274 60 => 'TimeCreated',
275 62 => 'DigitalCreationDate',
276 63 => 'DigitalCreationTime',
277 65 => 'OriginatingProgram',
278 70 => 'ProgramVersion',
279 75 => 'ObjectCycle',
280 80 => 'By-line',
281 85 => 'By-lineTitle',
282 90 => 'City',
283 92 => 'Sub-location',
284 95 => 'Province-State',
285 100 => 'Country-PrimaryLocationCode',
286 101 => 'Country-PrimaryLocationName',
287 103 => 'OriginalTransmissionReference',
288 105 => 'Headline',
289 110 => 'Credit',
290 115 => 'Source',
291 116 => 'CopyrightNotice',
292 118 => 'Contact',
293 120 => 'Caption-Abstract',
294 121 => 'LocalCaption',
295 122 => 'Writer-Editor',
296 125 => 'RasterizedCaption',
297 130 => 'ImageType',
298 131 => 'ImageOrientation',
299 135 => 'LanguageIdentifier',
300 150 => 'AudioType',
301 151 => 'AudioSamplingRate',
302 152 => 'AudioSamplingResolution',
303 153 => 'AudioDuration',
304 154 => 'AudioOutcue',
305 184 => 'JobID',
306 185 => 'MasterDocumentID',
307 186 => 'ShortDocumentID',
308 187 => 'UniqueDocumentID',
309 188 => 'OwnerID',
310 200 => 'ObjectPreviewFileFormat',
311 201 => 'ObjectPreviewFileVersion',
312 202 => 'ObjectPreviewData',
313 221 => 'Prefs',
314 225 => 'ClassifyState',
315 228 => 'SimilarityIndex',
316 230 => 'DocumentNotes',
317 231 => 'DocumentHistory',
318 232 => 'ExifCameraInfo',
319 ),
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',
331 66 => 'ICC_Profile',
332 70 => 'ColorCalibrationMatrix',
333 80 => 'LookupTable',
334 84 => 'NumIndexEntries',
335 85 => 'ColorPalette',
336 86 => 'IPTCBitsPerSample',
337 90 => 'SampleStructure',
338 100 => 'ScanningDirection',
339 102 => 'IPTCImageRotation',
340 110 => 'DataCompressionMethod',
341 120 => 'QuantizationMethod',
342 125 => 'EndPoints',
343 130 => 'ExcursionTolerance',
344 135 => 'BitsPerComponent',
345 140 => 'MaximumDensityRange',
346 145 => 'GammaCompensatedValue',
347 ),
348 7 => array( // IPTC PreObjectData Tags
349 10 => 'SizeMode',
350 20 => 'MaxSubfileSize',
351 90 => 'ObjectSizeAnnounced',
352 95 => 'MaximumObjectSize',
353 ),
354 8 => array( // IPTC ObjectData Tags
355 10 => 'SubFile',
356 ),
357 9 => array( // IPTC PostObjectData Tags
358 10 => 'ConfirmedObjectSize',
359 ),
360 );
361
362 }
363 return (isset($IPTCrecordTagName[$iptc_record][$iptc_tagkey]) ? $IPTCrecordTagName[$iptc_record][$iptc_tagkey] : $iptc_tagkey);
364 }
365
366 }