* Let yes be yes and no be no
[lhc/web/wiklou.git] / includes / Exif.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Metadata
5 *
6 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
7 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
8 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
26 * @bug 1555, 1947
27 */
28
29 /**
30 * @package MediaWiki
31 * @subpackage Metadata
32 */
33 class Exif {
34 //@{
35 /* @var array
36 * @private
37 */
38
39 /**#@+
40 * Exif tag type definition
41 */
42 const BYTE = 1; # An 8-bit (1-byte) unsigned integer.
43 const ASCII = 2; # An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.
44 const SHORT = 3; # A 16-bit (2-byte) unsigned integer.
45 const LONG = 4; # A 32-bit (4-byte) unsigned integer.
46 const RATIONAL = 5; # Two LONGs. The first LONG is the numerator and the second LONG expresses the denominator
47 const UNDEFINED = 7; # An 8-bit byte that can take any value depending on the field definition
48 const SLONG = 9; # A 32-bit (4-byte) signed integer (2's complement notation),
49 const SRATIONAL = 10; # Two SLONGs. The first SLONG is the numerator and the second SLONG is the denominator.
50
51 /**
52 * Exif tags grouped by category, the tagname itself is the key and the type
53 * is the value, in the case of more than one possible value type they are
54 * seperated by commas.
55 */
56 var $mExifTags;
57
58 /**
59 * A one dimentional array of all Exif tags
60 */
61 var $mFlatExifTags;
62
63 /**
64 * The raw Exif data returned by exif_read_data()
65 */
66 var $mRawExifData;
67
68 /**
69 * A Filtered version of $mRawExifData that has been pruned of invalid
70 * tags and tags that contain content they shouldn't contain according
71 * to the Exif specification
72 */
73 var $mFilteredExifData;
74
75 /**
76 * Filtered and formatted Exif data, see FormatExif::getFormattedData()
77 */
78 var $mFormattedExifData;
79
80 //@}
81
82 //@{
83 /* @var string
84 * @private
85 */
86
87 /**
88 * The file being processed
89 */
90 var $file;
91
92 /**
93 * The basename of the file being processed
94 */
95 var $basename;
96
97 /**
98 * The private log to log to
99 */
100 var $log = 'exif';
101
102 //@}
103
104 /**
105 * Constructor
106 *
107 * @param $file String: filename.
108 */
109 function Exif( $file ) {
110 /**
111 * Page numbers here refer to pages in the EXIF 2.2 standard
112 *
113 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
114 */
115 $this->mExifTags = array(
116 # TIFF Rev. 6.0 Attribute Information (p22)
117 'tiff' => array(
118 # Tags relating to image structure
119 'structure' => array(
120 'ImageWidth' => Exif::SHORT.','.Exif::LONG, # Image width
121 'ImageLength' => Exif::SHORT.','.Exif::LONG, # Image height
122 'BitsPerSample' => Exif::SHORT, # Number of bits per component
123 # "When a primary image is JPEG compressed, this designation is not"
124 # "necessary and is omitted." (p23)
125 'Compression' => Exif::SHORT, # Compression scheme #p23
126 'PhotometricInterpretation' => Exif::SHORT, # Pixel composition #p23
127 'Orientation' => Exif::SHORT, # Orientation of image #p24
128 'SamplesPerPixel' => Exif::SHORT, # Number of components
129 'PlanarConfiguration' => Exif::SHORT, # Image data arrangement #p24
130 'YCbCrSubSampling' => Exif::SHORT, # Subsampling ratio of Y to C #p24
131 'YCbCrPositioning' => Exif::SHORT, # Y and C positioning #p24-25
132 'XResolution' => Exif::RATIONAL, # Image resolution in width direction
133 'YResolution' => Exif::RATIONAL, # Image resolution in height direction
134 'ResolutionUnit' => Exif::SHORT, # Unit of X and Y resolution #(p26)
135 ),
136
137 # Tags relating to recording offset
138 'offset' => array(
139 'StripOffsets' => Exif::SHORT.','.Exif::LONG, # Image data location
140 'RowsPerStrip' => Exif::SHORT.','.Exif::LONG, # Number of rows per strip
141 'StripByteCounts' => Exif::SHORT.','.Exif::LONG, # Bytes per compressed strip
142 'JPEGInterchangeFormat' => Exif::SHORT.','.Exif::LONG, # Offset to JPEG SOI
143 'JPEGInterchangeFormatLength' => Exif::SHORT.','.Exif::LONG, # Bytes of JPEG data
144 ),
145
146 # Tags relating to image data characteristics
147 'characteristics' => array(
148 'TransferFunction' => Exif::SHORT, # Transfer function
149 'WhitePoint' => Exif::RATIONAL, # White point chromaticity
150 'PrimaryChromaticities' => Exif::RATIONAL, # Chromaticities of primarities
151 'YCbCrCoefficients' => Exif::RATIONAL, # Color space transformation matrix coefficients #p27
152 'ReferenceBlackWhite' => Exif::RATIONAL # Pair of black and white reference values
153 ),
154
155 # Other tags
156 'other' => array(
157 'DateTime' => Exif::ASCII, # File change date and time
158 'ImageDescription' => Exif::ASCII, # Image title
159 'Make' => Exif::ASCII, # Image input equipment manufacturer
160 'Model' => Exif::ASCII, # Image input equipment model
161 'Software' => Exif::ASCII, # Software used
162 'Artist' => Exif::ASCII, # Person who created the image
163 'Copyright' => Exif::ASCII, # Copyright holder
164 ),
165 ),
166
167 # Exif IFD Attribute Information (p30-31)
168 'exif' => array(
169 # Tags relating to version
170 'version' => array(
171 # TODO: NOTE: Nonexistence of this field is taken to mean nonconformance
172 # to the EXIF 2.1 AND 2.2 standards
173 'ExifVersion' => Exif::UNDEFINED, # Exif version
174 'FlashpixVersion' => Exif::UNDEFINED, # Supported Flashpix version #p32
175 ),
176
177 # Tags relating to Image Data Characteristics
178 'characteristics' => array(
179 'ColorSpace' => Exif::SHORT, # Color space information #p32
180 ),
181
182 # Tags relating to image configuration
183 'configuration' => array(
184 'ComponentsConfiguration' => Exif::UNDEFINED, # Meaning of each component #p33
185 'CompressedBitsPerPixel' => Exif::RATIONAL, # Image compression mode
186 'PixelYDimension' => Exif::SHORT.','.Exif::LONG, # Valid image width
187 'PixelXDimension' => Exif::SHORT.','.Exif::LONG, # Valind image height
188 ),
189
190 # Tags relating to related user information
191 'user' => array(
192 'MakerNote' => Exif::UNDEFINED, # Manufacturer notes
193 'UserComment' => Exif::UNDEFINED, # User comments #p34
194 ),
195
196 # Tags relating to related file information
197 'related' => array(
198 'RelatedSoundFile' => Exif::ASCII, # Related audio file
199 ),
200
201 # Tags relating to date and time
202 'dateandtime' => array(
203 'DateTimeOriginal' => Exif::ASCII, # Date and time of original data generation #p36
204 'DateTimeDigitized' => Exif::ASCII, # Date and time of original data generation
205 'SubSecTime' => Exif::ASCII, # DateTime subseconds
206 'SubSecTimeOriginal' => Exif::ASCII, # DateTimeOriginal subseconds
207 'SubSecTimeDigitized' => Exif::ASCII, # DateTimeDigitized subseconds
208 ),
209
210 # Tags relating to picture-taking conditions (p31)
211 'conditions' => array(
212 'ExposureTime' => Exif::RATIONAL, # Exposure time
213 'FNumber' => Exif::RATIONAL, # F Number
214 'ExposureProgram' => Exif::SHORT, # Exposure Program #p38
215 'SpectralSensitivity' => Exif::ASCII, # Spectral sensitivity
216 'ISOSpeedRatings' => Exif::SHORT, # ISO speed rating
217 'OECF' => Exif::UNDEFINED, # Optoelectronic conversion factor
218 'ShutterSpeedValue' => Exif::SRATIONAL, # Shutter speed
219 'ApertureValue' => Exif::RATIONAL, # Aperture
220 'BrightnessValue' => Exif::SRATIONAL, # Brightness
221 'ExposureBiasValue' => Exif::SRATIONAL, # Exposure bias
222 'MaxApertureValue' => Exif::RATIONAL, # Maximum land aperture
223 'SubjectDistance' => Exif::RATIONAL, # Subject distance
224 'MeteringMode' => Exif::SHORT, # Metering mode #p40
225 'LightSource' => Exif::SHORT, # Light source #p40-41
226 'Flash' => Exif::SHORT, # Flash #p41-42
227 'FocalLength' => Exif::RATIONAL, # Lens focal length
228 'SubjectArea' => Exif::SHORT, # Subject area
229 'FlashEnergy' => Exif::RATIONAL, # Flash energy
230 'SpatialFrequencyResponse' => Exif::UNDEFINED, # Spatial frequency response
231 'FocalPlaneXResolution' => Exif::RATIONAL, # Focal plane X resolution
232 'FocalPlaneYResolution' => Exif::RATIONAL, # Focal plane Y resolution
233 'FocalPlaneResolutionUnit' => Exif::SHORT, # Focal plane resolution unit #p46
234 'SubjectLocation' => Exif::SHORT, # Subject location
235 'ExposureIndex' => Exif::RATIONAL, # Exposure index
236 'SensingMethod' => Exif::SHORT, # Sensing method #p46
237 'FileSource' => Exif::UNDEFINED, # File source #p47
238 'SceneType' => Exif::UNDEFINED, # Scene type #p47
239 'CFAPattern' => Exif::UNDEFINED, # CFA pattern
240 'CustomRendered' => Exif::SHORT, # Custom image processing #p48
241 'ExposureMode' => Exif::SHORT, # Exposure mode #p48
242 'WhiteBalance' => Exif::SHORT, # White Balance #p49
243 'DigitalZoomRatio' => Exif::RATIONAL, # Digital zoom ration
244 'FocalLengthIn35mmFilm' => Exif::SHORT, # Focal length in 35 mm film
245 'SceneCaptureType' => Exif::SHORT, # Scene capture type #p49
246 'GainControl' => Exif::RATIONAL, # Scene control #p49-50
247 'Contrast' => Exif::SHORT, # Contrast #p50
248 'Saturation' => Exif::SHORT, # Saturation #p50
249 'Sharpness' => Exif::SHORT, # Sharpness #p50
250 'DeviceSettingDescription' => Exif::UNDEFINED, # Desice settings description
251 'SubjectDistanceRange' => Exif::SHORT, # Subject distance range #p51
252 ),
253
254 'other' => array(
255 'ImageUniqueID' => Exif::ASCII, # Unique image ID
256 ),
257 ),
258
259 # GPS Attribute Information (p52)
260 'gps' => array(
261 'GPSVersionID' => Exif::BYTE, # GPS tag version
262 'GPSLatitudeRef' => Exif::ASCII, # North or South Latitude #p52-53
263 'GPSLatitude' => Exif::RATIONAL, # Latitude
264 'GPSLongitudeRef' => Exif::ASCII, # East or West Longitude #p53
265 'GPSLongitude' => Exif::RATIONAL, # Longitude
266 'GPSAltitudeRef' => Exif::BYTE, # Altitude reference
267 'GPSAltitude' => Exif::RATIONAL, # Altitude
268 'GPSTimeStamp' => Exif::RATIONAL, # GPS time (atomic clock)
269 'GPSSatellites' => Exif::ASCII, # Satellites used for measurement
270 'GPSStatus' => Exif::ASCII, # Receiver status #p54
271 'GPSMeasureMode' => Exif::ASCII, # Measurement mode #p54-55
272 'GPSDOP' => Exif::RATIONAL, # Measurement precision
273 'GPSSpeedRef' => Exif::ASCII, # Speed unit #p55
274 'GPSSpeed' => Exif::RATIONAL, # Speed of GPS receiver
275 'GPSTrackRef' => Exif::ASCII, # Reference for direction of movement #p55
276 'GPSTrack' => Exif::RATIONAL, # Direction of movement
277 'GPSImgDirectionRef' => Exif::ASCII, # Reference for direction of image #p56
278 'GPSImgDirection' => Exif::RATIONAL, # Direction of image
279 'GPSMapDatum' => Exif::ASCII, # Geodetic survey data used
280 'GPSDestLatitudeRef' => Exif::ASCII, # Reference for latitude of destination #p56
281 'GPSDestLatitude' => Exif::RATIONAL, # Latitude destination
282 'GPSDestLongitudeRef' => Exif::ASCII, # Reference for longitude of destination #p57
283 'GPSDestLongitude' => Exif::RATIONAL, # Longitude of destination
284 'GPSDestBearingRef' => Exif::ASCII, # Reference for bearing of destination #p57
285 'GPSDestBearing' => Exif::RATIONAL, # Bearing of destination
286 'GPSDestDistanceRef' => Exif::ASCII, # Reference for distance to destination #p57-58
287 'GPSDestDistance' => Exif::RATIONAL, # Distance to destination
288 'GPSProcessingMethod' => Exif::UNDEFINED, # Name of GPS processing method
289 'GPSAreaInformation' => Exif::UNDEFINED, # Name of GPS area
290 'GPSDateStamp' => Exif::ASCII, # GPS date
291 'GPSDifferential' => Exif::SHORT, # GPS differential correction
292 ),
293 );
294
295 $this->file = $file;
296 $this->basename = basename( $this->file );
297
298 $this->makeFlatExifTags();
299
300 $this->debugFile( $this->basename, __FUNCTION__, true );
301 wfSuppressWarnings();
302 $data = exif_read_data( $this->file );
303 wfRestoreWarnings();
304 /**
305 * exif_read_data() will return false on invalid input, such as
306 * when somebody uploads a file called something.jpeg
307 * containing random gibberish.
308 */
309 $this->mRawExifData = $data ? $data : array();
310
311 $this->makeFilteredData();
312 $this->makeFormattedData();
313
314 $this->debugFile( __FUNCTION__, false );
315 }
316
317 /**#@+
318 * @private
319 */
320 /**
321 * Generate a flat list of the exif tags
322 */
323 function makeFlatExifTags() {
324 $this->extractTags( $this->mExifTags );
325 }
326
327 /**
328 * A recursing extractor function used by makeFlatExifTags()
329 *
330 * Note: This used to use an array_walk function, but it made PHP5
331 * segfault, see `cvs diff -u -r 1.4 -r 1.5 Exif.php`
332 */
333 function extractTags( &$tagset ) {
334 foreach( $tagset as $key => $val ) {
335 if( is_array( $val ) ) {
336 $this->extractTags( $val );
337 } else {
338 $this->mFlatExifTags[$key] = $val;
339 }
340 }
341 }
342
343 /**
344 * Make $this->mFilteredExifData
345 */
346 function makeFilteredData() {
347 $this->mFilteredExifData = $this->mRawExifData;
348
349 foreach( $this->mFilteredExifData as $k => $v ) {
350 if ( !in_array( $k, array_keys( $this->mFlatExifTags ) ) ) {
351 $this->debug( $v, __FUNCTION__, "'$k' is not a valid Exif tag" );
352 unset( $this->mFilteredExifData[$k] );
353 }
354 }
355
356 foreach( $this->mFilteredExifData as $k => $v ) {
357 if ( !$this->validate($k, $v) ) {
358 $this->debug( $v, __FUNCTION__, "'$k' contained invalid data" );
359 unset( $this->mFilteredExifData[$k] );
360 }
361 }
362 }
363
364 /**
365 * @todo document
366 */
367 function makeFormattedData( ) {
368 $format = new FormatExif( $this->getFilteredData() );
369 $this->mFormattedExifData = $format->getFormattedData();
370 }
371 /**#@-*/
372
373 /**#@+
374 * @return array
375 */
376 /**
377 * Get $this->mRawExifData
378 */
379 function getData() {
380 return $this->mRawExifData;
381 }
382
383 /**
384 * Get $this->mFilteredExifData
385 */
386 function getFilteredData() {
387 return $this->mFilteredExifData;
388 }
389
390 /**
391 * Get $this->mFormattedExifData
392 */
393 function getFormattedData() {
394 return $this->mFormattedExifData;
395 }
396 /**#@-*/
397
398 /**
399 * The version of the output format
400 *
401 * Before the actual metadata information is saved in the database we
402 * strip some of it since we don't want to save things like thumbnails
403 * which usually accompany Exif data. This value gets saved in the
404 * database along with the actual Exif data, and if the version in the
405 * database doesn't equal the value returned by this function the Exif
406 * data is regenerated.
407 *
408 * @return int
409 */
410 function version() {
411 return 1; // We don't need no bloddy constants!
412 }
413
414 /**#@+
415 * Validates if a tag value is of the type it should be according to the Exif spec
416 *
417 * @private
418 *
419 * @param $in Mixed: the input value to check
420 * @return bool
421 */
422 function isByte( $in ) {
423 if ( !is_array( $in ) && sprintf('%d', $in) == $in && $in >= 0 && $in <= 255 ) {
424 $this->debug( $in, __FUNCTION__, true );
425 return true;
426 } else {
427 $this->debug( $in, __FUNCTION__, false );
428 return false;
429 }
430 }
431
432 function isASCII( $in ) {
433 if ( is_array( $in ) ) {
434 return false;
435 }
436
437 if ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) {
438 $this->debug( $in, __FUNCTION__, 'found a character not in our whitelist' );
439 return false;
440 }
441
442 if ( preg_match( "/^\s*$/", $in ) ) {
443 $this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' );
444 return false;
445 }
446
447 return true;
448 }
449
450 function isShort( $in ) {
451 if ( !is_array( $in ) && sprintf('%d', $in) == $in && $in >= 0 && $in <= 65536 ) {
452 $this->debug( $in, __FUNCTION__, true );
453 return true;
454 } else {
455 $this->debug( $in, __FUNCTION__, false );
456 return false;
457 }
458 }
459
460 function isLong( $in ) {
461 if ( !is_array( $in ) && sprintf('%d', $in) == $in && $in >= 0 && $in <= 4294967296 ) {
462 $this->debug( $in, __FUNCTION__, true );
463 return true;
464 } else {
465 $this->debug( $in, __FUNCTION__, false );
466 return false;
467 }
468 }
469
470 function isRational( $in ) {
471 if ( !is_array( $in ) && @preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
472 return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
473 } else {
474 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
475 return false;
476 }
477 }
478
479 function isUndefined( $in ) {
480 if ( !is_array( $in ) && preg_match( "/^\d{4}$/", $in ) ) { // Allow ExifVersion and FlashpixVersion
481 $this->debug( $in, __FUNCTION__, true );
482 return true;
483 } else {
484 $this->debug( $in, __FUNCTION__, false );
485 return false;
486 }
487 }
488
489 function isSlong( $in ) {
490 if ( $this->isLong( abs( $in ) ) ) {
491 $this->debug( $in, __FUNCTION__, true );
492 return true;
493 } else {
494 $this->debug( $in, __FUNCTION__, false );
495 return false;
496 }
497 }
498
499 function isSrational( $in ) {
500 if ( !is_array( $in ) && preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
501 return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
502 } else {
503 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
504 return false;
505 }
506 }
507 /**#@-*/
508
509 /**
510 * Validates if a tag has a legal value according to the Exif spec
511 *
512 * @private
513 *
514 * @param $tag String: the tag to check.
515 * @param $val Mixed: the value of the tag.
516 * @return bool
517 */
518 function validate( $tag, $val ) {
519 $debug = "tag is '$tag'";
520 // Does not work if not typecast
521 switch( (string)$this->mFlatExifTags[$tag] ) {
522 case (string)Exif::BYTE:
523 $this->debug( $val, __FUNCTION__, $debug );
524 return $this->isByte( $val );
525 case (string)Exif::ASCII:
526 $this->debug( $val, __FUNCTION__, $debug );
527 return $this->isASCII( $val );
528 case (string)Exif::SHORT:
529 $this->debug( $val, __FUNCTION__, $debug );
530 return $this->isShort( $val );
531 case (string)Exif::LONG:
532 $this->debug( $val, __FUNCTION__, $debug );
533 return $this->isLong( $val );
534 case (string)Exif::RATIONAL:
535 $this->debug( $val, __FUNCTION__, $debug );
536 return $this->isRational( $val );
537 case (string)Exif::UNDEFINED:
538 $this->debug( $val, __FUNCTION__, $debug );
539 return $this->isUndefined( $val );
540 case (string)Exif::SLONG:
541 $this->debug( $val, __FUNCTION__, $debug );
542 return $this->isSlong( $val );
543 case (string)Exif::SRATIONAL:
544 $this->debug( $val, __FUNCTION__, $debug );
545 return $this->isSrational( $val );
546 case (string)Exif::SHORT.','.Exif::LONG:
547 $this->debug( $val, __FUNCTION__, $debug );
548 return $this->isShort( $val ) || $this->isLong( $val );
549 default:
550 $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
551 return false;
552 }
553 }
554
555 /**
556 * Convenience function for debugging output
557 *
558 * @private
559 *
560 * @param $in Mixed:
561 * @param $fname String:
562 * @param $action Mixed: , default NULL.
563 */
564 function debug( $in, $fname, $action = NULL ) {
565 $type = gettype( $in );
566 $class = ucfirst( __CLASS__ );
567 if ( $type === 'array' )
568 $in = print_r( $in, true );
569
570 if ( $action === true )
571 wfDebugLog( $this->log, "$class::$fname: accepted: '$in' (type: $type)\n");
572 elseif ( $action === false )
573 wfDebugLog( $this->log, "$class::$fname: rejected: '$in' (type: $type)\n");
574 elseif ( $action === null )
575 wfDebugLog( $this->log, "$class::$fname: input was: '$in' (type: $type)\n");
576 else
577 wfDebugLog( $this->log, "$class::$fname: $action (type: $type; content: '$in')\n");
578 }
579
580 /**
581 * Convenience function for debugging output
582 *
583 * @private
584 *
585 * @param $fname String: the name of the function calling this function
586 * @param $io Boolean: Specify whether we're beginning or ending
587 */
588 function debugFile( $fname, $io ) {
589 $class = ucfirst( __CLASS__ );
590 if ( $io ) {
591 wfDebugLog( $this->log, "$class::$fname: begin processing: '{$this->basename}'\n" );
592 } else {
593 wfDebugLog( $this->log, "$class::$fname: end processing: '{$this->basename}'\n" );
594 }
595 }
596
597 }
598
599 /**
600 * @package MediaWiki
601 * @subpackage Metadata
602 */
603 class FormatExif {
604 /**
605 * The Exif data to format
606 *
607 * @var array
608 * @private
609 */
610 var $mExif;
611
612 /**
613 * Constructor
614 *
615 * @param $exif Array: the Exif data to format ( as returned by
616 * Exif::getFilteredData() )
617 */
618 function FormatExif( $exif ) {
619 $this->mExif = $exif;
620 }
621
622 /**
623 * Numbers given by Exif user agents are often magical, that is they
624 * should be replaced by a detailed explanation depending on their
625 * value which most of the time are plain integers. This function
626 * formats Exif values into human readable form.
627 *
628 * @return array
629 */
630 function getFormattedData() {
631 global $wgLang;
632
633 $tags =& $this->mExif;
634
635 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
636 unset( $tags['ResolutionUnit'] );
637
638 foreach( $tags as $tag => $val ) {
639 switch( $tag ) {
640 case 'Compression':
641 switch( $val ) {
642 case 1: case 6:
643 $tags[$tag] = $this->msg( $tag, $val );
644 break;
645 default:
646 $tags[$tag] = $val;
647 break;
648 }
649 break;
650
651 case 'PhotometricInterpretation':
652 switch( $val ) {
653 case 2: case 6:
654 $tags[$tag] = $this->msg( $tag, $val );
655 break;
656 default:
657 $tags[$tag] = $val;
658 break;
659 }
660 break;
661
662 case 'Orientation':
663 switch( $val ) {
664 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
665 $tags[$tag] = $this->msg( $tag, $val );
666 break;
667 default:
668 $tags[$tag] = $val;
669 break;
670 }
671 break;
672
673 case 'PlanarConfiguration':
674 switch( $val ) {
675 case 1: case 2:
676 $tags[$tag] = $this->msg( $tag, $val );
677 break;
678 default:
679 $tags[$tag] = $val;
680 break;
681 }
682 break;
683
684 // TODO: YCbCrSubSampling
685 // TODO: YCbCrPositioning
686
687 case 'XResolution':
688 case 'YResolution':
689 switch( $resolutionunit ) {
690 case 2:
691 $tags[$tag] = $this->msg( 'XYResolution', 'i', $this->formatNum( $val ) );
692 break;
693 case 3:
694 $this->msg( 'XYResolution', 'c', $this->formatNum( $val ) );
695 break;
696 default:
697 $tags[$tag] = $val;
698 break;
699 }
700 break;
701
702 // TODO: YCbCrCoefficients #p27 (see annex E)
703 case 'ExifVersion': case 'FlashpixVersion':
704 $tags[$tag] = "$val"/100;
705 break;
706
707 case 'ColorSpace':
708 switch( $val ) {
709 case 1: case 'FFFF.H':
710 $tags[$tag] = $this->msg( $tag, $val );
711 break;
712 default:
713 $tags[$tag] = $val;
714 break;
715 }
716 break;
717
718 case 'ComponentsConfiguration':
719 switch( $val ) {
720 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
721 $tags[$tag] = $this->msg( $tag, $val );
722 break;
723 default:
724 $tags[$tag] = $val;
725 break;
726 }
727 break;
728
729 case 'DateTime':
730 case 'DateTimeOriginal':
731 case 'DateTimeDigitized':
732 if( preg_match( "/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/", $val ) ) {
733 $tags[$tag] = $wgLang->timeanddate( wfTimestamp(TS_MW, $val) );
734 }
735 break;
736
737 case 'ExposureProgram':
738 switch( $val ) {
739 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
740 $tags[$tag] = $this->msg( $tag, $val );
741 break;
742 default:
743 $tags[$tag] = $val;
744 break;
745 }
746 break;
747
748 case 'SubjectDistance':
749 $tags[$tag] = $this->msg( $tag, '', $this->formatNum( $val ) );
750 break;
751
752 case 'MeteringMode':
753 switch( $val ) {
754 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
755 $tags[$tag] = $this->msg( $tag, $val );
756 break;
757 default:
758 $tags[$tag] = $val;
759 break;
760 }
761 break;
762
763 case 'LightSource':
764 switch( $val ) {
765 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
766 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
767 case 21: case 22: case 23: case 24: case 255:
768 $tags[$tag] = $this->msg( $tag, $val );
769 break;
770 default:
771 $tags[$tag] = $val;
772 break;
773 }
774 break;
775
776 // TODO: Flash
777 case 'FocalPlaneResolutionUnit':
778 switch( $val ) {
779 case 2:
780 $tags[$tag] = $this->msg( $tag, $val );
781 break;
782 default:
783 $tags[$tag] = $val;
784 break;
785 }
786 break;
787
788 case 'SensingMethod':
789 switch( $val ) {
790 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
791 $tags[$tag] = $this->msg( $tag, $val );
792 break;
793 default:
794 $tags[$tag] = $val;
795 break;
796 }
797 break;
798
799 case 'FileSource':
800 switch( $val ) {
801 case 3:
802 $tags[$tag] = $this->msg( $tag, $val );
803 break;
804 default:
805 $tags[$tag] = $val;
806 break;
807 }
808 break;
809
810 case 'SceneType':
811 switch( $val ) {
812 case 1:
813 $tags[$tag] = $this->msg( $tag, $val );
814 break;
815 default:
816 $tags[$tag] = $val;
817 break;
818 }
819 break;
820
821 case 'CustomRendered':
822 switch( $val ) {
823 case 0: case 1:
824 $tags[$tag] = $this->msg( $tag, $val );
825 break;
826 default:
827 $tags[$tag] = $val;
828 break;
829 }
830 break;
831
832 case 'ExposureMode':
833 switch( $val ) {
834 case 0: case 1: case 2:
835 $tags[$tag] = $this->msg( $tag, $val );
836 break;
837 default:
838 $tags[$tag] = $val;
839 break;
840 }
841 break;
842
843 case 'WhiteBalance':
844 switch( $val ) {
845 case 0: case 1:
846 $tags[$tag] = $this->msg( $tag, $val );
847 break;
848 default:
849 $tags[$tag] = $val;
850 break;
851 }
852 break;
853
854 case 'SceneCaptureType':
855 switch( $val ) {
856 case 0: case 1: case 2: case 3:
857 $tags[$tag] = $this->msg( $tag, $val );
858 break;
859 default:
860 $tags[$tag] = $val;
861 break;
862 }
863 break;
864
865 case 'GainControl':
866 switch( $val ) {
867 case 0: case 1: case 2: case 3: case 4:
868 $tags[$tag] = $this->msg( $tag, $val );
869 break;
870 default:
871 $tags[$tag] = $val;
872 break;
873 }
874 break;
875
876 case 'Contrast':
877 switch( $val ) {
878 case 0: case 1: case 2:
879 $tags[$tag] = $this->msg( $tag, $val );
880 break;
881 default:
882 $tags[$tag] = $val;
883 break;
884 }
885 break;
886
887 case 'Saturation':
888 switch( $val ) {
889 case 0: case 1: case 2:
890 $tags[$tag] = $this->msg( $tag, $val );
891 break;
892 default:
893 $tags[$tag] = $val;
894 break;
895 }
896 break;
897
898 case 'Sharpness':
899 switch( $val ) {
900 case 0: case 1: case 2:
901 $tags[$tag] = $this->msg( $tag, $val );
902 break;
903 default:
904 $tags[$tag] = $val;
905 break;
906 }
907 break;
908
909 case 'SubjectDistanceRange':
910 switch( $val ) {
911 case 0: case 1: case 2: case 3:
912 $tags[$tag] = $this->msg( $tag, $val );
913 break;
914 default:
915 $tags[$tag] = $val;
916 break;
917 }
918 break;
919
920 case 'GPSLatitudeRef':
921 case 'GPSDestLatitudeRef':
922 switch( $val ) {
923 case 'N': case 'S':
924 $tags[$tag] = $this->msg( 'GPSLatitude', $val );
925 break;
926 default:
927 $tags[$tag] = $val;
928 break;
929 }
930 break;
931
932 case 'GPSLongitudeRef':
933 case 'GPSDestLongitudeRef':
934 switch( $val ) {
935 case 'E': case 'W':
936 $tags[$tag] = $this->msg( 'GPSLongitude', $val );
937 break;
938 default:
939 $tags[$tag] = $val;
940 break;
941 }
942 break;
943
944 case 'GPSStatus':
945 switch( $val ) {
946 case 'A': case 'V':
947 $tags[$tag] = $this->msg( $tag, $val );
948 break;
949 default:
950 $tags[$tag] = $val;
951 break;
952 }
953 break;
954
955 case 'GPSMeasureMode':
956 switch( $val ) {
957 case 2: case 3:
958 $tags[$tag] = $this->msg( $tag, $val );
959 break;
960 default:
961 $tags[$tag] = $val;
962 break;
963 }
964 break;
965
966 case 'GPSSpeedRef':
967 case 'GPSDestDistanceRef':
968 switch( $val ) {
969 case 'K': case 'M': case 'N':
970 $tags[$tag] = $this->msg( 'GPSSpeed', $val );
971 break;
972 default:
973 $tags[$tag] = $val;
974 break;
975 }
976 break;
977
978 case 'GPSTrackRef':
979 case 'GPSImgDirectionRef':
980 case 'GPSDestBearingRef':
981 switch( $val ) {
982 case 'T': case 'M':
983 $tags[$tag] = $this->msg( 'GPSDirection', $val );
984 break;
985 default:
986 $tags[$tag] = $val;
987 break;
988 }
989 break;
990
991 case 'GPSDateStamp':
992 $tags[$tag] = $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
993 break;
994
995 // This is not in the Exif standard, just a special
996 // case for our purposes which enables wikis to wikify
997 // the make, model and software name to link to their articles.
998 case 'Make':
999 case 'Model':
1000 case 'Software':
1001 $tags[$tag] = $this->msg( $tag, '', $val );
1002 break;
1003
1004 case 'ExposureTime':
1005 // Show the pretty fraction as well as decimal version
1006 $tags[$tag] = wfMsg( 'exif-exposuretime-format',
1007 $this->formatFraction( $val ), $this->formatNum( $val ) );
1008 break;
1009
1010 case 'FNumber':
1011 $tags[$tag] = wfMsg( 'exif-fnumber-format',
1012 $this->formatNum( $val ) );
1013 break;
1014
1015 case 'FocalLength':
1016 $tags[$tag] = wfMsg( 'exif-focallength-format',
1017 $this->formatNum( $val ) );
1018 break;
1019
1020 default:
1021 $tags[$tag] = $this->formatNum( $val );
1022 break;
1023 }
1024 }
1025
1026 return $tags;
1027 }
1028
1029 /**
1030 * Convenience function for getFormattedData()
1031 *
1032 * @private
1033 *
1034 * @param $tag String: the tag name to pass on
1035 * @param $val String: the value of the tag
1036 * @param $arg String: an argument to pass ($1)
1037 * @return string A wfMsg of "exif-$tag-$val" in lower case
1038 */
1039 function msg( $tag, $val, $arg = null ) {
1040 global $wgContLang;
1041
1042 if ($val === '')
1043 $val = 'value';
1044 return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg );
1045 }
1046
1047 /**
1048 * Format a number, convert numbers from fractions into floating point
1049 * numbers
1050 *
1051 * @private
1052 *
1053 * @param $num Mixed: the value to format
1054 * @return mixed A floating point number or whatever we were fed
1055 */
1056 function formatNum( $num ) {
1057 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) )
1058 return $m[2] != 0 ? $m[1] / $m[2] : $num;
1059 else
1060 return $num;
1061 }
1062
1063 /**
1064 * Format a rational number, reducing fractions
1065 *
1066 * @private
1067 *
1068 * @param $num Mixed: the value to format
1069 * @return mixed A floating point number or whatever we were fed
1070 */
1071 function formatFraction( $num ) {
1072 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) ) {
1073 $numerator = intval( $m[1] );
1074 $denominator = intval( $m[2] );
1075 $gcd = $this->gcd( $numerator, $denominator );
1076 if( $gcd != 0 ) {
1077 // 0 shouldn't happen! ;)
1078 return $numerator / $gcd . '/' . $denominator / $gcd;
1079 }
1080 }
1081 return $this->formatNum( $num );
1082 }
1083
1084 /**
1085 * Calculate the greatest common divisor of two integers.
1086 *
1087 * @param $a Integer: FIXME
1088 * @param $b Integer: FIXME
1089 * @return int
1090 * @private
1091 */
1092 function gcd( $a, $b ) {
1093 /*
1094 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1095 // Recursive form would be:
1096 if( $b == 0 )
1097 return $a;
1098 else
1099 return gcd( $b, $a % $b );
1100 */
1101 while( $b != 0 ) {
1102 $remainder = $a % $b;
1103
1104 // tail recursion...
1105 $a = $b;
1106 $b = $remainder;
1107 }
1108 return $a;
1109 }
1110 }
1111
1112 /**
1113 * MW 1.6 compatibility
1114 */
1115 define( 'MW_EXIF_BYTE', Exif::BYTE );
1116 define( 'MW_EXIF_ASCII', Exif::ASCII );
1117 define( 'MW_EXIF_SHORT', Exif::SHORT );
1118 define( 'MW_EXIF_LONG', Exif::LONG );
1119 define( 'MW_EXIF_RATIONAL', Exif::RATIONAL );
1120 define( 'MW_EXIF_UNDEFINED', Exif::UNDEFINED );
1121 define( 'MW_EXIF_SLONG', Exif::SLONG );
1122 define( 'MW_EXIF_SRATIONAL', Exif::SRATIONAL );
1123
1124 ?>