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