Autoload Exif and move global defines to Exif:: namespace
[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 MW_EXIF_BYTE = 1; # An 8-bit (1-byte) unsigned integer.
43 const MW_EXIF_ASCII = 2; # An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.
44 const MW_EXIF_SHORT = 3; # A 16-bit (2-byte) unsigned integer.
45 const MW_EXIF_LONG = 4; # A 32-bit (4-byte) unsigned integer.
46 const MW_EXIF_RATIONAL = 5; # Two LONGs. The first LONG is the numerator and the second LONG expresses the denominator
47 const MW_EXIF_UNDEFINED = 7; # An 8-bit byte that can take any value depending on the field definition
48 const MW_EXIF_SLONG = 9; # A 32-bit (4-byte) signed integer (2's complement notation),
49 const MW_EXIF_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::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG, # Image width
121 'ImageLength' => Exif::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG, # Image height
122 'BitsPerSample' => Exif::MW_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::MW_EXIF_SHORT, # Compression scheme #p23
126 'PhotometricInterpretation' => Exif::MW_EXIF_SHORT, # Pixel composition #p23
127 'Orientation' => Exif::MW_EXIF_SHORT, # Orientation of image #p24
128 'SamplesPerPixel' => Exif::MW_EXIF_SHORT, # Number of components
129 'PlanarConfiguration' => Exif::MW_EXIF_SHORT, # Image data arrangement #p24
130 'YCbCrSubSampling' => Exif::MW_EXIF_SHORT, # Subsampling ratio of Y to C #p24
131 'YCbCrPositioning' => Exif::MW_EXIF_SHORT, # Y and C positioning #p24-25
132 'XResolution' => Exif::MW_EXIF_RATIONAL, # Image resolution in width direction
133 'YResolution' => Exif::MW_EXIF_RATIONAL, # Image resolution in height direction
134 'ResolutionUnit' => Exif::MW_EXIF_SHORT, # Unit of X and Y resolution #(p26)
135 ),
136
137 # Tags relating to recording offset
138 'offset' => array(
139 'StripOffsets' => Exif::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG, # Image data location
140 'RowsPerStrip' => Exif::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG, # Number of rows per strip
141 'StripByteCounts' => Exif::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG, # Bytes per compressed strip
142 'JPEGInterchangeFormat' => Exif::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG, # Offset to JPEG SOI
143 'JPEGInterchangeFormatLength' => Exif::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG, # Bytes of JPEG data
144 ),
145
146 # Tags relating to image data characteristics
147 'characteristics' => array(
148 'TransferFunction' => Exif::MW_EXIF_SHORT, # Transfer function
149 'WhitePoint' => Exif::MW_EXIF_RATIONAL, # White point chromaticity
150 'PrimaryChromaticities' => Exif::MW_EXIF_RATIONAL, # Chromaticities of primarities
151 'YCbCrCoefficients' => Exif::MW_EXIF_RATIONAL, # Color space transformation matrix coefficients #p27
152 'ReferenceBlackWhite' => Exif::MW_EXIF_RATIONAL # Pair of black and white reference values
153 ),
154
155 # Other tags
156 'other' => array(
157 'DateTime' => Exif::MW_EXIF_ASCII, # File change date and time
158 'ImageDescription' => Exif::MW_EXIF_ASCII, # Image title
159 'Make' => Exif::MW_EXIF_ASCII, # Image input equipment manufacturer
160 'Model' => Exif::MW_EXIF_ASCII, # Image input equipment model
161 'Software' => Exif::MW_EXIF_ASCII, # Software used
162 'Artist' => Exif::MW_EXIF_ASCII, # Person who created the image
163 'Copyright' => Exif::MW_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::MW_EXIF_UNDEFINED, # Exif version
174 'FlashpixVersion' => Exif::MW_EXIF_UNDEFINED, # Supported Flashpix version #p32
175 ),
176
177 # Tags relating to Image Data Characteristics
178 'characteristics' => array(
179 'ColorSpace' => Exif::MW_EXIF_SHORT, # Color space information #p32
180 ),
181
182 # Tags relating to image configuration
183 'configuration' => array(
184 'ComponentsConfiguration' => Exif::MW_EXIF_UNDEFINED, # Meaning of each component #p33
185 'CompressedBitsPerPixel' => Exif::MW_EXIF_RATIONAL, # Image compression mode
186 'PixelYDimension' => Exif::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG, # Valid image width
187 'PixelXDimension' => Exif::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG, # Valind image height
188 ),
189
190 # Tags relating to related user information
191 'user' => array(
192 'MakerNote' => Exif::MW_EXIF_UNDEFINED, # Manufacturer notes
193 'UserComment' => Exif::MW_EXIF_UNDEFINED, # User comments #p34
194 ),
195
196 # Tags relating to related file information
197 'related' => array(
198 'RelatedSoundFile' => Exif::MW_EXIF_ASCII, # Related audio file
199 ),
200
201 # Tags relating to date and time
202 'dateandtime' => array(
203 'DateTimeOriginal' => Exif::MW_EXIF_ASCII, # Date and time of original data generation #p36
204 'DateTimeDigitized' => Exif::MW_EXIF_ASCII, # Date and time of original data generation
205 'SubSecTime' => Exif::MW_EXIF_ASCII, # DateTime subseconds
206 'SubSecTimeOriginal' => Exif::MW_EXIF_ASCII, # DateTimeOriginal subseconds
207 'SubSecTimeDigitized' => Exif::MW_EXIF_ASCII, # DateTimeDigitized subseconds
208 ),
209
210 # Tags relating to picture-taking conditions (p31)
211 'conditions' => array(
212 'ExposureTime' => Exif::MW_EXIF_RATIONAL, # Exposure time
213 'FNumber' => Exif::MW_EXIF_RATIONAL, # F Number
214 'ExposureProgram' => Exif::MW_EXIF_SHORT, # Exposure Program #p38
215 'SpectralSensitivity' => Exif::MW_EXIF_ASCII, # Spectral sensitivity
216 'ISOSpeedRatings' => Exif::MW_EXIF_SHORT, # ISO speed rating
217 'OECF' => Exif::MW_EXIF_UNDEFINED, # Optoelectronic conversion factor
218 'ShutterSpeedValue' => Exif::MW_EXIF_SRATIONAL, # Shutter speed
219 'ApertureValue' => Exif::MW_EXIF_RATIONAL, # Aperture
220 'BrightnessValue' => Exif::MW_EXIF_SRATIONAL, # Brightness
221 'ExposureBiasValue' => Exif::MW_EXIF_SRATIONAL, # Exposure bias
222 'MaxApertureValue' => Exif::MW_EXIF_RATIONAL, # Maximum land aperture
223 'SubjectDistance' => Exif::MW_EXIF_RATIONAL, # Subject distance
224 'MeteringMode' => Exif::MW_EXIF_SHORT, # Metering mode #p40
225 'LightSource' => Exif::MW_EXIF_SHORT, # Light source #p40-41
226 'Flash' => Exif::MW_EXIF_SHORT, # Flash #p41-42
227 'FocalLength' => Exif::MW_EXIF_RATIONAL, # Lens focal length
228 'SubjectArea' => Exif::MW_EXIF_SHORT, # Subject area
229 'FlashEnergy' => Exif::MW_EXIF_RATIONAL, # Flash energy
230 'SpatialFrequencyResponse' => Exif::MW_EXIF_UNDEFINED, # Spatial frequency response
231 'FocalPlaneXResolution' => Exif::MW_EXIF_RATIONAL, # Focal plane X resolution
232 'FocalPlaneYResolution' => Exif::MW_EXIF_RATIONAL, # Focal plane Y resolution
233 'FocalPlaneResolutionUnit' => Exif::MW_EXIF_SHORT, # Focal plane resolution unit #p46
234 'SubjectLocation' => Exif::MW_EXIF_SHORT, # Subject location
235 'ExposureIndex' => Exif::MW_EXIF_RATIONAL, # Exposure index
236 'SensingMethod' => Exif::MW_EXIF_SHORT, # Sensing method #p46
237 'FileSource' => Exif::MW_EXIF_UNDEFINED, # File source #p47
238 'SceneType' => Exif::MW_EXIF_UNDEFINED, # Scene type #p47
239 'CFAPattern' => Exif::MW_EXIF_UNDEFINED, # CFA pattern
240 'CustomRendered' => Exif::MW_EXIF_SHORT, # Custom image processing #p48
241 'ExposureMode' => Exif::MW_EXIF_SHORT, # Exposure mode #p48
242 'WhiteBalance' => Exif::MW_EXIF_SHORT, # White Balance #p49
243 'DigitalZoomRatio' => Exif::MW_EXIF_RATIONAL, # Digital zoom ration
244 'FocalLengthIn35mmFilm' => Exif::MW_EXIF_SHORT, # Focal length in 35 mm film
245 'SceneCaptureType' => Exif::MW_EXIF_SHORT, # Scene capture type #p49
246 'GainControl' => Exif::MW_EXIF_RATIONAL, # Scene control #p49-50
247 'Contrast' => Exif::MW_EXIF_SHORT, # Contrast #p50
248 'Saturation' => Exif::MW_EXIF_SHORT, # Saturation #p50
249 'Sharpness' => Exif::MW_EXIF_SHORT, # Sharpness #p50
250 'DeviceSettingDescription' => Exif::MW_EXIF_UNDEFINED, # Desice settings description
251 'SubjectDistanceRange' => Exif::MW_EXIF_SHORT, # Subject distance range #p51
252 ),
253
254 'other' => array(
255 'ImageUniqueID' => Exif::MW_EXIF_ASCII, # Unique image ID
256 ),
257 ),
258
259 # GPS Attribute Information (p52)
260 'gps' => array(
261 'GPSVersionID' => Exif::MW_EXIF_BYTE, # GPS tag version
262 'GPSLatitudeRef' => Exif::MW_EXIF_ASCII, # North or South Latitude #p52-53
263 'GPSLatitude' => Exif::MW_EXIF_RATIONAL, # Latitude
264 'GPSLongitudeRef' => Exif::MW_EXIF_ASCII, # East or West Longitude #p53
265 'GPSLongitude' => Exif::MW_EXIF_RATIONAL, # Longitude
266 'GPSAltitudeRef' => Exif::MW_EXIF_BYTE, # Altitude reference
267 'GPSAltitude' => Exif::MW_EXIF_RATIONAL, # Altitude
268 'GPSTimeStamp' => Exif::MW_EXIF_RATIONAL, # GPS time (atomic clock)
269 'GPSSatellites' => Exif::MW_EXIF_ASCII, # Satellites used for measurement
270 'GPSStatus' => Exif::MW_EXIF_ASCII, # Receiver status #p54
271 'GPSMeasureMode' => Exif::MW_EXIF_ASCII, # Measurement mode #p54-55
272 'GPSDOP' => Exif::MW_EXIF_RATIONAL, # Measurement precision
273 'GPSSpeedRef' => Exif::MW_EXIF_ASCII, # Speed unit #p55
274 'GPSSpeed' => Exif::MW_EXIF_RATIONAL, # Speed of GPS receiver
275 'GPSTrackRef' => Exif::MW_EXIF_ASCII, # Reference for direction of movement #p55
276 'GPSTrack' => Exif::MW_EXIF_RATIONAL, # Direction of movement
277 'GPSImgDirectionRef' => Exif::MW_EXIF_ASCII, # Reference for direction of image #p56
278 'GPSImgDirection' => Exif::MW_EXIF_RATIONAL, # Direction of image
279 'GPSMapDatum' => Exif::MW_EXIF_ASCII, # Geodetic survey data used
280 'GPSDestLatitudeRef' => Exif::MW_EXIF_ASCII, # Reference for latitude of destination #p56
281 'GPSDestLatitude' => Exif::MW_EXIF_RATIONAL, # Latitude destination
282 'GPSDestLongitudeRef' => Exif::MW_EXIF_ASCII, # Reference for longitude of destination #p57
283 'GPSDestLongitude' => Exif::MW_EXIF_RATIONAL, # Longitude of destination
284 'GPSDestBearingRef' => Exif::MW_EXIF_ASCII, # Reference for bearing of destination #p57
285 'GPSDestBearing' => Exif::MW_EXIF_RATIONAL, # Bearing of destination
286 'GPSDestDistanceRef' => Exif::MW_EXIF_ASCII, # Reference for distance to destination #p57-58
287 'GPSDestDistance' => Exif::MW_EXIF_RATIONAL, # Distance to destination
288 'GPSProcessingMethod' => Exif::MW_EXIF_UNDEFINED, # Name of GPS processing method
289 'GPSAreaInformation' => Exif::MW_EXIF_UNDEFINED, # Name of GPS area
290 'GPSDateStamp' => Exif::MW_EXIF_ASCII, # GPS date
291 'GPSDifferential' => Exif::MW_EXIF_SHORT, # GPS differential correction
292 ),
293 );
294
295 $this->file = $file;
296 $this->basename = basename( $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 ( 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 ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) {
434 $this->debug( $in, __FUNCTION__, 'found a character not in our whitelist' );
435 return false;
436 }
437
438 if ( preg_match( "/^\s*$/", $in ) ) {
439 $this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' );
440 return false;
441 }
442
443 return true;
444 }
445
446 function isShort( $in ) {
447 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 65536 ) {
448 $this->debug( $in, __FUNCTION__, true );
449 return true;
450 } else {
451 $this->debug( $in, __FUNCTION__, false );
452 return false;
453 }
454 }
455
456 function isLong( $in ) {
457 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 4294967296 ) {
458 $this->debug( $in, __FUNCTION__, true );
459 return true;
460 } else {
461 $this->debug( $in, __FUNCTION__, false );
462 return false;
463 }
464 }
465
466 function isRational( $in ) {
467 if ( @preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
468 return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
469 } else {
470 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
471 return false;
472 }
473 }
474
475 function isUndefined( $in ) {
476 if ( preg_match( "/^\d{4}$/", $in ) ) { // Allow ExifVersion and FlashpixVersion
477 $this->debug( $in, __FUNCTION__, true );
478 return true;
479 } else {
480 $this->debug( $in, __FUNCTION__, false );
481 return false;
482 }
483 }
484
485 function isSlong( $in ) {
486 if ( $this->isLong( abs( $in ) ) ) {
487 $this->debug( $in, __FUNCTION__, true );
488 return true;
489 } else {
490 $this->debug( $in, __FUNCTION__, false );
491 return false;
492 }
493 }
494
495 function isSrational( $in ) {
496 if ( preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
497 return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
498 } else {
499 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
500 return false;
501 }
502 }
503 /**#@-*/
504
505 /**
506 * Validates if a tag has a legal value according to the Exif spec
507 *
508 * @private
509 *
510 * @param $tag String: the tag to check.
511 * @param $val Mixed: the value of the tag.
512 * @return bool
513 */
514 function validate( $tag, $val ) {
515 $debug = "tag is '$tag'";
516 // Fucks up if not typecast
517 switch( (string)$this->mFlatExifTags[$tag] ) {
518 case (string)Exif::MW_EXIF_BYTE:
519 $this->debug( $val, __FUNCTION__, $debug );
520 return $this->isByte( $val );
521 case (string)Exif::MW_EXIF_ASCII:
522 $this->debug( $val, __FUNCTION__, $debug );
523 return $this->isASCII( $val );
524 case (string)Exif::MW_EXIF_SHORT:
525 $this->debug( $val, __FUNCTION__, $debug );
526 return $this->isShort( $val );
527 case (string)Exif::MW_EXIF_LONG:
528 $this->debug( $val, __FUNCTION__, $debug );
529 return $this->isLong( $val );
530 case (string)Exif::MW_EXIF_RATIONAL:
531 $this->debug( $val, __FUNCTION__, $debug );
532 return $this->isRational( $val );
533 case (string)Exif::MW_EXIF_UNDEFINED:
534 $this->debug( $val, __FUNCTION__, $debug );
535 return $this->isUndefined( $val );
536 case (string)Exif::MW_EXIF_SLONG:
537 $this->debug( $val, __FUNCTION__, $debug );
538 return $this->isSlong( $val );
539 case (string)Exif::MW_EXIF_SRATIONAL:
540 $this->debug( $val, __FUNCTION__, $debug );
541 return $this->isSrational( $val );
542 case (string)Exif::MW_EXIF_SHORT.','.Exif::MW_EXIF_LONG:
543 $this->debug( $val, __FUNCTION__, $debug );
544 return $this->isShort( $val ) || $this->isLong( $val );
545 default:
546 $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
547 return false;
548 }
549 }
550
551 /**
552 * Conviniance function for debugging output
553 *
554 * @private
555 *
556 * @param $in Mixed:
557 * @param $fname String:
558 * @param $action Mixed: , default NULL.
559 */
560 function debug( $in, $fname, $action = NULL ) {
561 $type = gettype( $in );
562 $class = ucfirst( __CLASS__ );
563 if ( $type === 'array' )
564 $in = print_r( $in, true );
565
566 if ( $action === true )
567 wfDebugLog( $this->log, "$class::$fname: accepted: '$in' (type: $type)\n");
568 elseif ( $action === false )
569 wfDebugLog( $this->log, "$class::$fname: rejected: '$in' (type: $type)\n");
570 elseif ( $action === null )
571 wfDebugLog( $this->log, "$class::$fname: input was: '$in' (type: $type)\n");
572 else
573 wfDebugLog( $this->log, "$class::$fname: $action (type: $type; content: '$in')\n");
574 }
575
576 /**
577 * Conviniance function for debugging output
578 *
579 * @private
580 *
581 * @param $fname String: the name of the function calling this function
582 * @param $io Boolean: 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 /**
596 * @package MediaWiki
597 * @subpackage Metadata
598 */
599 class FormatExif {
600 /**
601 * The Exif data to format
602 *
603 * @var array
604 * @private
605 */
606 var $mExif;
607
608 /**
609 * Constructor
610 *
611 * @param $exif Array: the Exif data to format ( as returned by
612 * Exif::getFilteredData() )
613 */
614 function FormatExif( $exif ) {
615 $this->mExif = $exif;
616 }
617
618 /**
619 * Numbers given by Exif user agents are often magical, that is they
620 * should be replaced by a detailed explanation depending on their
621 * value which most of the time are plain integers. This function
622 * formats Exif values into human readable form.
623 *
624 * @return array
625 */
626 function getFormattedData() {
627 global $wgLang;
628
629 $tags =& $this->mExif;
630
631 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
632 unset( $tags['ResolutionUnit'] );
633
634 foreach( $tags as $tag => $val ) {
635 switch( $tag ) {
636 case 'Compression':
637 switch( $val ) {
638 case 1: case 6:
639 $tags[$tag] = $this->msg( $tag, $val );
640 break;
641 default:
642 $tags[$tag] = $val;
643 break;
644 }
645 break;
646
647 case 'PhotometricInterpretation':
648 switch( $val ) {
649 case 2: case 6:
650 $tags[$tag] = $this->msg( $tag, $val );
651 break;
652 default:
653 $tags[$tag] = $val;
654 break;
655 }
656 break;
657
658 case 'Orientation':
659 switch( $val ) {
660 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
661 $tags[$tag] = $this->msg( $tag, $val );
662 break;
663 default:
664 $tags[$tag] = $val;
665 break;
666 }
667 break;
668
669 case 'PlanarConfiguration':
670 switch( $val ) {
671 case 1: case 2:
672 $tags[$tag] = $this->msg( $tag, $val );
673 break;
674 default:
675 $tags[$tag] = $val;
676 break;
677 }
678 break;
679
680 // TODO: YCbCrSubSampling
681 // TODO: YCbCrPositioning
682
683 case 'XResolution':
684 case 'YResolution':
685 switch( $resolutionunit ) {
686 case 2:
687 $tags[$tag] = $this->msg( 'XYResolution', 'i', $this->formatNum( $val ) );
688 break;
689 case 3:
690 $this->msg( 'XYResolution', 'c', $this->formatNum( $val ) );
691 break;
692 default:
693 $tags[$tag] = $val;
694 break;
695 }
696 break;
697
698 // TODO: YCbCrCoefficients #p27 (see annex E)
699 case 'ExifVersion': case 'FlashpixVersion':
700 $tags[$tag] = "$val"/100;
701 break;
702
703 case 'ColorSpace':
704 switch( $val ) {
705 case 1: case 'FFFF.H':
706 $tags[$tag] = $this->msg( $tag, $val );
707 break;
708 default:
709 $tags[$tag] = $val;
710 break;
711 }
712 break;
713
714 case 'ComponentsConfiguration':
715 switch( $val ) {
716 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
717 $tags[$tag] = $this->msg( $tag, $val );
718 break;
719 default:
720 $tags[$tag] = $val;
721 break;
722 }
723 break;
724
725 case 'DateTime':
726 case 'DateTimeOriginal':
727 case 'DateTimeDigitized':
728 if( preg_match( "/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/", $val ) ) {
729 $tags[$tag] = $wgLang->timeanddate( wfTimestamp(TS_MW, $val) );
730 }
731 break;
732
733 case 'ExposureProgram':
734 switch( $val ) {
735 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
736 $tags[$tag] = $this->msg( $tag, $val );
737 break;
738 default:
739 $tags[$tag] = $val;
740 break;
741 }
742 break;
743
744 case 'SubjectDistance':
745 $tags[$tag] = $this->msg( $tag, '', $this->formatNum( $val ) );
746 break;
747
748 case 'MeteringMode':
749 switch( $val ) {
750 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
751 $tags[$tag] = $this->msg( $tag, $val );
752 break;
753 default:
754 $tags[$tag] = $val;
755 break;
756 }
757 break;
758
759 case 'LightSource':
760 switch( $val ) {
761 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
762 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
763 case 21: case 22: case 23: case 24: case 255:
764 $tags[$tag] = $this->msg( $tag, $val );
765 break;
766 default:
767 $tags[$tag] = $val;
768 break;
769 }
770 break;
771
772 // TODO: Flash
773 case 'FocalPlaneResolutionUnit':
774 switch( $val ) {
775 case 2:
776 $tags[$tag] = $this->msg( $tag, $val );
777 break;
778 default:
779 $tags[$tag] = $val;
780 break;
781 }
782 break;
783
784 case 'SensingMethod':
785 switch( $val ) {
786 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
787 $tags[$tag] = $this->msg( $tag, $val );
788 break;
789 default:
790 $tags[$tag] = $val;
791 break;
792 }
793 break;
794
795 case 'FileSource':
796 switch( $val ) {
797 case 3:
798 $tags[$tag] = $this->msg( $tag, $val );
799 break;
800 default:
801 $tags[$tag] = $val;
802 break;
803 }
804 break;
805
806 case 'SceneType':
807 switch( $val ) {
808 case 1:
809 $tags[$tag] = $this->msg( $tag, $val );
810 break;
811 default:
812 $tags[$tag] = $val;
813 break;
814 }
815 break;
816
817 case 'CustomRendered':
818 switch( $val ) {
819 case 0: case 1:
820 $tags[$tag] = $this->msg( $tag, $val );
821 break;
822 default:
823 $tags[$tag] = $val;
824 break;
825 }
826 break;
827
828 case 'ExposureMode':
829 switch( $val ) {
830 case 0: case 1: case 2:
831 $tags[$tag] = $this->msg( $tag, $val );
832 break;
833 default:
834 $tags[$tag] = $val;
835 break;
836 }
837 break;
838
839 case 'WhiteBalance':
840 switch( $val ) {
841 case 0: case 1:
842 $tags[$tag] = $this->msg( $tag, $val );
843 break;
844 default:
845 $tags[$tag] = $val;
846 break;
847 }
848 break;
849
850 case 'SceneCaptureType':
851 switch( $val ) {
852 case 0: case 1: case 2: case 3:
853 $tags[$tag] = $this->msg( $tag, $val );
854 break;
855 default:
856 $tags[$tag] = $val;
857 break;
858 }
859 break;
860
861 case 'GainControl':
862 switch( $val ) {
863 case 0: case 1: case 2: case 3: case 4:
864 $tags[$tag] = $this->msg( $tag, $val );
865 break;
866 default:
867 $tags[$tag] = $val;
868 break;
869 }
870 break;
871
872 case 'Contrast':
873 switch( $val ) {
874 case 0: case 1: case 2:
875 $tags[$tag] = $this->msg( $tag, $val );
876 break;
877 default:
878 $tags[$tag] = $val;
879 break;
880 }
881 break;
882
883 case 'Saturation':
884 switch( $val ) {
885 case 0: case 1: case 2:
886 $tags[$tag] = $this->msg( $tag, $val );
887 break;
888 default:
889 $tags[$tag] = $val;
890 break;
891 }
892 break;
893
894 case 'Sharpness':
895 switch( $val ) {
896 case 0: case 1: case 2:
897 $tags[$tag] = $this->msg( $tag, $val );
898 break;
899 default:
900 $tags[$tag] = $val;
901 break;
902 }
903 break;
904
905 case 'SubjectDistanceRange':
906 switch( $val ) {
907 case 0: case 1: case 2: case 3:
908 $tags[$tag] = $this->msg( $tag, $val );
909 break;
910 default:
911 $tags[$tag] = $val;
912 break;
913 }
914 break;
915
916 case 'GPSLatitudeRef':
917 case 'GPSDestLatitudeRef':
918 switch( $val ) {
919 case 'N': case 'S':
920 $tags[$tag] = $this->msg( 'GPSLatitude', $val );
921 break;
922 default:
923 $tags[$tag] = $val;
924 break;
925 }
926 break;
927
928 case 'GPSLongitudeRef':
929 case 'GPSDestLongitudeRef':
930 switch( $val ) {
931 case 'E': case 'W':
932 $tags[$tag] = $this->msg( 'GPSLongitude', $val );
933 break;
934 default:
935 $tags[$tag] = $val;
936 break;
937 }
938 break;
939
940 case 'GPSStatus':
941 switch( $val ) {
942 case 'A': case 'V':
943 $tags[$tag] = $this->msg( $tag, $val );
944 break;
945 default:
946 $tags[$tag] = $val;
947 break;
948 }
949 break;
950
951 case 'GPSMeasureMode':
952 switch( $val ) {
953 case 2: case 3:
954 $tags[$tag] = $this->msg( $tag, $val );
955 break;
956 default:
957 $tags[$tag] = $val;
958 break;
959 }
960 break;
961
962 case 'GPSSpeedRef':
963 case 'GPSDestDistanceRef':
964 switch( $val ) {
965 case 'K': case 'M': case 'N':
966 $tags[$tag] = $this->msg( 'GPSSpeed', $val );
967 break;
968 default:
969 $tags[$tag] = $val;
970 break;
971 }
972 break;
973
974 case 'GPSTrackRef':
975 case 'GPSImgDirectionRef':
976 case 'GPSDestBearingRef':
977 switch( $val ) {
978 case 'T': case 'M':
979 $tags[$tag] = $this->msg( 'GPSDirection', $val );
980 break;
981 default:
982 $tags[$tag] = $val;
983 break;
984 }
985 break;
986
987 case 'GPSDateStamp':
988 $tags[$tag] = $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
989 break;
990
991 // This is not in the Exif standard, just a special
992 // case for our purposes which enables wikis to wikify
993 // the make, model and software name to link to their articles.
994 case 'Make':
995 case 'Model':
996 case 'Software':
997 $tags[$tag] = $this->msg( $tag, '', $val );
998 break;
999
1000 case 'ExposureTime':
1001 // Show the pretty fraction as well as decimal version
1002 $tags[$tag] = wfMsg( 'exif-exposuretime-format',
1003 $this->formatFraction( $val ), $this->formatNum( $val ) );
1004 break;
1005
1006 case 'FNumber':
1007 $tags[$tag] = wfMsg( 'exif-fnumber-format',
1008 $this->formatNum( $val ) );
1009 break;
1010
1011 case 'FocalLength':
1012 $tags[$tag] = wfMsg( 'exif-focallength-format',
1013 $this->formatNum( $val ) );
1014 break;
1015
1016 default:
1017 $tags[$tag] = $this->formatNum( $val );
1018 break;
1019 }
1020 }
1021
1022 return $tags;
1023 }
1024
1025 /**
1026 * Conviniance function for getFormattedData()
1027 *
1028 * @private
1029 *
1030 * @param $tag String: the tag name to pass on
1031 * @param $val String: the value of the tag
1032 * @param $arg String: an argument to pass ($1)
1033 * @return string A wfMsg of "exif-$tag-$val" in lower case
1034 */
1035 function msg( $tag, $val, $arg = null ) {
1036 global $wgContLang;
1037
1038 if ($val === '')
1039 $val = 'value';
1040 return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg );
1041 }
1042
1043 /**
1044 * Format a number, convert numbers from fractions into floating point
1045 * numbers
1046 *
1047 * @private
1048 *
1049 * @param $num Mixed: the value to format
1050 * @return mixed A floating point number or whatever we were fed
1051 */
1052 function formatNum( $num ) {
1053 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) )
1054 return $m[2] != 0 ? $m[1] / $m[2] : $num;
1055 else
1056 return $num;
1057 }
1058
1059 /**
1060 * Format a rational number, reducing fractions
1061 *
1062 * @private
1063 *
1064 * @param $num Mixed: the value to format
1065 * @return mixed A floating point number or whatever we were fed
1066 */
1067 function formatFraction( $num ) {
1068 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) ) {
1069 $numerator = intval( $m[1] );
1070 $denominator = intval( $m[2] );
1071 $gcd = $this->gcd( $numerator, $denominator );
1072 if( $gcd != 0 ) {
1073 // 0 shouldn't happen! ;)
1074 return $numerator / $gcd . '/' . $denominator / $gcd;
1075 }
1076 }
1077 return $this->formatNum( $num );
1078 }
1079
1080 /**
1081 * Calculate the greatest common divisor of two integers.
1082 *
1083 * @param $a Integer: FIXME
1084 * @param $b Integer: FIXME
1085 * @return int
1086 * @private
1087 */
1088 function gcd( $a, $b ) {
1089 /*
1090 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1091 // Recursive form would be:
1092 if( $b == 0 )
1093 return $a;
1094 else
1095 return gcd( $b, $a % $b );
1096 */
1097 while( $b != 0 ) {
1098 $remainder = $a % $b;
1099
1100 // tail recursion...
1101 $a = $b;
1102 $b = $remainder;
1103 }
1104 return $a;
1105 }
1106 }
1107 ?>