* Fixed a bug where tags would be wrongly rejected ( === => == )
[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 function Exif( $file ) {
89 /**
90 * Page numbers here refer to pages in the EXIF 2.2 standard
91 *
92 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
93 */
94 $this->mExifTags = array(
95 # TIFF Rev. 6.0 Attribute Information (p22)
96 'tiff' => array(
97 # Tags relating to image structure
98 'structure' => array(
99 'ImageWidth' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Image width
100 'ImageLength' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Image height
101 'BitsPerSample' => MW_EXIF_SHORT, # Number of bits per component
102 # "When a primary image is JPEG compressed, this designation is not"
103 # "necessary and is omitted." (p23)
104 'Compression' => MW_EXIF_SHORT, # Compression scheme #p23
105 'PhotometricInterpretation' => MW_EXIF_SHORT, # Pixel composition #p23
106 'Orientation' => MW_EXIF_SHORT, # Orientation of image #p24
107 'SamplesPerPixel' => MW_EXIF_SHORT, # Number of components
108 'PlanarConfiguration' => MW_EXIF_SHORT, # Image data arrangement #p24
109 'YCbCrSubSampling' => MW_EXIF_SHORT, # Subsampling ratio of Y to C #p24
110 'YCbCrPositioning' => MW_EXIF_SHORT, # Y and C positioning #p24-25
111 'XResolution' => MW_EXIF_RATIONAL, # Image resolution in width direction
112 'YResolution' => MW_EXIF_RATIONAL, # Image resolution in height direction
113 'ResolutionUnit' => MW_EXIF_SHORT, # Unit of X and Y resolution #(p26)
114 ),
115
116 # Tags relating to recording offset
117 'offset' => array(
118 'StripOffsets' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Image data location
119 'RowsPerStrip' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Number of rows per strip
120 'StripByteCounts' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Bytes per compressed strip
121 'JPEGInterchangeFormat' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Offset to JPEG SOI
122 'JPEGInterchangeFormatLength' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Bytes of JPEG data
123 ),
124
125 # Tags relating to image data characteristics
126 'characteristics' => array(
127 'TransferFunction' => MW_EXIF_SHORT, # Transfer function
128 'WhitePoint' => MW_EXIF_RATIONAL, # White point chromaticity
129 'PrimaryChromaticities' => MW_EXIF_RATIONAL, # Chromaticities of primarities
130 'YCbCrCoefficients' => MW_EXIF_RATIONAL, # Color space transformation matrix coefficients #p27
131 'ReferenceBlackWhite' => MW_EXIF_RATIONAL # Pair of black and white reference values
132 ),
133
134 # Other tags
135 'other' => array(
136 'DateTime' => MW_EXIF_ASCII, # File change date and time
137 'ImageDescription' => MW_EXIF_ASCII, # Image title
138 'Make' => MW_EXIF_ASCII, # Image input equipment manufacturer
139 'Model' => MW_EXIF_ASCII, # Image input equipment model
140 'Software' => MW_EXIF_ASCII, # Software used
141 'Artist' => MW_EXIF_ASCII, # Person who created the image
142 'Copyright' => MW_EXIF_ASCII, # Copyright holder
143 ),
144 ),
145
146 # Exif IFD Attribute Information (p30-31)
147 'exif' => array(
148 # Tags relating to version
149 'version' => array(
150 # TODO: NOTE: Nonexistence of this field is taken to mean nonconformance
151 # to the EXIF 2.1 AND 2.2 standards
152 'ExifVersion' => MW_EXIF_UNDEFINED, # Exif version
153 'FlashpixVersion' => MW_EXIF_UNDEFINED, # Supported Flashpix version #p32
154 ),
155
156 # Tags relating to Image Data Characteristics
157 'characteristics' => array(
158 'ColorSpace' => MW_EXIF_SHORT, # Color space information #p32
159 ),
160
161 # Tags relating to image configuration
162 'configuration' => array(
163 'ComponentsConfiguration' => MW_EXIF_UNDEFINED, # Meaning of each component #p33
164 'CompressedBitsPerPixel' => MW_EXIF_RATIONAL, # Image compression mode
165 'PixelYDimension' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Valid image width
166 'PixelXDimension' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Valind image height
167 ),
168
169 # Tags relating to related user information
170 'user' => array(
171 'MakerNote' => MW_EXIF_UNDEFINED, # Manufacturer notes
172 'UserComment' => MW_EXIF_UNDEFINED, # User comments #p34
173 ),
174
175 # Tags relating to related file information
176 'related' => array(
177 'RelatedSoundFile' => MW_EXIF_ASCII, # Related audio file
178 ),
179
180 # Tags relating to date and time
181 'dateandtime' => array(
182 'DateTimeOriginal' => MW_EXIF_ASCII, # Date and time of original data generation #p36
183 'DateTimeDigitized' => MW_EXIF_ASCII, # Date and time of original data generation
184 'SubSecTime' => MW_EXIF_ASCII, # DateTime subseconds
185 'SubSecTimeOriginal' => MW_EXIF_ASCII, # DateTimeOriginal subseconds
186 'SubSecTimeDigitized' => MW_EXIF_ASCII, # DateTimeDigitized subseconds
187 ),
188
189 # Tags relating to picture-taking conditions (p31)
190 'conditions' => array(
191 'ExposureTime' => MW_EXIF_RATIONAL, # Exposure time
192 'FNumber' => MW_EXIF_RATIONAL, # F Number
193 'ExposureProgram' => MW_EXIF_SHORT, # Exposure Program #p38
194 'SpectralSensitivity' => MW_EXIF_ASCII, # Spectral sensitivity
195 'ISOSpeedRatings' => MW_EXIF_SHORT, # ISO speed rating
196 'OECF' => MW_EXIF_UNDEFINED, # Optoelectronic conversion factor
197 'ShutterSpeedValue' => MW_EXIF_SRATIONAL, # Shutter speed
198 'ApertureValue' => MW_EXIF_RATIONAL, # Aperture
199 'BrightnessValue' => MW_EXIF_SRATIONAL, # Brightness
200 'ExposureBiasValue' => MW_EXIF_SRATIONAL, # Exposure bias
201 'MaxApertureValue' => MW_EXIF_RATIONAL, # Maximum land aperture
202 'SubjectDistance' => MW_EXIF_RATIONAL, # Subject distance
203 'MeteringMode' => MW_EXIF_SHORT, # Metering mode #p40
204 'LightSource' => MW_EXIF_SHORT, # Light source #p40-41
205 'Flash' => MW_EXIF_SHORT, # Flash #p41-42
206 'FocalLength' => MW_EXIF_RATIONAL, # Lens focal length
207 'SubjectArea' => MW_EXIF_SHORT, # Subject area
208 'FlashEnergy' => MW_EXIF_RATIONAL, # Flash energy
209 'SpatialFrequencyResponse' => MW_EXIF_UNDEFINED, # Spatial frequency response
210 'FocalPlaneXResolution' => MW_EXIF_RATIONAL, # Focal plane X resolution
211 'FocalPlaneYResolution' => MW_EXIF_RATIONAL, # Focal plane Y resolution
212 'FocalPlaneResolutionUnit' => MW_EXIF_SHORT, # Focal plane resolution unit #p46
213 'SubjectLocation' => MW_EXIF_SHORT, # Subject location
214 'ExposureIndex' => MW_EXIF_RATIONAL, # Exposure index
215 'SensingMethod' => MW_EXIF_SHORT, # Sensing method #p46
216 'FileSource' => MW_EXIF_UNDEFINED, # File source #p47
217 'SceneType' => MW_EXIF_UNDEFINED, # Scene type #p47
218 'CFAPattern' => MW_EXIF_UNDEFINED, # CFA pattern
219 'CustomRendered' => MW_EXIF_SHORT, # Custom image processing #p48
220 'ExposureMode' => MW_EXIF_SHORT, # Exposure mode #p48
221 'WhiteBalance' => MW_EXIF_SHORT, # White Balance #p49
222 'DigitalZoomRatio' => MW_EXIF_RATIONAL, # Digital zoom ration
223 'FocalLengthIn35mmFilm' => MW_EXIF_SHORT, # Focal length in 35 mm film
224 'SceneCaptureType' => MW_EXIF_SHORT, # Scene capture type #p49
225 'GainControl' => MW_EXIF_RATIONAL, # Scene control #p49-50
226 'Contrast' => MW_EXIF_SHORT, # Contrast #p50
227 'Saturation' => MW_EXIF_SHORT, # Saturation #p50
228 'Sharpness' => MW_EXIF_SHORT, # Sharpness #p50
229 'DeviceSettingDescription' => MW_EXIF_UNDEFINED, # Desice settings description
230 'SubjectDistanceRange' => MW_EXIF_SHORT, # Subject distance range #p51
231 ),
232
233 'other' => array(
234 'ImageUniqueID' => MW_EXIF_ASCII, # Unique image ID
235 ),
236 ),
237
238 # GPS Attribute Information (p52)
239 'gps' => array(
240 'GPSVersionID' => MW_EXIF_BYTE, # GPS tag version
241 'GPSLatitudeRef' => MW_EXIF_ASCII, # North or South Latitude #p52-53
242 'GPSLatitude' => MW_EXIF_RATIONAL, # Latitude
243 'GPSLongitudeRef' => MW_EXIF_ASCII, # East or West Longitude #p53
244 'GPSLongitude' => MW_EXIF_RATIONAL, # Longitude
245 'GPSAltitudeRef' => MW_EXIF_BYTE, # Altitude reference
246 'GPSAltitude' => MW_EXIF_RATIONAL, # Altitude
247 'GPSTimeStamp' => MW_EXIF_RATIONAL, # GPS time (atomic clock)
248 'GPSSatellites' => MW_EXIF_ASCII, # Satellites used for measurement
249 'GPSStatus' => MW_EXIF_ASCII, # Receiver status #p54
250 'GPSMeasureMode' => MW_EXIF_ASCII, # Measurement mode #p54-55
251 'GPSDOP' => MW_EXIF_RATIONAL, # Measurement precision
252 'GPSSpeedRef' => MW_EXIF_ASCII, # Speed unit #p55
253 'GPSSpeed' => MW_EXIF_RATIONAL, # Speed of GPS receiver
254 'GPSTrackRef' => MW_EXIF_ASCII, # Reference for direction of movement #p55
255 'GPSTrack' => MW_EXIF_RATIONAL, # Direction of movement
256 'GPSImgDirectionRef' => MW_EXIF_ASCII, # Reference for direction of image #p56
257 'GPSImgDirection' => MW_EXIF_RATIONAL, # Direction of image
258 'GPSMapDatum' => MW_EXIF_ASCII, # Geodetic survey data used
259 'GPSDestLatitudeRef' => MW_EXIF_ASCII, # Reference for latitude of destination #p56
260 'GPSDestLatitude' => MW_EXIF_RATIONAL, # Latitude destination
261 'GPSDestLongitudeRef' => MW_EXIF_ASCII, # Reference for longitude of destination #p57
262 'GPSDestLongitude' => MW_EXIF_RATIONAL, # Longitude of destination
263 'GPSDestBearingRef' => MW_EXIF_ASCII, # Reference for bearing of destination #p57
264 'GPSDestBearing' => MW_EXIF_RATIONAL, # Bearing of destination
265 'GPSDestDistanceRef' => MW_EXIF_ASCII, # Reference for distance to destination #p57-58
266 'GPSDestDistance' => MW_EXIF_RATIONAL, # Distance to destination
267 'GPSProcessingMethod' => MW_EXIF_UNDEFINED, # Name of GPS processing method
268 'GPSAreaInformation' => MW_EXIF_UNDEFINED, # Name of GPS area
269 'GPSDateStamp' => MW_EXIF_ASCII, # GPS date
270 'GPSDifferential' => MW_EXIF_SHORT, # GPS differential correction
271 ),
272 );
273
274 $this->makeFlatExifTags();
275 wfSuppressWarnings();
276 $this->mRawExifData = exif_read_data( $file );
277 wfRestoreWarnings();
278 $this->makeFilteredData();
279 $this->makeFormattedData();
280 }
281
282 /**#@+
283 * @access private
284 */
285 /**
286 * Generate a flat list of the exif tags
287 */
288 function makeFlatExifTags() {
289 $this->extractTags( $this->mExifTags );
290 }
291
292 /**
293 * A recursing extractor function used by makeFlatExifTags()
294 *
295 * Note: This used to use an array_walk function, but it made PHP5
296 * segfault, see `cvs diff -u -r 1.4 -r 1.5 Exif.php`
297 */
298 function extractTags( &$tagset ) {
299 foreach( $tagset as $key => $val ) {
300 if( is_array( $val ) ) {
301 $this->extractTags( $val );
302 } else {
303 $this->mFlatExifTags[$key] = $val;
304 }
305 }
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 * @param mixed $in The input value to check
382 * @return bool
383 */
384 function isByte( $in ) {
385 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 255 ) {
386 $this->debug( $in, __FUNCTION__, true );
387 return true;
388 } else {
389 $this->debug( $in, __FUNCTION__, false );
390 return false;
391 }
392 }
393
394 function isASCII( $in ) {
395 if ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) {
396 $this->debug( $in, __FUNCTION__, 'found a character not in our whitelist' );
397 return false;
398 }
399
400 if ( preg_match( "/^\s*$/", $in ) ) {
401 $this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' );
402 return false;
403 }
404
405 if ( strlen($in) > 1024 ) { // TODO: This might not be according to the spec
406 $this->debug( $in, __FUNCTION__, 'input was too long' );
407 return false;
408 }
409
410 return true;
411 }
412
413 function isShort( $in ) {
414 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 65536 ) {
415 $this->debug( $in, __FUNCTION__, true );
416 return true;
417 } else {
418 $this->debug( $in, __FUNCTION__, false );
419 return false;
420 }
421 }
422
423 function isLong( $in ) {
424 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 4294967296 ) {
425 $this->debug( $in, __FUNCTION__, true );
426 return true;
427 } else {
428 $this->debug( $in, __FUNCTION__, false );
429 return false;
430 }
431 }
432
433 function isRational( $in ) {
434 if ( @preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
435 return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
436 } else {
437 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
438 return false;
439 }
440 }
441
442 function isUndefined( $in ) {
443 if ( preg_match( "/^\d{4}$/", $in ) ) { // Allow ExifVersion and FlashpixVersion
444 $this->debug( $in, __FUNCTION__, true );
445 return true;
446 } else {
447 $this->debug( $in, __FUNCTION__, false );
448 return false;
449 }
450 }
451
452 function isSlong( $in ) {
453 if ( $this->isLong( abs( $in ) ) ) {
454 $this->debug( $in, __FUNCTION__, true );
455 return true;
456 } else {
457 $this->debug( $in, __FUNCTION__, false );
458 return false;
459 }
460 }
461
462 function isSrational( $in ) {
463 if ( preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
464 return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
465 } else {
466 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
467 return false;
468 }
469 }
470 /**#@-*/
471
472 /**
473 * Validates if a tag has a legal value according to the Exif spec
474 *
475 * @param string $tag The tag to check
476 * @param mixed $val The value of the tag
477 * @return bool
478 */
479 function validate( $tag, $val ) {
480 $debug = "tag is '$tag'";
481 // Fucks up if not typecast
482 switch( (string)$this->mFlatExifTags[$tag] ) {
483 case (string)MW_EXIF_BYTE:
484 $this->debug( $val, __FUNCTION__, $debug );
485 return $this->isByte( $val );
486 case (string)MW_EXIF_ASCII:
487 $this->debug( $val, __FUNCTION__, $debug );
488 return $this->isASCII( $val );
489 case (string)MW_EXIF_SHORT:
490 $this->debug( $val, __FUNCTION__, $debug );
491 return $this->isShort( $val );
492 case (string)MW_EXIF_LONG:
493 $this->debug( $val, __FUNCTION__, $debug );
494 return $this->isLong( $val );
495 case (string)MW_EXIF_RATIONAL:
496 $this->debug( $val, __FUNCTION__, $debug );
497 return $this->isRational( $val );
498 case (string)MW_EXIF_UNDEFINED:
499 $this->debug( $val, __FUNCTION__, $debug );
500 return $this->isUndefined( $val );
501 case (string)MW_EXIF_SLONG:
502 $this->debug( $val, __FUNCTION__, $debug );
503 return $this->isSlong( $val );
504 case (string)MW_EXIF_SRATIONAL:
505 $this->debug( $val, __FUNCTION__, $debug );
506 return $this->isSrational( $val );
507 case (string)MW_EXIF_SHORT.','.MW_EXIF_LONG:
508 $this->debug( $val, __FUNCTION__, $debug );
509 return $this->isShort( $val ) || $this->isLong( $val );
510 default:
511 $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
512 return false;
513 }
514 }
515
516 /**
517 * Conviniance function for debugging output
518 *
519 * @param mixed $in
520 * @param string $fname
521 * @param mixed $action
522 */
523 function debug( $in, $fname, $action = null ) {
524 $type = gettype( $in );
525 $class = ucfirst( __CLASS__ );
526 if ( $type === 'array' )
527 $in = print_r( $in, true );
528
529 if ( $action === true )
530 wfDebug( "$class::$fname: accepted: '$in' (type: $type)\n");
531 elseif ( $action === false )
532 wfDebug( "$class::$fname: rejected: '$in' (type: $type)\n");
533 elseif ( $action === null )
534 wfDebug( "$class::$fname: input was: '$in' (type: $type)\n");
535 else
536 wfDebug( "$class::$fname: $action (type: $type; content: '$in')\n");
537 }
538
539 }
540
541 /**
542 * @package MediaWiki
543 * @subpackage Metadata
544 */
545 class FormatExif {
546 /**
547 * The Exif data to format
548 *
549 * @var array
550 * @access private
551 */
552 var $mExif;
553
554 /**
555 * Constructor
556 *
557 * @param array $exif The Exif data to format ( as returned by
558 * Exif::getFilteredData() )
559 */
560 function FormatExif( $exif ) {
561 $this->mExif = $exif;
562 }
563
564 /**
565 * Numbers given by Exif user agents are often magical, that is they
566 * should be replaced by a detailed explanation depending on their
567 * value which most of the time are plain integers. This function
568 * formats Exif values into human readable form.
569 *
570 * @return array
571 */
572 function getFormattedData() {
573 global $wgLang;
574
575 $tags =& $this->mExif;
576
577 foreach( $tags as $tag => $val ) {
578 switch( $tag ) {
579 case 'Compression':
580 switch( $val ) {
581 case 1: case 6:
582 $tags[$tag] = $this->msg( $tag, $val );
583 break;
584 default:
585 $tags[$tag] = $val;
586 break;
587 }
588 break;
589
590 case 'PhotometricInterpretation':
591 switch( $val ) {
592 case 2: case 6:
593 $tags[$tag] = $this->msg( $tag, $val );
594 break;
595 default:
596 $tags[$tag] = $val;
597 break;
598 }
599 break;
600
601 case 'Orientation':
602 switch( $val ) {
603 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
604 $tags[$tag] = $this->msg( $tag, $val );
605 break;
606 default:
607 $tags[$tag] = $val;
608 break;
609 }
610 break;
611
612 case 'PlanarConfiguration':
613 switch( $val ) {
614 case 1: case 2:
615 $tags[$tag] = $this->msg( $tag, $val );
616 break;
617 default:
618 $tags[$tag] = $val;
619 break;
620 }
621 break;
622
623 // TODO: YCbCrSubSampling
624 // TODO: YCbCrPositioning
625 // TODO: If this field does not exists use 2
626 case 'ResolutionUnit': #p26
627 switch( $val ) {
628 case 2: case 3:
629 $tags[$tag] = $this->msg( $tag, $val );
630 break;
631 default:
632 $tags[$tag] = $val;
633 break;
634 }
635 break;
636
637 // TODO: YCbCrCoefficients #p27 (see annex E)
638 case 'ExifVersion': case 'FlashpixVersion':
639 $tags[$tag] = "$val"/100;
640 break;
641
642 case 'ColorSpace':
643 switch( $val ) {
644 case 1: case 'FFFF.H':
645 $tags[$tag] = $this->msg( $tag, $val );
646 break;
647 default:
648 $tags[$tag] = $val;
649 break;
650 }
651 break;
652
653 case 'ComponentsConfiguration':
654 switch( $val ) {
655 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
656 $tags[$tag] = $this->msg( $tag, $val );
657 break;
658 default:
659 $tags[$tag] = $val;
660 break;
661 }
662 break;
663
664 case 'DateTime':
665 case 'DateTimeOriginal':
666 case 'DateTimeDigitized':
667 $tags[$tag] = $wgLang->timeanddate( wfTimestamp(TS_MW, $val) );
668 break;
669
670 case 'ExposureProgram':
671 switch( $val ) {
672 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
673 $tags[$tag] = $this->msg( $tag, $val );
674 break;
675 default:
676 $tags[$tag] = $val;
677 break;
678 }
679 break;
680
681 case 'MeteringMode':
682 switch( $val ) {
683 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
684 $tags[$tag] = $this->msg( $tag, $val );
685 break;
686 default:
687 $tags[$tag] = $val;
688 break;
689 }
690 break;
691
692 case 'LightSource':
693 switch( $val ) {
694 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
695 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
696 case 21: case 22: case 23: case 24: case 255:
697 $tags[$tag] = $this->msg( $tag, $val );
698 break;
699 default:
700 $tags[$tag] = $val;
701 break;
702 }
703 break;
704
705 // TODO: Flash
706 case 'FocalPlaneResolutionUnit':
707 switch( $val ) {
708 case 2:
709 $tags[$tag] = $this->msg( $tag, $val );
710 break;
711 default:
712 $tags[$tag] = $val;
713 break;
714 }
715 break;
716
717 case 'SensingMethod':
718 switch( $val ) {
719 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
720 $tags[$tag] = $this->msg( $tag, $val );
721 break;
722 default:
723 $tags[$tag] = $val;
724 break;
725 }
726 break;
727
728 case 'FileSource':
729 switch( $val ) {
730 case 3:
731 $tags[$tag] = $this->msg( $tag, $val );
732 break;
733 default:
734 $tags[$tag] = $val;
735 break;
736 }
737 break;
738
739 case 'SceneType':
740 switch( $val ) {
741 case 1:
742 $tags[$tag] = $this->msg( $tag, $val );
743 break;
744 default:
745 $tags[$tag] = $val;
746 break;
747 }
748 break;
749
750 case 'CustomRendered':
751 switch( $val ) {
752 case 0: case 1:
753 $tags[$tag] = $this->msg( $tag, $val );
754 break;
755 default:
756 $tags[$tag] = $val;
757 break;
758 }
759 break;
760
761 case 'ExposureMode':
762 switch( $val ) {
763 case 0: case 1: case 2:
764 $tags[$tag] = $this->msg( $tag, $val );
765 break;
766 default:
767 $tags[$tag] = $val;
768 break;
769 }
770 break;
771
772 case 'WhiteBalance':
773 switch( $val ) {
774 case 0: case 1:
775 $tags[$tag] = $this->msg( $tag, $val );
776 break;
777 default:
778 $tags[$tag] = $val;
779 break;
780 }
781 break;
782
783 case 'SceneCaptureType':
784 switch( $val ) {
785 case 0: case 1: case 2: case 3:
786 $tags[$tag] = $this->msg( $tag, $val );
787 break;
788 default:
789 $tags[$tag] = $val;
790 break;
791 }
792 break;
793
794 case 'GainControl':
795 switch( $val ) {
796 case 0: case 1: case 2: case 3: case 4:
797 $tags[$tag] = $this->msg( $tag, $val );
798 break;
799 default:
800 $tags[$tag] = $val;
801 break;
802 }
803 break;
804
805 case 'Contrast':
806 switch( $val ) {
807 case 0: case 1: case 2:
808 $tags[$tag] = $this->msg( $tag, $val );
809 break;
810 default:
811 $tags[$tag] = $val;
812 break;
813 }
814 break;
815
816 case 'Saturation':
817 switch( $val ) {
818 case 0: case 1: case 2:
819 $tags[$tag] = $this->msg( $tag, $val );
820 break;
821 default:
822 $tags[$tag] = $val;
823 break;
824 }
825 break;
826
827 case 'Sharpness':
828 switch( $val ) {
829 case 0: case 1: case 2:
830 $tags[$tag] = $this->msg( $tag, $val );
831 break;
832 default:
833 $tags[$tag] = $val;
834 break;
835 }
836 break;
837
838 case 'SubjectDistanceRange':
839 switch( $val ) {
840 case 0: case 1: case 2: case 3:
841 $tags[$tag] = $this->msg( $tag, $val );
842 break;
843 default:
844 $tags[$tag] = $val;
845 break;
846 }
847 break;
848
849 case 'GPSLatitudeRef':
850 case 'GPSDestLatitudeRef':
851 switch( $val ) {
852 case 'N': case 'S':
853 $tags[$tag] = $this->msg( 'GPSLatitude', $val );
854 break;
855 default:
856 $tags[$tag] = $val;
857 break;
858 }
859 break;
860
861 case 'GPSLongitudeRef':
862 case 'GPSDestLongitudeRef':
863 switch( $val ) {
864 case 'E': case 'W':
865 $tags[$tag] = $this->msg( 'GPSLongitude', $val );
866 break;
867 default:
868 $tags[$tag] = $val;
869 break;
870 }
871 break;
872
873 case 'GPSStatus':
874 switch( $val ) {
875 case 'A': case 'V':
876 $tags[$tag] = $this->msg( $tag, $val );
877 break;
878 default:
879 $tags[$tag] = $val;
880 break;
881 }
882 break;
883
884 case 'GPSMeasureMode':
885 switch( $val ) {
886 case 2: case 3:
887 $tags[$tag] = $this->msg( $tag, $val );
888 break;
889 default:
890 $tags[$tag] = $val;
891 break;
892 }
893 break;
894
895 case 'GPSSpeedRef':
896 case 'GPSDestDistanceRef':
897 switch( $val ) {
898 case 'K': case 'M': case 'N':
899 $tags[$tag] = $this->msg( 'GPSSpeed', $val );
900 break;
901 default:
902 $tags[$tag] = $val;
903 break;
904 }
905 break;
906
907 case 'GPSTrackRef':
908 case 'GPSImgDirectionRef':
909 case 'GPSDestBearingRef':
910 switch( $val ) {
911 case 'T': case 'M':
912 $tags[$tag] = $this->msg( 'GPSDirection', $val );
913 break;
914 default:
915 $tags[$tag] = $val;
916 break;
917 }
918 break;
919
920 case 'GPSDateStamp':
921 $tags[$tag] = $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
922 break;
923
924 // This is not in the Exif standard, just a special
925 // case for our purposes which enables wikis to wikify
926 // the make, model and software name to link to their articles.
927 case 'Make':
928 case 'Model':
929 case 'Software':
930 $tags[$tag] = wfMsg( strtolower( "exif-$tag-value" ), $val );
931 break;
932 default:
933 if ( preg_match( '/^(\d+)\/(\d+)$/', $val, $m ) ) {
934 $tags[$tag] = $m[2] != 0 ? $m[1]/$m[2] : $val;
935 break;
936 }
937 $tags[$tag] = $val;
938 break;
939 }
940 }
941
942 return $tags;
943 }
944
945 /**
946 * Conviniance function for format()
947 *
948 * @param string $tag The tag name to pass on
949 * @param string $val The value of the tag
950 * @return string A wfMsg of "exif-$tag-$val" in lower case
951 */
952 function msg( $tag, $val ) {
953 return wfMsg( strtolower("exif-$tag-$val") );
954 }
955 }