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