Check whether the username passed to makeTalkUrlDetails is valid to give a clearer...
[mediawiki.git] / includes / Exif.php
blob9bca6fd0058b15d0dc4c38f6ce4ca24b41673863
1 <?php
2 /**
3 * @addtogroup Metadata
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
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.
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.
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
24 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
25 * @bug 1555, 1947
28 /**
29 * @addtogroup Metadata
31 class Exif {
32 //@{
33 /* @var array
34 * @private
37 /**#@+
38 * Exif tag type definition
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.
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.
54 var $mExifTags;
56 /**
57 * A one dimentional array of all Exif tags
59 var $mFlatExifTags;
61 /**
62 * The raw Exif data returned by exif_read_data()
64 var $mRawExifData;
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
71 var $mFilteredExifData;
73 /**
74 * Filtered and formatted Exif data, see FormatExif::getFormattedData()
76 var $mFormattedExifData;
78 //@}
80 //@{
81 /* @var string
82 * @private
85 /**
86 * The file being processed
88 var $file;
90 /**
91 * The basename of the file being processed
93 var $basename;
95 /**
96 * The private log to log to
98 var $log = 'exif';
100 //@}
103 * Constructor
105 * @param $file String: filename.
107 function __construct( $file ) {
109 * Page numbers here refer to pages in the EXIF 2.2 standard
111 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
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)
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
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
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
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
175 # Tags relating to Image Data Characteristics
176 'characteristics' => array(
177 'ColorSpace' => Exif::SHORT, # Color space information #p32
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
188 # Tags relating to related user information
189 'user' => array(
190 'MakerNote' => Exif::UNDEFINED, # Manufacturer notes
191 'UserComment' => Exif::UNDEFINED, # User comments #p34
194 # Tags relating to related file information
195 'related' => array(
196 'RelatedSoundFile' => Exif::ASCII, # Related audio file
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
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
252 'other' => array(
253 'ImageUniqueID' => Exif::ASCII, # Unique image ID
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
293 $this->file = $file;
294 $this->basename = wfBaseName( $this->file );
296 $this->makeFlatExifTags();
298 $this->debugFile( $this->basename, __FUNCTION__, true );
299 wfSuppressWarnings();
300 $data = exif_read_data( $this->file );
301 wfRestoreWarnings();
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.
307 $this->mRawExifData = $data ? $data : array();
309 $this->makeFilteredData();
310 $this->makeFormattedData();
312 $this->debugFile( __FUNCTION__, false );
315 /**#@+
316 * @private
319 * Generate a flat list of the exif tags
321 function makeFlatExifTags() {
322 $this->extractTags( $this->mExifTags );
326 * A recursing extractor function used by makeFlatExifTags()
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`
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;
342 * Make $this->mFilteredExifData
344 function makeFilteredData() {
345 $this->mFilteredExifData = $this->mRawExifData;
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] );
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] );
363 * @todo document
365 function makeFormattedData( ) {
366 $format = new FormatExif( $this->getFilteredData() );
367 $this->mFormattedExifData = $format->getFormattedData();
369 /**#@-*/
371 /**#@+
372 * @return array
375 * Get $this->mRawExifData
377 function getData() {
378 return $this->mRawExifData;
382 * Get $this->mFilteredExifData
384 function getFilteredData() {
385 return $this->mFilteredExifData;
389 * Get $this->mFormattedExifData
391 function getFormattedData() {
392 return $this->mFormattedExifData;
394 /**#@-*/
397 * The version of the output format
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.
406 * @return int
408 function version() {
409 return 1; // We don't need no bloddy constants!
412 /**#@+
413 * Validates if a tag value is of the type it should be according to the Exif spec
415 * @private
417 * @param $in Mixed: the input value to check
418 * @return bool
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;
430 function isASCII( $in ) {
431 if ( is_array( $in ) ) {
432 return false;
435 if ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) {
436 $this->debug( $in, __FUNCTION__, 'found a character not in our whitelist' );
437 return false;
440 if ( preg_match( '/^\s*$/', $in ) ) {
441 $this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' );
442 return false;
445 return true;
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;
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;
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;
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;
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;
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;
507 /**#@-*/
510 * Validates if a tag has a legal value according to the Exif spec
512 * @private
514 * @param $tag String: the tag to check.
515 * @param $val Mixed: the value of the tag.
516 * @return bool
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;
556 * Convenience function for debugging output
558 * @private
560 * @param $in Mixed:
561 * @param $fname String:
562 * @param $action Mixed: , default NULL.
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 );
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");
581 * Convenience function for debugging output
583 * @private
585 * @param $fname String: the name of the function calling this function
586 * @param $io Boolean: Specify whether we're beginning or ending
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" );
600 * @addtogroup Metadata
602 class FormatExif {
604 * The Exif data to format
606 * @var array
607 * @private
609 var $mExif;
612 * Constructor
614 * @param $exif Array: the Exif data to format ( as returned by
615 * Exif::getFilteredData() )
617 function FormatExif( $exif ) {
618 $this->mExif = $exif;
622 * Numbers given by Exif user agents are often magical, that is they
623 * should be replaced by a detailed explanation depending on their
624 * value which most of the time are plain integers. This function
625 * formats Exif values into human readable form.
627 * @return array
629 function getFormattedData() {
630 global $wgLang;
632 $tags =& $this->mExif;
634 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
635 unset( $tags['ResolutionUnit'] );
637 foreach( $tags as $tag => $val ) {
638 switch( $tag ) {
639 case 'Compression':
640 switch( $val ) {
641 case 1: case 6:
642 $tags[$tag] = $this->msg( $tag, $val );
643 break;
644 default:
645 $tags[$tag] = $val;
646 break;
648 break;
650 case 'PhotometricInterpretation':
651 switch( $val ) {
652 case 2: case 6:
653 $tags[$tag] = $this->msg( $tag, $val );
654 break;
655 default:
656 $tags[$tag] = $val;
657 break;
659 break;
661 case 'Orientation':
662 switch( $val ) {
663 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
664 $tags[$tag] = $this->msg( $tag, $val );
665 break;
666 default:
667 $tags[$tag] = $val;
668 break;
670 break;
672 case 'PlanarConfiguration':
673 switch( $val ) {
674 case 1: case 2:
675 $tags[$tag] = $this->msg( $tag, $val );
676 break;
677 default:
678 $tags[$tag] = $val;
679 break;
681 break;
683 // TODO: YCbCrSubSampling
684 // TODO: YCbCrPositioning
686 case 'XResolution':
687 case 'YResolution':
688 switch( $resolutionunit ) {
689 case 2:
690 $tags[$tag] = $this->msg( 'XYResolution', 'i', $this->formatNum( $val ) );
691 break;
692 case 3:
693 $this->msg( 'XYResolution', 'c', $this->formatNum( $val ) );
694 break;
695 default:
696 $tags[$tag] = $val;
697 break;
699 break;
701 // TODO: YCbCrCoefficients #p27 (see annex E)
702 case 'ExifVersion': case 'FlashpixVersion':
703 $tags[$tag] = "$val"/100;
704 break;
706 case 'ColorSpace':
707 switch( $val ) {
708 case 1: case 'FFFF.H':
709 $tags[$tag] = $this->msg( $tag, $val );
710 break;
711 default:
712 $tags[$tag] = $val;
713 break;
715 break;
717 case 'ComponentsConfiguration':
718 switch( $val ) {
719 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
720 $tags[$tag] = $this->msg( $tag, $val );
721 break;
722 default:
723 $tags[$tag] = $val;
724 break;
726 break;
728 case 'DateTime':
729 case 'DateTimeOriginal':
730 case 'DateTimeDigitized':
731 if( $val == '0000:00:00 00:00:00' ) {
732 $tags[$tag] = wfMsg('exif-unknowndate');
733 } elseif( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/', $val ) ) {
734 $tags[$tag] = $wgLang->timeanddate( wfTimestamp(TS_MW, $val) );
736 break;
738 case 'ExposureProgram':
739 switch( $val ) {
740 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
741 $tags[$tag] = $this->msg( $tag, $val );
742 break;
743 default:
744 $tags[$tag] = $val;
745 break;
747 break;
749 case 'SubjectDistance':
750 $tags[$tag] = $this->msg( $tag, '', $this->formatNum( $val ) );
751 break;
753 case 'MeteringMode':
754 switch( $val ) {
755 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
756 $tags[$tag] = $this->msg( $tag, $val );
757 break;
758 default:
759 $tags[$tag] = $val;
760 break;
762 break;
764 case 'LightSource':
765 switch( $val ) {
766 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
767 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
768 case 21: case 22: case 23: case 24: case 255:
769 $tags[$tag] = $this->msg( $tag, $val );
770 break;
771 default:
772 $tags[$tag] = $val;
773 break;
775 break;
777 // TODO: Flash
778 case 'FocalPlaneResolutionUnit':
779 switch( $val ) {
780 case 2:
781 $tags[$tag] = $this->msg( $tag, $val );
782 break;
783 default:
784 $tags[$tag] = $val;
785 break;
787 break;
789 case 'SensingMethod':
790 switch( $val ) {
791 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
792 $tags[$tag] = $this->msg( $tag, $val );
793 break;
794 default:
795 $tags[$tag] = $val;
796 break;
798 break;
800 case 'FileSource':
801 switch( $val ) {
802 case 3:
803 $tags[$tag] = $this->msg( $tag, $val );
804 break;
805 default:
806 $tags[$tag] = $val;
807 break;
809 break;
811 case 'SceneType':
812 switch( $val ) {
813 case 1:
814 $tags[$tag] = $this->msg( $tag, $val );
815 break;
816 default:
817 $tags[$tag] = $val;
818 break;
820 break;
822 case 'CustomRendered':
823 switch( $val ) {
824 case 0: case 1:
825 $tags[$tag] = $this->msg( $tag, $val );
826 break;
827 default:
828 $tags[$tag] = $val;
829 break;
831 break;
833 case 'ExposureMode':
834 switch( $val ) {
835 case 0: case 1: case 2:
836 $tags[$tag] = $this->msg( $tag, $val );
837 break;
838 default:
839 $tags[$tag] = $val;
840 break;
842 break;
844 case 'WhiteBalance':
845 switch( $val ) {
846 case 0: case 1:
847 $tags[$tag] = $this->msg( $tag, $val );
848 break;
849 default:
850 $tags[$tag] = $val;
851 break;
853 break;
855 case 'SceneCaptureType':
856 switch( $val ) {
857 case 0: case 1: case 2: case 3:
858 $tags[$tag] = $this->msg( $tag, $val );
859 break;
860 default:
861 $tags[$tag] = $val;
862 break;
864 break;
866 case 'GainControl':
867 switch( $val ) {
868 case 0: case 1: case 2: case 3: case 4:
869 $tags[$tag] = $this->msg( $tag, $val );
870 break;
871 default:
872 $tags[$tag] = $val;
873 break;
875 break;
877 case 'Contrast':
878 switch( $val ) {
879 case 0: case 1: case 2:
880 $tags[$tag] = $this->msg( $tag, $val );
881 break;
882 default:
883 $tags[$tag] = $val;
884 break;
886 break;
888 case 'Saturation':
889 switch( $val ) {
890 case 0: case 1: case 2:
891 $tags[$tag] = $this->msg( $tag, $val );
892 break;
893 default:
894 $tags[$tag] = $val;
895 break;
897 break;
899 case 'Sharpness':
900 switch( $val ) {
901 case 0: case 1: case 2:
902 $tags[$tag] = $this->msg( $tag, $val );
903 break;
904 default:
905 $tags[$tag] = $val;
906 break;
908 break;
910 case 'SubjectDistanceRange':
911 switch( $val ) {
912 case 0: case 1: case 2: case 3:
913 $tags[$tag] = $this->msg( $tag, $val );
914 break;
915 default:
916 $tags[$tag] = $val;
917 break;
919 break;
921 case 'GPSLatitudeRef':
922 case 'GPSDestLatitudeRef':
923 switch( $val ) {
924 case 'N': case 'S':
925 $tags[$tag] = $this->msg( 'GPSLatitude', $val );
926 break;
927 default:
928 $tags[$tag] = $val;
929 break;
931 break;
933 case 'GPSLongitudeRef':
934 case 'GPSDestLongitudeRef':
935 switch( $val ) {
936 case 'E': case 'W':
937 $tags[$tag] = $this->msg( 'GPSLongitude', $val );
938 break;
939 default:
940 $tags[$tag] = $val;
941 break;
943 break;
945 case 'GPSStatus':
946 switch( $val ) {
947 case 'A': case 'V':
948 $tags[$tag] = $this->msg( $tag, $val );
949 break;
950 default:
951 $tags[$tag] = $val;
952 break;
954 break;
956 case 'GPSMeasureMode':
957 switch( $val ) {
958 case 2: case 3:
959 $tags[$tag] = $this->msg( $tag, $val );
960 break;
961 default:
962 $tags[$tag] = $val;
963 break;
965 break;
967 case 'GPSSpeedRef':
968 case 'GPSDestDistanceRef':
969 switch( $val ) {
970 case 'K': case 'M': case 'N':
971 $tags[$tag] = $this->msg( 'GPSSpeed', $val );
972 break;
973 default:
974 $tags[$tag] = $val;
975 break;
977 break;
979 case 'GPSTrackRef':
980 case 'GPSImgDirectionRef':
981 case 'GPSDestBearingRef':
982 switch( $val ) {
983 case 'T': case 'M':
984 $tags[$tag] = $this->msg( 'GPSDirection', $val );
985 break;
986 default:
987 $tags[$tag] = $val;
988 break;
990 break;
992 case 'GPSDateStamp':
993 $tags[$tag] = $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
994 break;
996 // This is not in the Exif standard, just a special
997 // case for our purposes which enables wikis to wikify
998 // the make, model and software name to link to their articles.
999 case 'Make':
1000 case 'Model':
1001 case 'Software':
1002 $tags[$tag] = $this->msg( $tag, '', $val );
1003 break;
1005 case 'ExposureTime':
1006 // Show the pretty fraction as well as decimal version
1007 $tags[$tag] = wfMsg( 'exif-exposuretime-format',
1008 $this->formatFraction( $val ), $this->formatNum( $val ) );
1009 break;
1011 case 'FNumber':
1012 $tags[$tag] = wfMsg( 'exif-fnumber-format',
1013 $this->formatNum( $val ) );
1014 break;
1016 case 'FocalLength':
1017 $tags[$tag] = wfMsg( 'exif-focallength-format',
1018 $this->formatNum( $val ) );
1019 break;
1021 default:
1022 $tags[$tag] = $this->formatNum( $val );
1023 break;
1027 return $tags;
1031 * Convenience function for getFormattedData()
1033 * @private
1035 * @param $tag String: the tag name to pass on
1036 * @param $val String: the value of the tag
1037 * @param $arg String: an argument to pass ($1)
1038 * @return string A wfMsg of "exif-$tag-$val" in lower case
1040 function msg( $tag, $val, $arg = null ) {
1041 global $wgContLang;
1043 if ($val === '')
1044 $val = 'value';
1045 return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg );
1049 * Format a number, convert numbers from fractions into floating point
1050 * numbers
1052 * @private
1054 * @param $num Mixed: the value to format
1055 * @return mixed A floating point number or whatever we were fed
1057 function formatNum( $num ) {
1058 $m = array();
1059 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) )
1060 return $m[2] != 0 ? $m[1] / $m[2] : $num;
1061 else
1062 return $num;
1066 * Format a rational number, reducing fractions
1068 * @private
1070 * @param $num Mixed: the value to format
1071 * @return mixed A floating point number or whatever we were fed
1073 function formatFraction( $num ) {
1074 $m = array();
1075 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) ) {
1076 $numerator = intval( $m[1] );
1077 $denominator = intval( $m[2] );
1078 $gcd = $this->gcd( $numerator, $denominator );
1079 if( $gcd != 0 ) {
1080 // 0 shouldn't happen! ;)
1081 return $numerator / $gcd . '/' . $denominator / $gcd;
1084 return $this->formatNum( $num );
1088 * Calculate the greatest common divisor of two integers.
1090 * @param $a Integer: FIXME
1091 * @param $b Integer: FIXME
1092 * @return int
1093 * @private
1095 function gcd( $a, $b ) {
1097 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1098 // Recursive form would be:
1099 if( $b == 0 )
1100 return $a;
1101 else
1102 return gcd( $b, $a % $b );
1104 while( $b != 0 ) {
1105 $remainder = $a % $b;
1107 // tail recursion...
1108 $a = $b;
1109 $b = $remainder;
1111 return $a;
1116 * MW 1.6 compatibility
1118 define( 'MW_EXIF_BYTE', Exif::BYTE );
1119 define( 'MW_EXIF_ASCII', Exif::ASCII );
1120 define( 'MW_EXIF_SHORT', Exif::SHORT );
1121 define( 'MW_EXIF_LONG', Exif::LONG );
1122 define( 'MW_EXIF_RATIONAL', Exif::RATIONAL );
1123 define( 'MW_EXIF_UNDEFINED', Exif::UNDEFINED );
1124 define( 'MW_EXIF_SLONG', Exif::SLONG );
1125 define( 'MW_EXIF_SRATIONAL', Exif::SRATIONAL );