2 if (defined('MEDIAWIKI')) {
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
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.
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.
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
26 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
31 * Exif tag type definition
33 define('MW_EXIF_BYTE', 1); # An 8-bit 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.
46 * @subpackage Metadata
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.
63 * A one dimentional array of all Exif tags
68 * A one dimentional array of all Exif tags that we'd want to save in
69 * the database or present on a page.
80 * Page numbers here refer to pages in the EXIF 2.2 standard
82 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
85 # TIFF Rev. 6.0 Attribute Information (p22)
87 # Tags relating to image structure
89 'ImageWidth' => MW_EXIF_SHORT
.','.MW_EXIF_LONG
, # Image width
90 'ImageLength' => MW_EXIF_SHORT
.','.MW_EXIF_LONG
, # Image height
91 'BitsPerSample' => MW_EXIF_SHORT
, # Number of bits per component
92 # "When a primary image is JPEG compressed, this designation is not"
93 # "necessary and is omitted." (p23)
94 'Compression' => MW_EXIF_SHORT
, # Compression scheme #p23
95 'PhotometricInterpretation' => MW_EXIF_SHORT
, # Pixel composition #p23
96 'Orientation' => MW_EXIF_SHORT
, # Orientation of image #p24
97 'SamplesPerPixel' => MW_EXIF_SHORT
, # Number of components
98 'PlanarConfiguration' => MW_EXIF_SHORT
, # Image data arrangement #p24
99 'YCbCrSubSampling' => MW_EXIF_SHORT
, # Subsampling ratio of Y to C #p24
100 'YCbCrPositioning' => MW_EXIF_SHORT
, # Y and C positioning #p24-25
101 'XResolution' => MW_EXIF_RATIONAL
, # Image resolution in width direction
102 'YResolution' => MW_EXIF_RATIONAL
, # Image resolution in height direction
103 'ResolutionUnit' => MW_EXIF_SHORT
, # Unit of X and Y resolution #(p26)
106 # Tags relating to recording offset
108 'StripOffsets' => MW_EXIF_SHORT
.','.MW_EXIF_LONG
, # Image data location
109 'RowsPerStrip' => MW_EXIF_SHORT
.','.MW_EXIF_LONG
, # Number of rows per strip
110 'StripByteCounts' => MW_EXIF_SHORT
.','.MW_EXIF_LONG
, # Bytes per compressed strip
111 'JPEGInterchangeFormat' => MW_EXIF_SHORT
.','.MW_EXIF_LONG
, # Offset to JPEG SOI
112 'JPEGInterchangeFormatLength' => MW_EXIF_SHORT
.','.MW_EXIF_LONG
, # Bytes of JPEG data
115 # Tags relating to image data characteristics
116 'characteristics' => array(
117 'TransferFunction' => MW_EXIF_SHORT
, # Transfer function
118 'WhitePoint' => MW_EXIF_RATIONAL
, # White point chromaticity
119 'PrimaryChromaticities' => MW_EXIF_RATIONAL
, # Chromaticities of primarities
120 'YCbCrCoefficients' => MW_EXIF_RATIONAL
, # Color space transformation matrix coefficients #p27
121 'ReferenceBlackWhite' => MW_EXIF_RATIONAL
# Pair of black and white reference values
126 'DateTime' => MW_EXIF_ASCII
, # File change date and time
127 'ImageDescription' => MW_EXIF_ASCII
, # Image title
128 'Make' => MW_EXIF_ASCII
, # Image input equipment manufacturer
129 'Model' => MW_EXIF_ASCII
, # Image input equipment model
130 'Software' => MW_EXIF_ASCII
, # Software used
131 'Artist' => MW_EXIF_ASCII
, # Person who created the image
132 'Copyright' => MW_EXIF_ASCII
, # Copyright holder
136 # Exif IFD Attribute Information (p30-31)
138 # Tags relating to version
140 # TODO: NOTE: Nonexistence of this field is taken to mean nonconformance
141 # to the EXIF 2.1 AND 2.2 standards
142 'ExifVersion' => MW_EXIF_UNDEFINED
, # Exif version
143 'FlashpixVersion' => MW_EXIF_UNDEFINED
, # Supported Flashpix version #p32
146 # Tags relating to Image Data Characteristics
147 'characteristics' => array(
148 'ColorSpace' => MW_EXIF_SHORT
, # Color space information #p32
151 # Tags relating to image configuration
152 'configuration' => array(
153 'ComponentsConfiguration' => MW_EXIF_UNDEFINED
, # Meaning of each component #p33
154 'CompressedBitsPerPixel' => MW_EXIF_RATIONAL
, # Image compression mode
155 'PixelYDimension' => MW_EXIF_SHORT
.','.MW_EXIF_LONG
, # Valid image width
156 'PixelXDimension' => MW_EXIF_SHORT
.','.MW_EXIF_LONG
, # Valind image height
159 # Tags relating to related user information
161 'MakerNote' => MW_EXIF_UNDEFINED
, # Manufacturer notes
162 'UserComment' => MW_EXIF_UNDEFINED
, # User comments #p34
165 # Tags relating to related file information
167 'RelatedSoundFile' => MW_EXIF_ASCII
, # Related audio file
170 # Tags relating to date and time
171 'dateandtime' => array(
172 'DateTimeOriginal' => MW_EXIF_ASCII
, # Date and time of original data generation #p36
173 'DateTimeDigitized' => MW_EXIF_ASCII
, # Date and time of original data generation
174 'SubSecTime' => MW_EXIF_ASCII
, # DateTime subseconds
175 'SubSecTimeOriginal' => MW_EXIF_ASCII
, # DateTimeOriginal subseconds
176 'SubSecTimeDigitized' => MW_EXIF_ASCII
, # DateTimeDigitized subseconds
179 # Tags relating to picture-taking conditions (p31)
180 'conditions' => array(
181 'ExposureTime' => MW_EXIF_RATIONAL
, # Exposure time
182 'FNumber' => MW_EXIF_RATIONAL
, # F Number
183 'ExposureProgram' => MW_EXIF_SHORT
, # Exposure Program #p38
184 'SpectralSensitivity' => MW_EXIF_ASCII
, # Spectral sensitivity
185 'ISOSpeedRatings' => MW_EXIF_SHORT
, # ISO speed rating
186 'OECF' => MW_EXIF_UNDEFINED
, # Optoelectronic conversion factor
187 'ShutterSpeedValue' => MW_EXIF_SRATIONAL
, # Shutter speed
188 'ApertureValue' => MW_EXIF_RATIONAL
, # Aperture
189 'BrightnessValue' => MW_EXIF_SRATIONAL
, # Brightness
190 'ExposureBiasValue' => MW_EXIF_SRATIONAL
, # Exposure bias
191 'MaxApertureValue' => MW_EXIF_RATIONAL
, # Maximum land aperture
192 'SubjectDistance' => MW_EXIF_RATIONAL
, # Subject distance
193 'MeteringMode' => MW_EXIF_SHORT
, # Metering mode #p40
194 'LightSource' => MW_EXIF_SHORT
, # Light source #p40-41
195 'Flash' => MW_EXIF_SHORT
, # Flash #p41-42
196 'FocalLength' => MW_EXIF_RATIONAL
, # Lens focal length
197 'SubjectArea' => MW_EXIF_SHORT
, # Subject area
198 'FlashEnergy' => MW_EXIF_RATIONAL
, # Flash energy
199 'SpatialFrequencyResponse' => MW_EXIF_UNDEFINED
, # Spatial frequency response
200 'FocalPlaneXResolution' => MW_EXIF_RATIONAL
, # Focal plane X resolution
201 'FocalPlaneYResolution' => MW_EXIF_RATIONAL
, # Focal plane Y resolution
202 'FocalPlaneResolutionUnit' => MW_EXIF_SHORT
, # Focal plane resolution unit
203 'SubjectLocation' => MW_EXIF_SHORT
, # Subject location
204 'ExposureIndex' => MW_EXIF_RATIONAL
, # Exposure index
205 'SensingMethod' => MW_EXIF_SHORT
, # Sensing method #p46
206 'FileSource' => MW_EXIF_UNDEFINED
, # File source #p47
207 'SceneType' => MW_EXIF_UNDEFINED
, # Scene type #p47
208 'CFAPattern' => MW_EXIF_UNDEFINED
, # CFA pattern
209 'CustomRendered' => MW_EXIF_SHORT
, # Custom image processing #p48
210 'ExposureMode' => MW_EXIF_SHORT
, # Exposure mode #p48
211 'WhiteBalance' => MW_EXIF_SHORT
, # White Balance #p49
212 'DigitalZoomRatio' => MW_EXIF_RATIONAL
, # Digital zoom ration
213 'FocalLengthIn35mmFilm' => MW_EXIF_SHORT
, # Focal length in 35 mm film
214 'SceneCaptureType' => MW_EXIF_SHORT
, # Scene capture type #p49
215 'GainControl' => MW_EXIF_RATIONAL
, # Scene control #p49-50
216 'Contrast' => MW_EXIF_SHORT
, # Contrast #p50
217 'Saturation' => MW_EXIF_SHORT
, # Saturation #p50
218 'Sharpness' => MW_EXIF_SHORT
, # Sharpness #p50
219 'DeviceSettingDescription' => MW_EXIF_UNDEFINED
, # Desice settings description
220 'SubjectDistanceRange' => MW_EXIF_SHORT
, # Subject distance range #p51
224 'ImageUniqueID' => MW_EXIF_ASCII
, # Unique image ID
228 # GPS Attribute Information (p52)
230 'GPSVersionID' => MW_EXIF_BYTE
, # GPS tag version
231 'GPSLatitudeRef' => MW_EXIF_ASCII
, # North or South Latitude #p52-53
232 'GPSLatitude' => MW_EXIF_RATIONAL
, # Latitude
233 'GPSLongitudeRef' => MW_EXIF_ASCII
, # East or West Longitude #p53
234 'GPSLongitude' => MW_EXIF_RATIONAL
, # Longitude
235 'GPSAltitudeRef' => MW_EXIF_BYTE
, # Altitude reference
236 'GPSAltitude' => MW_EXIF_RATIONAL
, # Altitude
237 'GPSTimeStamp' => MW_EXIF_RATIONAL
, # GPS time (atomic clock)
238 'GPSSatellites' => MW_EXIF_ASCII
, # Satellites used for measurement
239 'GPSStatus' => MW_EXIF_ASCII
, # Receiver status #p54
240 'GPSMeasureMode' => MW_EXIF_ASCII
, # Measurement mode #p54-55
241 'GPSDOP' => MW_EXIF_RATIONAL
, # Measurement precision
242 'GPSSpeedRef' => MW_EXIF_ASCII
, # Speed unit #p55
243 'GPSSpeed' => MW_EXIF_RATIONAL
, # Speed of GPS receiver
244 'GPSTrackRef' => MW_EXIF_ASCII
, # Reference for direction of movement #p55
245 'GPSTrack' => MW_EXIF_RATIONAL
, # Direction of movement
246 'GPSImgDirectionRef' => MW_EXIF_ASCII
, # Reference for direction of image #p56
247 'GPSImgDirection' => MW_EXIF_RATIONAL
, # Direction of image
248 'GPSMapDatum' => MW_EXIF_ASCII
, # Geodetic survey data used
249 'GPSDestLatitudeRef' => MW_EXIF_ASCII
, # Reference for latitude of destination #p56
250 'GPSDestLatitude' => MW_EXIF_RATIONAL
, # Latitude destination
251 'GPSDestLongitudeRef' => MW_EXIF_ASCII
, # Reference for longitude of destination #p57
252 'GPSDestLongitude' => MW_EXIF_RATIONAL
, # Longitude of destination
253 'GPSDestBearingRef' => MW_EXIF_ASCII
, # Reference for bearing of destination #p57
254 'GPSDestBearing' => MW_EXIF_RATIONAL
, # Bearing of destination
255 'GPSDestDistanceRef' => MW_EXIF_ASCII
, # Reference for distance to destination #p57-58
256 'GPSDestDistance' => MW_EXIF_RATIONAL
, # Distance to destination
257 'GPSProcessingMethod' => MW_EXIF_UNDEFINED
, # Name of GPS processing method
258 'GPSAreaInformation' => MW_EXIF_UNDEFINED
, # Name of GPS area
259 'GPSDateStamp' => MW_EXIF_ASCII
, # GPS date
260 'GPSDifferential' => MW_EXIF_SHORT
, # GPS differential correction
264 $this->makeFlatExifTags();
265 $this->makeValidExifTags();
269 * Get the raw list of exiftags
279 * Generate a flat list of the exif tags
281 function makeFlatExifTags() {
282 $exif = $this->getExif();
283 $this->extractTags( $exif );
287 * A recursing extractor function used by makeFlatExifTags()
289 function extractTags( $tagset ) {
290 foreach( $tagset as $key => $val ) {
291 if( is_array( $val ) ) {
292 $this->extractTags( $val );
294 $this->mFlatExif
[$key] = $val;
300 * Produce a list of all Exif tags appropriate for user output
302 * Produce a list of all tags that we want to show in output, in order not to
303 * output binary gibberish such as raw thumbnails we strip all tags
304 * with the datatype of UNDEFINED.
306 * @todo We might actually want to display some of the UNDEFINED
307 * stuff, but we strip it for now.
309 function makeValidExifTags() {
310 foreach( $this->mFlatExif
as $key => $val ) {
311 if( strpos( $val, (string)MW_EXIF_UNDEFINED
) !== false ) {
314 $this->mValidExif
[] = $key;
319 * The version of the output format
321 * Before the actual metadata information is saved in the database we
322 * strip some of it since we don't want to save things like thumbnails
323 * which usually accompany Exif data. This value gets saved in the
324 * database along with the actual Exif data, and if the version in the
325 * database doesn't equal the value returned by this function the Exif
326 * data is regenerated.
331 return 1; // We don't need no bloddy constants!
335 * Validates if a tag value is of the type it should be according to the Exif spec
337 * @param mixed $in The input value to check
340 function isByte( $in ) {
341 return is_numeric( $in ) && $in >= 0 && $in <= 255;
344 function isASCII( $in ) {
345 return true; // TODO: FIXME
348 function isShort( $in ) {
349 return is_numeric( $in ) && $in >= 0 && $in <= 65536;
352 function isLong( $in ) {
353 return is_numeric( $in ) && $in >= 0 && $in <= 4294967296;
357 function isRational( $in ) {
358 $in = explode( '/', $in, 2 );
359 return $this->isLong( $in[0] ) && $this->isLong( $in[1] );
362 function isUndefined( $in ) {
366 function isSlong( $in ) {
367 return $this->isLong( abs( $in ) );
370 function isSrational( $in ) {
371 $in = explode( '/', $in, 2 );
372 return $this->isSlong( $in[0] ) && $this->isSlong( $in[1] );
377 * Validates if a tag has a legal value according to the Exif spec, presumes
378 * that the given tag is valid ( has been checked in advance with
379 * $this->mValidExif )
381 * @param string $tag The tag to check
382 * @param mixed $val The value of the tag
385 function validate( $tag, $val ) {
386 // Fucks up if not typecast
387 switch( (string)$this->mFlatExif
[$tag] ) {
388 case (string)MW_EXIF_BYTE
:
389 return $this->isByte( $val );
390 case (string)MW_EXIF_ASCII
:
391 return $this->isASCII( $val );
392 case (string)MW_EXIF_SHORT
:
393 return $this->isShort( $val );
394 case (string)MW_EXIF_LONG
:
395 return $this->isLong( $val );
396 case (string)MW_EXIF_RATIONAL
:
397 return $this->isRational( $val );
398 case (string)MW_EXIF_UNDEFINED
:
399 return $this->isUndefined( $val );
400 case (string)MW_EXIF_SLONG
:
401 return $this->isSlong( $val );
402 case (string)MW_EXIF_SRATIONAL
:
403 return $this->isSrational( $val );
404 case (string)MW_EXIF_SHORT
.','.MW_EXIF_LONG
:
405 return $this->isShort( $val ) ||
$this->isLong( $val );
407 wfDebug( "Exif: The tag \"$tag\" had an invalid value: \"$val\"\n" );
413 * Numbers given by Exif user agents are often magical, that is they
414 * should be replaced by a detailed explanation depending on their
415 * value which most of the time are plain integers. This function
416 * formats Exif values into human readable form.
418 * @param string $tag The tag to be formatted
419 * @param mixed $val The value of the tag
422 function format( $tag, $val ) {
429 return $this->msg( $tag, $val );
432 case 'PhotometricInterpretation':
435 return $this->msg( $tag, $val );
440 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
441 return $this->msg( $tag, $val );
444 // TODO: If this field does not exist use 1
445 case 'PlanarConfiguration':
448 return $this->msg( $tag, $val );
451 // TODO: YCbCrSubSampling
452 // TODO: YCbCrPositioning
453 // TODO: If this field does not exists use 2
454 case 'ResolutionUnit': #p26
457 return $this->msg( $tag, $val );
460 // TODO: YCbCrCoefficients #p27 (see annex E)
461 case 'ExifVersion': case 'FlashpixVersion':
466 case 1: case 'FFFF.H':
467 return $this->msg( $tag, $val );
470 case 'ComponentsConfiguration':
472 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
473 return $this->msg( $tag, $val );
477 case 'DateTimeOriginal':
478 case 'DateTimeDigitized':
479 return $wgLang->timeanddate( wfTimestamp(TS_MW
, $val) );
481 case 'ExposureProgram':
483 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
484 return $this->msg( $tag, $val );
488 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
489 return $this->msg( $tag, $val );
493 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
494 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
495 case 21: case 22: case 23: case 24: case 255:
496 return $this->msg( $tag, $val );
499 case 'SensingMethod':
501 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
502 return $this->msg( $tag, $val );
507 return $this->msg( $tag, $val );
512 return $this->msg( $tag, $val );
514 case 'CustomRendered':
517 return $this->msg( $tag, $val );
521 case 0: case 1: case 2:
522 return $this->msg( $tag, $val );
527 return $this->msg( $tag, $val );
529 case 'SceneCaptureType':
531 case 0: case 1: case 2: case 3:
532 return $this->msg( $tag, $val );
536 case 0: case 1: case 2: case 3: case 4:
537 return $this->msg( $tag, $val );
541 case 0: case 1: case 2:
542 return $this->msg( $tag, $val );
546 case 0: case 1: case 2:
547 return $this->msg( $tag, $val );
551 case 0: case 1: case 2:
552 return $this->msg( $tag, $val );
554 case 'SubjectDistanceRange':
556 case 0: case 1: case 2: case 3:
557 return $this->msg( $tag, $val );
559 case 'GPSLatitudeRef':
562 return $this->msg( $tag, $val );
564 case 'GPSLongitudeRef':
567 return $this->msg( $tag, $val );
572 return $this->msg( $tag, $val );
574 case 'GPSMeasureMode':
577 return $this->msg( $tag, $val );
581 case 'K': case 'M': case 'N':
582 return $this->msg( $tag, $val );
587 return $this->msg( $tag, $val );
589 case 'GPSImgDirectionRef':
592 return $this->msg( $tag, $val );
594 case 'GPSDestLatitudeRef':
597 return $this->msg( $tag, $val );
599 case 'GPSDestLongitudeRef':
602 return $this->msg( $tag, $val );
604 case 'GPSDestBearingRef':
607 return $this->msg( $tag, $val );
610 return $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
612 // This is not in the Exif standard, just a special
613 // case for our purposes which enables wikis to wikify
614 // the make and model to write articles about them.
615 case 'Make': case 'Model':
616 return wfMsg( strtolower( "exif-$tag-value" ), $val );
623 * Conviniance function for format()
625 * @param string $tag The tag name to pass on
626 * @param string $val The value of the tag
627 * @return string A wfMsg of "exif-$tag-$val" in lower case
629 function msg( $tag, $val ) {
630 return wfMsg( strtolower("exif-$tag-$val") );