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