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