* Fix notice on search index update due to non-array
[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 if ( strlen($in) > 1024 ) { // TODO: This might not be according to the spec
408 $this->debug( $in, __FUNCTION__, 'input was too long' );
409 return false;
410 }
411
412 return true;
413 }
414
415 function isShort( $in ) {
416 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 65536 ) {
417 $this->debug( $in, __FUNCTION__, true );
418 return true;
419 } else {
420 $this->debug( $in, __FUNCTION__, false );
421 return false;
422 }
423 }
424
425 function isLong( $in ) {
426 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 4294967296 ) {
427 $this->debug( $in, __FUNCTION__, true );
428 return true;
429 } else {
430 $this->debug( $in, __FUNCTION__, false );
431 return false;
432 }
433 }
434
435 function isRational( $in ) {
436 if ( @preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
437 return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
438 } else {
439 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
440 return false;
441 }
442 }
443
444 function isUndefined( $in ) {
445 if ( preg_match( "/^\d{4}$/", $in ) ) { // Allow ExifVersion and FlashpixVersion
446 $this->debug( $in, __FUNCTION__, true );
447 return true;
448 } else {
449 $this->debug( $in, __FUNCTION__, false );
450 return false;
451 }
452 }
453
454 function isSlong( $in ) {
455 if ( $this->isLong( abs( $in ) ) ) {
456 $this->debug( $in, __FUNCTION__, true );
457 return true;
458 } else {
459 $this->debug( $in, __FUNCTION__, false );
460 return false;
461 }
462 }
463
464 function isSrational( $in ) {
465 if ( preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
466 return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
467 } else {
468 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
469 return false;
470 }
471 }
472 /**#@-*/
473
474 /**
475 * Validates if a tag has a legal value according to the Exif spec
476 *
477 * @access private
478 *
479 * @param string $tag The tag to check
480 * @param mixed $val The value of the tag
481 * @return bool
482 */
483 function validate( $tag, $val ) {
484 $debug = "tag is '$tag'";
485 // Fucks up if not typecast
486 switch( (string)$this->mFlatExifTags[$tag] ) {
487 case (string)MW_EXIF_BYTE:
488 $this->debug( $val, __FUNCTION__, $debug );
489 return $this->isByte( $val );
490 case (string)MW_EXIF_ASCII:
491 $this->debug( $val, __FUNCTION__, $debug );
492 return $this->isASCII( $val );
493 case (string)MW_EXIF_SHORT:
494 $this->debug( $val, __FUNCTION__, $debug );
495 return $this->isShort( $val );
496 case (string)MW_EXIF_LONG:
497 $this->debug( $val, __FUNCTION__, $debug );
498 return $this->isLong( $val );
499 case (string)MW_EXIF_RATIONAL:
500 $this->debug( $val, __FUNCTION__, $debug );
501 return $this->isRational( $val );
502 case (string)MW_EXIF_UNDEFINED:
503 $this->debug( $val, __FUNCTION__, $debug );
504 return $this->isUndefined( $val );
505 case (string)MW_EXIF_SLONG:
506 $this->debug( $val, __FUNCTION__, $debug );
507 return $this->isSlong( $val );
508 case (string)MW_EXIF_SRATIONAL:
509 $this->debug( $val, __FUNCTION__, $debug );
510 return $this->isSrational( $val );
511 case (string)MW_EXIF_SHORT.','.MW_EXIF_LONG:
512 $this->debug( $val, __FUNCTION__, $debug );
513 return $this->isShort( $val ) || $this->isLong( $val );
514 default:
515 $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
516 return false;
517 }
518 }
519
520 /**
521 * Conviniance function for debugging output
522 *
523 * @access private
524 *
525 * @param mixed $in
526 * @param string $fname
527 * @param mixed $action
528 */
529 function debug( $in, $fname, $action = null ) {
530 $type = gettype( $in );
531 $class = ucfirst( __CLASS__ );
532 if ( $type === 'array' )
533 $in = print_r( $in, true );
534
535 if ( $action === true )
536 wfDebug( "$class::$fname: accepted: '$in' (type: $type)\n");
537 elseif ( $action === false )
538 wfDebug( "$class::$fname: rejected: '$in' (type: $type)\n");
539 elseif ( $action === null )
540 wfDebug( "$class::$fname: input was: '$in' (type: $type)\n");
541 else
542 wfDebug( "$class::$fname: $action (type: $type; content: '$in')\n");
543 }
544
545 }
546
547 /**
548 * @package MediaWiki
549 * @subpackage Metadata
550 */
551 class FormatExif {
552 /**
553 * The Exif data to format
554 *
555 * @var array
556 * @access private
557 */
558 var $mExif;
559
560 /**
561 * Constructor
562 *
563 * @param array $exif The Exif data to format ( as returned by
564 * Exif::getFilteredData() )
565 */
566 function FormatExif( $exif ) {
567 $this->mExif = $exif;
568 }
569
570 /**
571 * Numbers given by Exif user agents are often magical, that is they
572 * should be replaced by a detailed explanation depending on their
573 * value which most of the time are plain integers. This function
574 * formats Exif values into human readable form.
575 *
576 * @return array
577 */
578 function getFormattedData() {
579 global $wgLang;
580
581 $tags =& $this->mExif;
582
583 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
584 unset( $tags['ResolutionUnit'] );
585
586 foreach( $tags as $tag => $val ) {
587 switch( $tag ) {
588 case 'Compression':
589 switch( $val ) {
590 case 1: case 6:
591 $tags[$tag] = $this->msg( $tag, $val );
592 break;
593 default:
594 $tags[$tag] = $val;
595 break;
596 }
597 break;
598
599 case 'PhotometricInterpretation':
600 switch( $val ) {
601 case 2: case 6:
602 $tags[$tag] = $this->msg( $tag, $val );
603 break;
604 default:
605 $tags[$tag] = $val;
606 break;
607 }
608 break;
609
610 case 'Orientation':
611 switch( $val ) {
612 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
613 $tags[$tag] = $this->msg( $tag, $val );
614 break;
615 default:
616 $tags[$tag] = $val;
617 break;
618 }
619 break;
620
621 case 'PlanarConfiguration':
622 switch( $val ) {
623 case 1: case 2:
624 $tags[$tag] = $this->msg( $tag, $val );
625 break;
626 default:
627 $tags[$tag] = $val;
628 break;
629 }
630 break;
631
632 // TODO: YCbCrSubSampling
633 // TODO: YCbCrPositioning
634
635 case 'XResolution':
636 case 'YResolution':
637 switch( $resolutionunit ) {
638 case 2:
639 $tags[$tag] = $this->msg( 'XYResolution', 'i', $this->formatNum( $val ) );
640 break;
641 case 3:
642 $this->msg( 'XYResolution', 'c', $this->formatNum( $val ) );
643 break;
644 default:
645 $tags[$tag] = $val;
646 break;
647 }
648 break;
649
650 // TODO: YCbCrCoefficients #p27 (see annex E)
651 case 'ExifVersion': case 'FlashpixVersion':
652 $tags[$tag] = "$val"/100;
653 break;
654
655 case 'ColorSpace':
656 switch( $val ) {
657 case 1: case 'FFFF.H':
658 $tags[$tag] = $this->msg( $tag, $val );
659 break;
660 default:
661 $tags[$tag] = $val;
662 break;
663 }
664 break;
665
666 case 'ComponentsConfiguration':
667 switch( $val ) {
668 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
669 $tags[$tag] = $this->msg( $tag, $val );
670 break;
671 default:
672 $tags[$tag] = $val;
673 break;
674 }
675 break;
676
677 case 'DateTime':
678 case 'DateTimeOriginal':
679 case 'DateTimeDigitized':
680 $tags[$tag] = $wgLang->timeanddate( wfTimestamp(TS_MW, $val) );
681 break;
682
683 case 'ExposureProgram':
684 switch( $val ) {
685 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
686 $tags[$tag] = $this->msg( $tag, $val );
687 break;
688 default:
689 $tags[$tag] = $val;
690 break;
691 }
692 break;
693
694 case 'SubjectDistance':
695 $tags[$tag] = $this->msg( $tag, '', $this->formatNum( $val ) );
696 break;
697
698 case 'MeteringMode':
699 switch( $val ) {
700 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
701 $tags[$tag] = $this->msg( $tag, $val );
702 break;
703 default:
704 $tags[$tag] = $val;
705 break;
706 }
707 break;
708
709 case 'LightSource':
710 switch( $val ) {
711 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
712 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
713 case 21: case 22: case 23: case 24: case 255:
714 $tags[$tag] = $this->msg( $tag, $val );
715 break;
716 default:
717 $tags[$tag] = $val;
718 break;
719 }
720 break;
721
722 // TODO: Flash
723 case 'FocalPlaneResolutionUnit':
724 switch( $val ) {
725 case 2:
726 $tags[$tag] = $this->msg( $tag, $val );
727 break;
728 default:
729 $tags[$tag] = $val;
730 break;
731 }
732 break;
733
734 case 'SensingMethod':
735 switch( $val ) {
736 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
737 $tags[$tag] = $this->msg( $tag, $val );
738 break;
739 default:
740 $tags[$tag] = $val;
741 break;
742 }
743 break;
744
745 case 'FileSource':
746 switch( $val ) {
747 case 3:
748 $tags[$tag] = $this->msg( $tag, $val );
749 break;
750 default:
751 $tags[$tag] = $val;
752 break;
753 }
754 break;
755
756 case 'SceneType':
757 switch( $val ) {
758 case 1:
759 $tags[$tag] = $this->msg( $tag, $val );
760 break;
761 default:
762 $tags[$tag] = $val;
763 break;
764 }
765 break;
766
767 case 'CustomRendered':
768 switch( $val ) {
769 case 0: case 1:
770 $tags[$tag] = $this->msg( $tag, $val );
771 break;
772 default:
773 $tags[$tag] = $val;
774 break;
775 }
776 break;
777
778 case 'ExposureMode':
779 switch( $val ) {
780 case 0: case 1: case 2:
781 $tags[$tag] = $this->msg( $tag, $val );
782 break;
783 default:
784 $tags[$tag] = $val;
785 break;
786 }
787 break;
788
789 case 'WhiteBalance':
790 switch( $val ) {
791 case 0: case 1:
792 $tags[$tag] = $this->msg( $tag, $val );
793 break;
794 default:
795 $tags[$tag] = $val;
796 break;
797 }
798 break;
799
800 case 'SceneCaptureType':
801 switch( $val ) {
802 case 0: case 1: case 2: case 3:
803 $tags[$tag] = $this->msg( $tag, $val );
804 break;
805 default:
806 $tags[$tag] = $val;
807 break;
808 }
809 break;
810
811 case 'GainControl':
812 switch( $val ) {
813 case 0: case 1: case 2: case 3: case 4:
814 $tags[$tag] = $this->msg( $tag, $val );
815 break;
816 default:
817 $tags[$tag] = $val;
818 break;
819 }
820 break;
821
822 case 'Contrast':
823 switch( $val ) {
824 case 0: case 1: case 2:
825 $tags[$tag] = $this->msg( $tag, $val );
826 break;
827 default:
828 $tags[$tag] = $val;
829 break;
830 }
831 break;
832
833 case 'Saturation':
834 switch( $val ) {
835 case 0: case 1: case 2:
836 $tags[$tag] = $this->msg( $tag, $val );
837 break;
838 default:
839 $tags[$tag] = $val;
840 break;
841 }
842 break;
843
844 case 'Sharpness':
845 switch( $val ) {
846 case 0: case 1: case 2:
847 $tags[$tag] = $this->msg( $tag, $val );
848 break;
849 default:
850 $tags[$tag] = $val;
851 break;
852 }
853 break;
854
855 case 'SubjectDistanceRange':
856 switch( $val ) {
857 case 0: case 1: case 2: case 3:
858 $tags[$tag] = $this->msg( $tag, $val );
859 break;
860 default:
861 $tags[$tag] = $val;
862 break;
863 }
864 break;
865
866 case 'GPSLatitudeRef':
867 case 'GPSDestLatitudeRef':
868 switch( $val ) {
869 case 'N': case 'S':
870 $tags[$tag] = $this->msg( 'GPSLatitude', $val );
871 break;
872 default:
873 $tags[$tag] = $val;
874 break;
875 }
876 break;
877
878 case 'GPSLongitudeRef':
879 case 'GPSDestLongitudeRef':
880 switch( $val ) {
881 case 'E': case 'W':
882 $tags[$tag] = $this->msg( 'GPSLongitude', $val );
883 break;
884 default:
885 $tags[$tag] = $val;
886 break;
887 }
888 break;
889
890 case 'GPSStatus':
891 switch( $val ) {
892 case 'A': case 'V':
893 $tags[$tag] = $this->msg( $tag, $val );
894 break;
895 default:
896 $tags[$tag] = $val;
897 break;
898 }
899 break;
900
901 case 'GPSMeasureMode':
902 switch( $val ) {
903 case 2: case 3:
904 $tags[$tag] = $this->msg( $tag, $val );
905 break;
906 default:
907 $tags[$tag] = $val;
908 break;
909 }
910 break;
911
912 case 'GPSSpeedRef':
913 case 'GPSDestDistanceRef':
914 switch( $val ) {
915 case 'K': case 'M': case 'N':
916 $tags[$tag] = $this->msg( 'GPSSpeed', $val );
917 break;
918 default:
919 $tags[$tag] = $val;
920 break;
921 }
922 break;
923
924 case 'GPSTrackRef':
925 case 'GPSImgDirectionRef':
926 case 'GPSDestBearingRef':
927 switch( $val ) {
928 case 'T': case 'M':
929 $tags[$tag] = $this->msg( 'GPSDirection', $val );
930 break;
931 default:
932 $tags[$tag] = $val;
933 break;
934 }
935 break;
936
937 case 'GPSDateStamp':
938 $tags[$tag] = $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
939 break;
940
941 // This is not in the Exif standard, just a special
942 // case for our purposes which enables wikis to wikify
943 // the make, model and software name to link to their articles.
944 case 'Make':
945 case 'Model':
946 case 'Software':
947 $tags[$tag] = $this->msg( $tag, '', $val );
948 break;
949 default:
950 $tags[$tag] = $this->formatNum( $val );
951 break;
952 }
953 }
954
955 return $tags;
956 }
957
958 /**
959 * Conviniance function for getFormattedData()
960 *
961 * @access private
962 *
963 * @param string $tag The tag name to pass on
964 * @param string $val The value of the tag
965 * @param string $arg An argument to pass ($1)
966 * @return string A wfMsg of "exif-$tag-$val" in lower case
967 */
968 function msg( $tag, $val, $arg = null ) {
969 if ($val === '')
970 $val = 'value';
971 return wfMsg( strtolower( "exif-$tag-$val" ), $arg );
972 }
973
974 /**
975 * Format a number, convert numbers from fractions into floating point
976 * numbers
977 *
978 * @access private
979 *
980 * @param mixed $num The value to format
981 * @return mixed A floating point number or whatever we were fed
982 */
983 function formatNum( $num ) {
984 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) )
985 return $m[2] != 0 ? $m[1] / $m[2] : $num;
986 else
987 return $num;
988 }
989 }