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