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