Code housekeeping stuff (and barring any stuff-ups on my behalf, there should be...
[lhc/web/wiklou.git] / includes / Exif.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Metadata
5 *
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
9 *
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.
14 *
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.
19 *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
26 * @bug 1555, 1947
27 */
28
29 /**
30 * @package MediaWiki
31 * @subpackage Metadata
32 */
33 class Exif {
34 //@{
35 /* @var array
36 * @private
37 */
38
39 /**#@+
40 * Exif tag type definition
41 */
42 const BYTE = 1; # An 8-bit (1-byte) unsigned integer.
43 const ASCII = 2; # An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.
44 const SHORT = 3; # A 16-bit (2-byte) unsigned integer.
45 const LONG = 4; # A 32-bit (4-byte) unsigned integer.
46 const RATIONAL = 5; # Two LONGs. The first LONG is the numerator and the second LONG expresses the denominator
47 const UNDEFINED = 7; # An 8-bit byte that can take any value depending on the field definition
48 const SLONG = 9; # A 32-bit (4-byte) signed integer (2's complement notation),
49 const SRATIONAL = 10; # Two SLONGs. The first SLONG is the numerator and the second SLONG is the denominator.
50
51 /**
52 * Exif tags grouped by category, the tagname itself is the key and the type
53 * is the value, in the case of more than one possible value type they are
54 * seperated by commas.
55 */
56 var $mExifTags;
57
58 /**
59 * A one dimentional array of all Exif tags
60 */
61 var $mFlatExifTags;
62
63 /**
64 * The raw Exif data returned by exif_read_data()
65 */
66 var $mRawExifData;
67
68 /**
69 * A Filtered version of $mRawExifData that has been pruned of invalid
70 * tags and tags that contain content they shouldn't contain according
71 * to the Exif specification
72 */
73 var $mFilteredExifData;
74
75 /**
76 * Filtered and formatted Exif data, see FormatExif::getFormattedData()
77 */
78 var $mFormattedExifData;
79
80 //@}
81
82 //@{
83 /* @var string
84 * @private
85 */
86
87 /**
88 * The file being processed
89 */
90 var $file;
91
92 /**
93 * The basename of the file being processed
94 */
95 var $basename;
96
97 /**
98 * The private log to log to
99 */
100 var $log = 'exif';
101
102 //@}
103
104 /**
105 * Constructor
106 *
107 * @param $file String: filename.
108 */
109 function Exif( $file ) {
110 /**
111 * Page numbers here refer to pages in the EXIF 2.2 standard
112 *
113 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
114 */
115 $this->mExifTags = array(
116 # TIFF Rev. 6.0 Attribute Information (p22)
117 'tiff' => array(
118 # Tags relating to image structure
119 'structure' => array(
120 'ImageWidth' => Exif::SHORT.','.Exif::LONG, # Image width
121 'ImageLength' => Exif::SHORT.','.Exif::LONG, # Image height
122 'BitsPerSample' => Exif::SHORT, # Number of bits per component
123 # "When a primary image is JPEG compressed, this designation is not"
124 # "necessary and is omitted." (p23)
125 'Compression' => Exif::SHORT, # Compression scheme #p23
126 'PhotometricInterpretation' => Exif::SHORT, # Pixel composition #p23
127 'Orientation' => Exif::SHORT, # Orientation of image #p24
128 'SamplesPerPixel' => Exif::SHORT, # Number of components
129 'PlanarConfiguration' => Exif::SHORT, # Image data arrangement #p24
130 'YCbCrSubSampling' => Exif::SHORT, # Subsampling ratio of Y to C #p24
131 'YCbCrPositioning' => Exif::SHORT, # Y and C positioning #p24-25
132 'XResolution' => Exif::RATIONAL, # Image resolution in width direction
133 'YResolution' => Exif::RATIONAL, # Image resolution in height direction
134 'ResolutionUnit' => Exif::SHORT, # Unit of X and Y resolution #(p26)
135 ),
136
137 # Tags relating to recording offset
138 'offset' => array(
139 'StripOffsets' => Exif::SHORT.','.Exif::LONG, # Image data location
140 'RowsPerStrip' => Exif::SHORT.','.Exif::LONG, # Number of rows per strip
141 'StripByteCounts' => Exif::SHORT.','.Exif::LONG, # Bytes per compressed strip
142 'JPEGInterchangeFormat' => Exif::SHORT.','.Exif::LONG, # Offset to JPEG SOI
143 'JPEGInterchangeFormatLength' => Exif::SHORT.','.Exif::LONG, # Bytes of JPEG data
144 ),
145
146 # Tags relating to image data characteristics
147 'characteristics' => array(
148 'TransferFunction' => Exif::SHORT, # Transfer function
149 'WhitePoint' => Exif::RATIONAL, # White point chromaticity
150 'PrimaryChromaticities' => Exif::RATIONAL, # Chromaticities of primarities
151 'YCbCrCoefficients' => Exif::RATIONAL, # Color space transformation matrix coefficients #p27
152 'ReferenceBlackWhite' => Exif::RATIONAL # Pair of black and white reference values
153 ),
154
155 # Other tags
156 'other' => array(
157 'DateTime' => Exif::ASCII, # File change date and time
158 'ImageDescription' => Exif::ASCII, # Image title
159 'Make' => Exif::ASCII, # Image input equipment manufacturer
160 'Model' => Exif::ASCII, # Image input equipment model
161 'Software' => Exif::ASCII, # Software used
162 'Artist' => Exif::ASCII, # Person who created the image
163 'Copyright' => Exif::ASCII, # Copyright holder
164 ),
165 ),
166
167 # Exif IFD Attribute Information (p30-31)
168 'exif' => array(
169 # Tags relating to version
170 'version' => array(
171 # TODO: NOTE: Nonexistence of this field is taken to mean nonconformance
172 # to the EXIF 2.1 AND 2.2 standards
173 'ExifVersion' => Exif::UNDEFINED, # Exif version
174 'FlashpixVersion' => Exif::UNDEFINED, # Supported Flashpix version #p32
175 ),
176
177 # Tags relating to Image Data Characteristics
178 'characteristics' => array(
179 'ColorSpace' => Exif::SHORT, # Color space information #p32
180 ),
181
182 # Tags relating to image configuration
183 'configuration' => array(
184 'ComponentsConfiguration' => Exif::UNDEFINED, # Meaning of each component #p33
185 'CompressedBitsPerPixel' => Exif::RATIONAL, # Image compression mode
186 'PixelYDimension' => Exif::SHORT.','.Exif::LONG, # Valid image width
187 'PixelXDimension' => Exif::SHORT.','.Exif::LONG, # Valind image height
188 ),
189
190 # Tags relating to related user information
191 'user' => array(
192 'MakerNote' => Exif::UNDEFINED, # Manufacturer notes
193 'UserComment' => Exif::UNDEFINED, # User comments #p34
194 ),
195
196 # Tags relating to related file information
197 'related' => array(
198 'RelatedSoundFile' => Exif::ASCII, # Related audio file
199 ),
200
201 # Tags relating to date and time
202 'dateandtime' => array(
203 'DateTimeOriginal' => Exif::ASCII, # Date and time of original data generation #p36
204 'DateTimeDigitized' => Exif::ASCII, # Date and time of original data generation
205 'SubSecTime' => Exif::ASCII, # DateTime subseconds
206 'SubSecTimeOriginal' => Exif::ASCII, # DateTimeOriginal subseconds
207 'SubSecTimeDigitized' => Exif::ASCII, # DateTimeDigitized subseconds
208 ),
209
210 # Tags relating to picture-taking conditions (p31)
211 'conditions' => array(
212 'ExposureTime' => Exif::RATIONAL, # Exposure time
213 'FNumber' => Exif::RATIONAL, # F Number
214 'ExposureProgram' => Exif::SHORT, # Exposure Program #p38
215 'SpectralSensitivity' => Exif::ASCII, # Spectral sensitivity
216 'ISOSpeedRatings' => Exif::SHORT, # ISO speed rating
217 'OECF' => Exif::UNDEFINED, # Optoelectronic conversion factor
218 'ShutterSpeedValue' => Exif::SRATIONAL, # Shutter speed
219 'ApertureValue' => Exif::RATIONAL, # Aperture
220 'BrightnessValue' => Exif::SRATIONAL, # Brightness
221 'ExposureBiasValue' => Exif::SRATIONAL, # Exposure bias
222 'MaxApertureValue' => Exif::RATIONAL, # Maximum land aperture
223 'SubjectDistance' => Exif::RATIONAL, # Subject distance
224 'MeteringMode' => Exif::SHORT, # Metering mode #p40
225 'LightSource' => Exif::SHORT, # Light source #p40-41
226 'Flash' => Exif::SHORT, # Flash #p41-42
227 'FocalLength' => Exif::RATIONAL, # Lens focal length
228 'SubjectArea' => Exif::SHORT, # Subject area
229 'FlashEnergy' => Exif::RATIONAL, # Flash energy
230 'SpatialFrequencyResponse' => Exif::UNDEFINED, # Spatial frequency response
231 'FocalPlaneXResolution' => Exif::RATIONAL, # Focal plane X resolution
232 'FocalPlaneYResolution' => Exif::RATIONAL, # Focal plane Y resolution
233 'FocalPlaneResolutionUnit' => Exif::SHORT, # Focal plane resolution unit #p46
234 'SubjectLocation' => Exif::SHORT, # Subject location
235 'ExposureIndex' => Exif::RATIONAL, # Exposure index
236 'SensingMethod' => Exif::SHORT, # Sensing method #p46
237 'FileSource' => Exif::UNDEFINED, # File source #p47
238 'SceneType' => Exif::UNDEFINED, # Scene type #p47
239 'CFAPattern' => Exif::UNDEFINED, # CFA pattern
240 'CustomRendered' => Exif::SHORT, # Custom image processing #p48
241 'ExposureMode' => Exif::SHORT, # Exposure mode #p48
242 'WhiteBalance' => Exif::SHORT, # White Balance #p49
243 'DigitalZoomRatio' => Exif::RATIONAL, # Digital zoom ration
244 'FocalLengthIn35mmFilm' => Exif::SHORT, # Focal length in 35 mm film
245 'SceneCaptureType' => Exif::SHORT, # Scene capture type #p49
246 'GainControl' => Exif::RATIONAL, # Scene control #p49-50
247 'Contrast' => Exif::SHORT, # Contrast #p50
248 'Saturation' => Exif::SHORT, # Saturation #p50
249 'Sharpness' => Exif::SHORT, # Sharpness #p50
250 'DeviceSettingDescription' => Exif::UNDEFINED, # Desice settings description
251 'SubjectDistanceRange' => Exif::SHORT, # Subject distance range #p51
252 ),
253
254 'other' => array(
255 'ImageUniqueID' => Exif::ASCII, # Unique image ID
256 ),
257 ),
258
259 # GPS Attribute Information (p52)
260 'gps' => array(
261 'GPSVersionID' => Exif::BYTE, # GPS tag version
262 'GPSLatitudeRef' => Exif::ASCII, # North or South Latitude #p52-53
263 'GPSLatitude' => Exif::RATIONAL, # Latitude
264 'GPSLongitudeRef' => Exif::ASCII, # East or West Longitude #p53
265 'GPSLongitude' => Exif::RATIONAL, # Longitude
266 'GPSAltitudeRef' => Exif::BYTE, # Altitude reference
267 'GPSAltitude' => Exif::RATIONAL, # Altitude
268 'GPSTimeStamp' => Exif::RATIONAL, # GPS time (atomic clock)
269 'GPSSatellites' => Exif::ASCII, # Satellites used for measurement
270 'GPSStatus' => Exif::ASCII, # Receiver status #p54
271 'GPSMeasureMode' => Exif::ASCII, # Measurement mode #p54-55
272 'GPSDOP' => Exif::RATIONAL, # Measurement precision
273 'GPSSpeedRef' => Exif::ASCII, # Speed unit #p55
274 'GPSSpeed' => Exif::RATIONAL, # Speed of GPS receiver
275 'GPSTrackRef' => Exif::ASCII, # Reference for direction of movement #p55
276 'GPSTrack' => Exif::RATIONAL, # Direction of movement
277 'GPSImgDirectionRef' => Exif::ASCII, # Reference for direction of image #p56
278 'GPSImgDirection' => Exif::RATIONAL, # Direction of image
279 'GPSMapDatum' => Exif::ASCII, # Geodetic survey data used
280 'GPSDestLatitudeRef' => Exif::ASCII, # Reference for latitude of destination #p56
281 'GPSDestLatitude' => Exif::RATIONAL, # Latitude destination
282 'GPSDestLongitudeRef' => Exif::ASCII, # Reference for longitude of destination #p57
283 'GPSDestLongitude' => Exif::RATIONAL, # Longitude of destination
284 'GPSDestBearingRef' => Exif::ASCII, # Reference for bearing of destination #p57
285 'GPSDestBearing' => Exif::RATIONAL, # Bearing of destination
286 'GPSDestDistanceRef' => Exif::ASCII, # Reference for distance to destination #p57-58
287 'GPSDestDistance' => Exif::RATIONAL, # Distance to destination
288 'GPSProcessingMethod' => Exif::UNDEFINED, # Name of GPS processing method
289 'GPSAreaInformation' => Exif::UNDEFINED, # Name of GPS area
290 'GPSDateStamp' => Exif::ASCII, # GPS date
291 'GPSDifferential' => Exif::SHORT, # GPS differential correction
292 ),
293 );
294
295 $this->file = $file;
296 $this->basename = wfBaseName( $this->file );
297
298 $this->makeFlatExifTags();
299
300 $this->debugFile( $this->basename, __FUNCTION__, true );
301 wfSuppressWarnings();
302 $data = exif_read_data( $this->file );
303 wfRestoreWarnings();
304 /**
305 * exif_read_data() will return false on invalid input, such as
306 * when somebody uploads a file called something.jpeg
307 * containing random gibberish.
308 */
309 $this->mRawExifData = $data ? $data : array();
310
311 $this->makeFilteredData();
312 $this->makeFormattedData();
313
314 $this->debugFile( __FUNCTION__, false );
315 }
316
317 /**#@+
318 * @private
319 */
320 /**
321 * Generate a flat list of the exif tags
322 */
323 function makeFlatExifTags() {
324 $this->extractTags( $this->mExifTags );
325 }
326
327 /**
328 * A recursing extractor function used by makeFlatExifTags()
329 *
330 * Note: This used to use an array_walk function, but it made PHP5
331 * segfault, see `cvs diff -u -r 1.4 -r 1.5 Exif.php`
332 */
333 function extractTags( &$tagset ) {
334 foreach( $tagset as $key => $val ) {
335 if( is_array( $val ) ) {
336 $this->extractTags( $val );
337 } else {
338 $this->mFlatExifTags[$key] = $val;
339 }
340 }
341 }
342
343 /**
344 * Make $this->mFilteredExifData
345 */
346 function makeFilteredData() {
347 $this->mFilteredExifData = $this->mRawExifData;
348
349 foreach( $this->mFilteredExifData as $k => $v ) {
350 if ( !in_array( $k, array_keys( $this->mFlatExifTags ) ) ) {
351 $this->debug( $v, __FUNCTION__, "'$k' is not a valid Exif tag" );
352 unset( $this->mFilteredExifData[$k] );
353 }
354 }
355
356 foreach( $this->mFilteredExifData as $k => $v ) {
357 if ( !$this->validate($k, $v) ) {
358 $this->debug( $v, __FUNCTION__, "'$k' contained invalid data" );
359 unset( $this->mFilteredExifData[$k] );
360 }
361 }
362 }
363
364 /**
365 * @todo document
366 */
367 function makeFormattedData( ) {
368 $format = new FormatExif( $this->getFilteredData() );
369 $this->mFormattedExifData = $format->getFormattedData();
370 }
371 /**#@-*/
372
373 /**#@+
374 * @return array
375 */
376 /**
377 * Get $this->mRawExifData
378 */
379 function getData() {
380 return $this->mRawExifData;
381 }
382
383 /**
384 * Get $this->mFilteredExifData
385 */
386 function getFilteredData() {
387 return $this->mFilteredExifData;
388 }
389
390 /**
391 * Get $this->mFormattedExifData
392 */
393 function getFormattedData() {
394 return $this->mFormattedExifData;
395 }
396 /**#@-*/
397
398 /**
399 * The version of the output format
400 *
401 * Before the actual metadata information is saved in the database we
402 * strip some of it since we don't want to save things like thumbnails
403 * which usually accompany Exif data. This value gets saved in the
404 * database along with the actual Exif data, and if the version in the
405 * database doesn't equal the value returned by this function the Exif
406 * data is regenerated.
407 *
408 * @return int
409 */
410 function version() {
411 return 1; // We don't need no bloddy constants!
412 }
413
414 /**#@+
415 * Validates if a tag value is of the type it should be according to the Exif spec
416 *
417 * @private
418 *
419 * @param $in Mixed: the input value to check
420 * @return bool
421 */
422 function isByte( $in ) {
423 if ( !is_array( $in ) && sprintf('%d', $in) == $in && $in >= 0 && $in <= 255 ) {
424 $this->debug( $in, __FUNCTION__, true );
425 return true;
426 } else {
427 $this->debug( $in, __FUNCTION__, false );
428 return false;
429 }
430 }
431
432 function isASCII( $in ) {
433 if ( is_array( $in ) ) {
434 return false;
435 }
436
437 if ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) {
438 $this->debug( $in, __FUNCTION__, 'found a character not in our whitelist' );
439 return false;
440 }
441
442 if ( preg_match( '/^\s*$/', $in ) ) {
443 $this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' );
444 return false;
445 }
446
447 return true;
448 }
449
450 function isShort( $in ) {
451 if ( !is_array( $in ) && sprintf('%d', $in) == $in && $in >= 0 && $in <= 65536 ) {
452 $this->debug( $in, __FUNCTION__, true );
453 return true;
454 } else {
455 $this->debug( $in, __FUNCTION__, false );
456 return false;
457 }
458 }
459
460 function isLong( $in ) {
461 if ( !is_array( $in ) && sprintf('%d', $in) == $in && $in >= 0 && $in <= 4294967296 ) {
462 $this->debug( $in, __FUNCTION__, true );
463 return true;
464 } else {
465 $this->debug( $in, __FUNCTION__, false );
466 return false;
467 }
468 }
469
470 function isRational( $in ) {
471 $m = array();
472 if ( !is_array( $in ) && @preg_match( '/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m ) ) { # Avoid division by zero
473 return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
474 } else {
475 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
476 return false;
477 }
478 }
479
480 function isUndefined( $in ) {
481 if ( !is_array( $in ) && preg_match( '/^\d{4}$/', $in ) ) { // Allow ExifVersion and FlashpixVersion
482 $this->debug( $in, __FUNCTION__, true );
483 return true;
484 } else {
485 $this->debug( $in, __FUNCTION__, false );
486 return false;
487 }
488 }
489
490 function isSlong( $in ) {
491 if ( $this->isLong( abs( $in ) ) ) {
492 $this->debug( $in, __FUNCTION__, true );
493 return true;
494 } else {
495 $this->debug( $in, __FUNCTION__, false );
496 return false;
497 }
498 }
499
500 function isSrational( $in ) {
501 $m = array();
502 if ( !is_array( $in ) && preg_match( '/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m ) ) { # Avoid division by zero
503 return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
504 } else {
505 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
506 return false;
507 }
508 }
509 /**#@-*/
510
511 /**
512 * Validates if a tag has a legal value according to the Exif spec
513 *
514 * @private
515 *
516 * @param $tag String: the tag to check.
517 * @param $val Mixed: the value of the tag.
518 * @return bool
519 */
520 function validate( $tag, $val ) {
521 $debug = "tag is '$tag'";
522 // Does not work if not typecast
523 switch( (string)$this->mFlatExifTags[$tag] ) {
524 case (string)Exif::BYTE:
525 $this->debug( $val, __FUNCTION__, $debug );
526 return $this->isByte( $val );
527 case (string)Exif::ASCII:
528 $this->debug( $val, __FUNCTION__, $debug );
529 return $this->isASCII( $val );
530 case (string)Exif::SHORT:
531 $this->debug( $val, __FUNCTION__, $debug );
532 return $this->isShort( $val );
533 case (string)Exif::LONG:
534 $this->debug( $val, __FUNCTION__, $debug );
535 return $this->isLong( $val );
536 case (string)Exif::RATIONAL:
537 $this->debug( $val, __FUNCTION__, $debug );
538 return $this->isRational( $val );
539 case (string)Exif::UNDEFINED:
540 $this->debug( $val, __FUNCTION__, $debug );
541 return $this->isUndefined( $val );
542 case (string)Exif::SLONG:
543 $this->debug( $val, __FUNCTION__, $debug );
544 return $this->isSlong( $val );
545 case (string)Exif::SRATIONAL:
546 $this->debug( $val, __FUNCTION__, $debug );
547 return $this->isSrational( $val );
548 case (string)Exif::SHORT.','.Exif::LONG:
549 $this->debug( $val, __FUNCTION__, $debug );
550 return $this->isShort( $val ) || $this->isLong( $val );
551 default:
552 $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
553 return false;
554 }
555 }
556
557 /**
558 * Convenience function for debugging output
559 *
560 * @private
561 *
562 * @param $in Mixed:
563 * @param $fname String:
564 * @param $action Mixed: , default NULL.
565 */
566 function debug( $in, $fname, $action = NULL ) {
567 $type = gettype( $in );
568 $class = ucfirst( __CLASS__ );
569 if ( $type === 'array' )
570 $in = print_r( $in, true );
571
572 if ( $action === true )
573 wfDebugLog( $this->log, "$class::$fname: accepted: '$in' (type: $type)\n");
574 elseif ( $action === false )
575 wfDebugLog( $this->log, "$class::$fname: rejected: '$in' (type: $type)\n");
576 elseif ( $action === null )
577 wfDebugLog( $this->log, "$class::$fname: input was: '$in' (type: $type)\n");
578 else
579 wfDebugLog( $this->log, "$class::$fname: $action (type: $type; content: '$in')\n");
580 }
581
582 /**
583 * Convenience function for debugging output
584 *
585 * @private
586 *
587 * @param $fname String: the name of the function calling this function
588 * @param $io Boolean: Specify whether we're beginning or ending
589 */
590 function debugFile( $fname, $io ) {
591 $class = ucfirst( __CLASS__ );
592 if ( $io ) {
593 wfDebugLog( $this->log, "$class::$fname: begin processing: '{$this->basename}'\n" );
594 } else {
595 wfDebugLog( $this->log, "$class::$fname: end processing: '{$this->basename}'\n" );
596 }
597 }
598
599 }
600
601 /**
602 * @package MediaWiki
603 * @subpackage Metadata
604 */
605 class FormatExif {
606 /**
607 * The Exif data to format
608 *
609 * @var array
610 * @private
611 */
612 var $mExif;
613
614 /**
615 * Constructor
616 *
617 * @param $exif Array: the Exif data to format ( as returned by
618 * Exif::getFilteredData() )
619 */
620 function FormatExif( $exif ) {
621 $this->mExif = $exif;
622 }
623
624 /**
625 * Numbers given by Exif user agents are often magical, that is they
626 * should be replaced by a detailed explanation depending on their
627 * value which most of the time are plain integers. This function
628 * formats Exif values into human readable form.
629 *
630 * @return array
631 */
632 function getFormattedData() {
633 global $wgLang;
634
635 $tags =& $this->mExif;
636
637 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
638 unset( $tags['ResolutionUnit'] );
639
640 foreach( $tags as $tag => $val ) {
641 switch( $tag ) {
642 case 'Compression':
643 switch( $val ) {
644 case 1: case 6:
645 $tags[$tag] = $this->msg( $tag, $val );
646 break;
647 default:
648 $tags[$tag] = $val;
649 break;
650 }
651 break;
652
653 case 'PhotometricInterpretation':
654 switch( $val ) {
655 case 2: case 6:
656 $tags[$tag] = $this->msg( $tag, $val );
657 break;
658 default:
659 $tags[$tag] = $val;
660 break;
661 }
662 break;
663
664 case 'Orientation':
665 switch( $val ) {
666 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
667 $tags[$tag] = $this->msg( $tag, $val );
668 break;
669 default:
670 $tags[$tag] = $val;
671 break;
672 }
673 break;
674
675 case 'PlanarConfiguration':
676 switch( $val ) {
677 case 1: case 2:
678 $tags[$tag] = $this->msg( $tag, $val );
679 break;
680 default:
681 $tags[$tag] = $val;
682 break;
683 }
684 break;
685
686 // TODO: YCbCrSubSampling
687 // TODO: YCbCrPositioning
688
689 case 'XResolution':
690 case 'YResolution':
691 switch( $resolutionunit ) {
692 case 2:
693 $tags[$tag] = $this->msg( 'XYResolution', 'i', $this->formatNum( $val ) );
694 break;
695 case 3:
696 $this->msg( 'XYResolution', 'c', $this->formatNum( $val ) );
697 break;
698 default:
699 $tags[$tag] = $val;
700 break;
701 }
702 break;
703
704 // TODO: YCbCrCoefficients #p27 (see annex E)
705 case 'ExifVersion': case 'FlashpixVersion':
706 $tags[$tag] = "$val"/100;
707 break;
708
709 case 'ColorSpace':
710 switch( $val ) {
711 case 1: case 'FFFF.H':
712 $tags[$tag] = $this->msg( $tag, $val );
713 break;
714 default:
715 $tags[$tag] = $val;
716 break;
717 }
718 break;
719
720 case 'ComponentsConfiguration':
721 switch( $val ) {
722 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
723 $tags[$tag] = $this->msg( $tag, $val );
724 break;
725 default:
726 $tags[$tag] = $val;
727 break;
728 }
729 break;
730
731 case 'DateTime':
732 case 'DateTimeOriginal':
733 case 'DateTimeDigitized':
734 if( 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 ?>