5ee4eabd37c231cb0fca32cc4c53d6845b6f536b
[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 foreach( $tags as $tag => $val ) {
584 switch( $tag ) {
585 case 'Compression':
586 switch( $val ) {
587 case 1: case 6:
588 $tags[$tag] = $this->msg( $tag, $val );
589 break;
590 default:
591 $tags[$tag] = $val;
592 break;
593 }
594 break;
595
596 case 'PhotometricInterpretation':
597 switch( $val ) {
598 case 2: case 6:
599 $tags[$tag] = $this->msg( $tag, $val );
600 break;
601 default:
602 $tags[$tag] = $val;
603 break;
604 }
605 break;
606
607 case 'Orientation':
608 switch( $val ) {
609 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
610 $tags[$tag] = $this->msg( $tag, $val );
611 break;
612 default:
613 $tags[$tag] = $val;
614 break;
615 }
616 break;
617
618 case 'PlanarConfiguration':
619 switch( $val ) {
620 case 1: case 2:
621 $tags[$tag] = $this->msg( $tag, $val );
622 break;
623 default:
624 $tags[$tag] = $val;
625 break;
626 }
627 break;
628
629 // TODO: YCbCrSubSampling
630 // TODO: YCbCrPositioning
631 // TODO: If this field does not exists use 2
632 case 'ResolutionUnit': #p26
633 switch( $val ) {
634 case 2: case 3:
635 $tags[$tag] = $this->msg( $tag, $val );
636 break;
637 default:
638 $tags[$tag] = $val;
639 break;
640 }
641 break;
642
643 // TODO: YCbCrCoefficients #p27 (see annex E)
644 case 'ExifVersion': case 'FlashpixVersion':
645 $tags[$tag] = "$val"/100;
646 break;
647
648 case 'ColorSpace':
649 switch( $val ) {
650 case 1: case 'FFFF.H':
651 $tags[$tag] = $this->msg( $tag, $val );
652 break;
653 default:
654 $tags[$tag] = $val;
655 break;
656 }
657 break;
658
659 case 'ComponentsConfiguration':
660 switch( $val ) {
661 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
662 $tags[$tag] = $this->msg( $tag, $val );
663 break;
664 default:
665 $tags[$tag] = $val;
666 break;
667 }
668 break;
669
670 case 'DateTime':
671 case 'DateTimeOriginal':
672 case 'DateTimeDigitized':
673 $tags[$tag] = $wgLang->timeanddate( wfTimestamp(TS_MW, $val) );
674 break;
675
676 case 'ExposureProgram':
677 switch( $val ) {
678 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
679 $tags[$tag] = $this->msg( $tag, $val );
680 break;
681 default:
682 $tags[$tag] = $val;
683 break;
684 }
685 break;
686
687 case 'SubjectDistance':
688 $tags[$tag] = $this->msg( $tag, '', $this->formatNum( $val ) );
689 break;
690
691 case 'MeteringMode':
692 switch( $val ) {
693 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
694 $tags[$tag] = $this->msg( $tag, $val );
695 break;
696 default:
697 $tags[$tag] = $val;
698 break;
699 }
700 break;
701
702 case 'LightSource':
703 switch( $val ) {
704 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
705 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
706 case 21: case 22: case 23: case 24: case 255:
707 $tags[$tag] = $this->msg( $tag, $val );
708 break;
709 default:
710 $tags[$tag] = $val;
711 break;
712 }
713 break;
714
715 // TODO: Flash
716 case 'FocalPlaneResolutionUnit':
717 switch( $val ) {
718 case 2:
719 $tags[$tag] = $this->msg( $tag, $val );
720 break;
721 default:
722 $tags[$tag] = $val;
723 break;
724 }
725 break;
726
727 case 'SensingMethod':
728 switch( $val ) {
729 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
730 $tags[$tag] = $this->msg( $tag, $val );
731 break;
732 default:
733 $tags[$tag] = $val;
734 break;
735 }
736 break;
737
738 case 'FileSource':
739 switch( $val ) {
740 case 3:
741 $tags[$tag] = $this->msg( $tag, $val );
742 break;
743 default:
744 $tags[$tag] = $val;
745 break;
746 }
747 break;
748
749 case 'SceneType':
750 switch( $val ) {
751 case 1:
752 $tags[$tag] = $this->msg( $tag, $val );
753 break;
754 default:
755 $tags[$tag] = $val;
756 break;
757 }
758 break;
759
760 case 'CustomRendered':
761 switch( $val ) {
762 case 0: case 1:
763 $tags[$tag] = $this->msg( $tag, $val );
764 break;
765 default:
766 $tags[$tag] = $val;
767 break;
768 }
769 break;
770
771 case 'ExposureMode':
772 switch( $val ) {
773 case 0: case 1: case 2:
774 $tags[$tag] = $this->msg( $tag, $val );
775 break;
776 default:
777 $tags[$tag] = $val;
778 break;
779 }
780 break;
781
782 case 'WhiteBalance':
783 switch( $val ) {
784 case 0: case 1:
785 $tags[$tag] = $this->msg( $tag, $val );
786 break;
787 default:
788 $tags[$tag] = $val;
789 break;
790 }
791 break;
792
793 case 'SceneCaptureType':
794 switch( $val ) {
795 case 0: case 1: case 2: case 3:
796 $tags[$tag] = $this->msg( $tag, $val );
797 break;
798 default:
799 $tags[$tag] = $val;
800 break;
801 }
802 break;
803
804 case 'GainControl':
805 switch( $val ) {
806 case 0: case 1: case 2: case 3: case 4:
807 $tags[$tag] = $this->msg( $tag, $val );
808 break;
809 default:
810 $tags[$tag] = $val;
811 break;
812 }
813 break;
814
815 case 'Contrast':
816 switch( $val ) {
817 case 0: case 1: case 2:
818 $tags[$tag] = $this->msg( $tag, $val );
819 break;
820 default:
821 $tags[$tag] = $val;
822 break;
823 }
824 break;
825
826 case 'Saturation':
827 switch( $val ) {
828 case 0: case 1: case 2:
829 $tags[$tag] = $this->msg( $tag, $val );
830 break;
831 default:
832 $tags[$tag] = $val;
833 break;
834 }
835 break;
836
837 case 'Sharpness':
838 switch( $val ) {
839 case 0: case 1: case 2:
840 $tags[$tag] = $this->msg( $tag, $val );
841 break;
842 default:
843 $tags[$tag] = $val;
844 break;
845 }
846 break;
847
848 case 'SubjectDistanceRange':
849 switch( $val ) {
850 case 0: case 1: case 2: case 3:
851 $tags[$tag] = $this->msg( $tag, $val );
852 break;
853 default:
854 $tags[$tag] = $val;
855 break;
856 }
857 break;
858
859 case 'GPSLatitudeRef':
860 case 'GPSDestLatitudeRef':
861 switch( $val ) {
862 case 'N': case 'S':
863 $tags[$tag] = $this->msg( 'GPSLatitude', $val );
864 break;
865 default:
866 $tags[$tag] = $val;
867 break;
868 }
869 break;
870
871 case 'GPSLongitudeRef':
872 case 'GPSDestLongitudeRef':
873 switch( $val ) {
874 case 'E': case 'W':
875 $tags[$tag] = $this->msg( 'GPSLongitude', $val );
876 break;
877 default:
878 $tags[$tag] = $val;
879 break;
880 }
881 break;
882
883 case 'GPSStatus':
884 switch( $val ) {
885 case 'A': case 'V':
886 $tags[$tag] = $this->msg( $tag, $val );
887 break;
888 default:
889 $tags[$tag] = $val;
890 break;
891 }
892 break;
893
894 case 'GPSMeasureMode':
895 switch( $val ) {
896 case 2: case 3:
897 $tags[$tag] = $this->msg( $tag, $val );
898 break;
899 default:
900 $tags[$tag] = $val;
901 break;
902 }
903 break;
904
905 case 'GPSSpeedRef':
906 case 'GPSDestDistanceRef':
907 switch( $val ) {
908 case 'K': case 'M': case 'N':
909 $tags[$tag] = $this->msg( 'GPSSpeed', $val );
910 break;
911 default:
912 $tags[$tag] = $val;
913 break;
914 }
915 break;
916
917 case 'GPSTrackRef':
918 case 'GPSImgDirectionRef':
919 case 'GPSDestBearingRef':
920 switch( $val ) {
921 case 'T': case 'M':
922 $tags[$tag] = $this->msg( 'GPSDirection', $val );
923 break;
924 default:
925 $tags[$tag] = $val;
926 break;
927 }
928 break;
929
930 case 'GPSDateStamp':
931 $tags[$tag] = $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
932 break;
933
934 // This is not in the Exif standard, just a special
935 // case for our purposes which enables wikis to wikify
936 // the make, model and software name to link to their articles.
937 case 'Make':
938 case 'Model':
939 case 'Software':
940 $tags[$tag] = $this->msg( $tag, '', $val );
941 break;
942 default:
943 $tags[$tag] = $this->formatNum( $val );
944 break;
945 }
946 }
947
948 return $tags;
949 }
950
951 /**
952 * Conviniance function for getFormattedData()
953 *
954 * @access private
955 *
956 * @param string $tag The tag name to pass on
957 * @param string $val The value of the tag
958 * @param string $arg An argument to pass ($1)
959 * @return string A wfMsg of "exif-$tag-$val" in lower case
960 */
961 function msg( $tag, $val, $arg = null ) {
962 if ($val === '')
963 $val = 'value';
964 return wfMsg( strtolower( "exif-$tag-$val" ), $arg );
965 }
966
967 /**
968 * Format a number, convert numbers from fractions into floating point
969 * numbers
970 *
971 * @access private
972 *
973 * @param mixed $num The value to format
974 * @return mixed A floating point number or whatever we were fed
975 */
976 function formatNum( $num ) {
977 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) )
978 return $m[2] != 0 ? $m[1] / $m[2] : $num;
979 else
980 return $num;
981 }
982 }