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
32 * @subpackage Metadata
40 * Exif tags grouped by category, the tagname itself is the key and the type
41 * is the value, in the case of more than one possible value type they are
42 * seperated by commas.
49 * A one dimentional array of all Exif tags
54 * A one dimentional array of all Exif tags that we'd want to save in
55 * the database or present on a page.
66 * Exif tag type definition
68 define('BYTE', 1); # An 8-bit unsigned integer.
69 define('ASCII', 2); # An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.
70 define('SHORT', 3); # A 16-bit (2-byte) unsigned integer.
71 define('LONG', 4); # A 32-bit (4-byte) unsigned integer.
72 define('RATIONAL', 5); # Two LONGs. The first LONG is the numerator and the second LONG expresses the denominator
73 define('UNDEFINED', 7); # An 8-bit byte that can take any value depending on the field definition
74 define('SLONG', 9); # A 32-bit (4-byte) signed integer (2's complement notation),
75 define('SRATIONAL', 10); # Two SLONGs. The first SLONG is the numerator and the second SLONG is the denominator.
79 * Page numbers here refer to pages in the EXIF 2.2 standard
81 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
84 # TIFF Rev. 6.0 Attribute Information (p22)
86 # Tags relating to image structure
88 'ImageWidth' => SHORT
.','.LONG
, # Image width
89 'ImageLength' => SHORT
.','.LONG
, # Image height
90 'BitsPerSample' => SHORT
, # Number of bits per component
91 # "When a primary image is JPEG compressed, this designation is not"
92 # "necessary and is omitted." (p23)
93 'Compression' => SHORT
, # Compression scheme #p23
94 'PhotometricInterpretation' => SHORT
, # Pixel composition #p23
95 'Orientation' => SHORT
, # Orientation of image #p24
96 'SamplesPerPixel' => SHORT
, # Number of components
97 'PlanarConfiguration' => SHORT
, # Image data arrangement #p24
98 'YCbCrSubSampling' => SHORT
, # Subsampling ratio of Y to C #p24
99 'YCbCrPositioning' => SHORT
, # Y and C positioning #p24-25
100 'XResolution' => RATIONAL
, # Image resolution in width direction
101 'YResolution' => RATIONAL
, # Image resolution in height direction
102 'ResolutionUnit' => SHORT
, # Unit of X and Y resolution #(p26)
105 # Tags relating to recording offset
107 'StripOffsets' => SHORT
.','.LONG
, # Image data location
108 'RowsPerStrip' => SHORT
.','.LONG
, # Number of rows per strip
109 'StripByteCounts' => SHORT
.','.LONG
, # Bytes per compressed strip
110 'JPEGInterchangeFormat' => SHORT
.','.LONG
, # Offset to JPEG SOI
111 'JPEGInterchangeFormatLength' => SHORT
.','.LONG
, # Bytes of JPEG data
114 # Tags relating to image data characteristics
115 'characteristics' => array(
116 'TransferFunction' => SHORT
, # Transfer function
117 'WhitePoint' => RATIONAL
, # White point chromaticity
118 'PrimaryChromaticities' => RATIONAL
, # Chromaticities of primarities
119 'YCbCrCoefficients' => RATIONAL
, # Color space transformation matrix coefficients #p27
120 'ReferenceBlackWhite' => RATIONAL
# Pair of black and white reference values
125 'DateTime' => ASCII
, # File change date and time
126 'ImageDescription' => ASCII
, # Image title
127 'Make' => ASCII
, # Image input equipment manufacturer
128 'Model' => ASCII
, # Image input equipment model
129 'Software' => ASCII
, # Software used
130 'Artist' => ASCII
, # Person who created the image
131 'Copyright' => ASCII
, # Copyright holder
135 # Exif IFD Attribute Information (p30-31)
137 # Tags relating to version
139 # TODO: NOTE: Nonexistence of this field is taken to mean nonconformance
140 # to the EXIF 2.1 AND 2.2 standards
141 'ExifVersion' => UNDEFINED
, # Exif version
142 'FlashpixVersion' => UNDEFINED
, # Supported Flashpix version #p32
145 # Tags relating to Image Data Characteristics
146 'characteristics' => array(
147 'ColorSpace' => SHORT
, # Color space information #p32
150 # Tags relating to image configuration
151 'configuration' => array(
152 'ComponentsConfiguration' => UNDEFINED
, # Meaning of each component #p33
153 'CompressedBitsPerPixel' => RATIONAL
, # Image compression mode
154 'PixelYDimension' => SHORT
.','.LONG
, # Valid image width
155 'PixelXDimension' => SHORT
.','.LONG
, # Valind image height
158 # Tags relating to related user information
160 'MakerNote' => UNDEFINED
, # Manufacturer notes
161 'UserComment' => UNDEFINED
, # User comments #p34
164 # Tags relating to related file information
166 'RelatedSoundFile' => ASCII
, # Related audio file
169 # Tags relating to date and time
170 'dateandtime' => array(
171 'DateTimeOriginal' => ASCII
, # Date and time of original data generation #p36
172 'DateTimeDigitized' => ASCII
, # Date and time of original data generation
173 'SubSecTime' => ASCII
, # DateTime subseconds
174 'SubSecTimeOriginal' => ASCII
, # DateTimeOriginal subseconds
175 'SubSecTimeDigitized' => ASCII
, # DateTimeDigitized subseconds
178 # Tags relating to picture-taking conditions (p31)
179 'conditions' => array(
180 'ExposureTime' => RATIONAL
, # Exposure time
181 'FNumber' => RATIONAL
, # F Number
182 'ExposureProgram' => SHORT
, # Exposure Program #p38
183 'SpectralSensitivity' => ASCII
, # Spectral sensitivity
184 'ISOSpeedRatings' => SHORT
, # ISO speed rating
185 'OECF' => UNDEFINED
, # Optoelectronic conversion factor
186 'ShutterSpeedValue' => SRATIONAL
, # Shutter speed
187 'ApertureValue' => RATIONAL
, # Aperture
188 'BrightnessValue' => SRATIONAL
, # Brightness
189 'ExposureBiasValue' => SRATIONAL
, # Exposure bias
190 'MaxApertureValue' => RATIONAL
, # Maximum land aperture
191 'SubjectDistance' => RATIONAL
, # Subject distance
192 'MeteringMode' => SHORT
, # Metering mode #p40
193 'LightSource' => SHORT
, # Light source #p40-41
194 'Flash' => SHORT
, # Flash #p41-42
195 'FocalLength' => RATIONAL
, # Lens focal length
196 'SubjectArea' => SHORT
, # Subject area
197 'FlashEnergy' => RATIONAL
, # Flash energy
198 'SpatialFrequencyResponse' => UNDEFINED
, # Spatial frequency response
199 'FocalPlaneXResolution' => RATIONAL
, # Focal plane X resolution
200 'FocalPlaneYResolution' => RATIONAL
, # Focal plane Y resolution
201 'FocalPlaneResolutionUnit' => SHORT
, # Focal plane resolution unit
202 'SubjectLocation' => SHORT
, # Subject location
203 'ExposureIndex' => RATIONAL
, # Exposure index
204 'SensingMethod' => SHORT
, # Sensing method #p46
205 'FileSource' => UNDEFINED
, # File source #p47
206 'SceneType' => UNDEFINED
, # Scene type #p47
207 'CFAPattern' => UNDEFINED
, # CFA pattern
208 'CustomRendered' => SHORT
, # Custom image processing #p48
209 'ExposureMode' => SHORT
, # Exposure mode #p48
210 'WhiteBalance' => SHORT
, # White Balance #p49
211 'DigitalZoomRatio' => RATIONAL
, # Digital zoom ration
212 'FocalLengthIn35mmFilm' => SHORT
, # Focal length in 35 mm film
213 'SceneCaptureType' => SHORT
, # Scene capture type #p49
214 'GainControl' => RATIONAL
, # Scene control #p49-50
215 'Contrast' => SHORT
, # Contrast #p50
216 'Saturation' => SHORT
, # Saturation #p50
217 'Sharpness' => SHORT
, # Sharpness #p50
218 'DeviceSettingDescription' => UNDEFINED
, # Desice settings description
219 'SubjectDistanceRange' => SHORT
, # Subject distance range #p51
223 'ImageUniqueID' => ASCII
, # Unique image ID
227 # GPS Attribute Information (p52)
229 'GPSVersionID' => BYTE
, # GPS tag version
230 'GPSLatitudeRef' => ASCII
, # North or South Latitude #p52-53
231 'GPSLatitude' => RATIONAL
, # Latitude
232 'GPSLongitudeRef' => ASCII
, # East or West Longitude #p53
233 'GPSLongitude' => RATIONAL
, # Longitude
234 'GPSAltitudeRef' => BYTE
, # Altitude reference
235 'GPSAltitude' => RATIONAL
, # Altitude
236 'GPSTimeStamp' => RATIONAL
, # GPS time (atomic clock)
237 'GPSSatellites' => ASCII
, # Satellites used for measurement
238 'GPSStatus' => ASCII
, # Receiver status #p54
239 'GPSMeasureMode' => ASCII
, # Measurement mode #p54-55
240 'GPSDOP' => RATIONAL
, # Measurement precision
241 'GPSSpeedRef' => ASCII
, # Speed unit #p55
242 'GPSSpeed' => RATIONAL
, # Speed of GPS receiver
243 'GPSTrackRef' => ASCII
, # Reference for direction of movement #p55
244 'GPSTrack' => RATIONAL
, # Direction of movement
245 'GPSImgDirectionRef' => ASCII
, # Reference for direction of image #p56
246 'GPSImgDirection' => RATIONAL
, # Direction of image
247 'GPSMapDatum' => ASCII
, # Geodetic survey data used
248 'GPSDestLatitudeRef' => ASCII
, # Reference for latitude of destination #p56
249 'GPSDestLatitude' => RATIONAL
, # Latitude destination
250 'GPSDestLongitudeRef' => ASCII
, # Reference for longitude of destination #p57
251 'GPSDestLongitude' => RATIONAL
, # Longitude of destination
252 'GPSDestBearingRef' => ASCII
, # Reference for bearing of destination #p57
253 'GPSDestBearing' => RATIONAL
, # Bearing of destination
254 'GPSDestDistanceRef' => ASCII
, # Reference for distance to destination #p57-58
255 'GPSDestDistance' => RATIONAL
, # Distance to destination
256 'GPSProcessingMethod' => UNDEFINED
, # Name of GPS processing method
257 'GPSAreaInformation' => UNDEFINED
, # Name of GPS area
258 'GPSDateStamp' => ASCII
, # GPS date
259 'GPSDifferential' => SHORT
, # GPS differential correction
263 $this->makeFlatExifTags();
264 $this->makeValidExifTags();
268 * Get the raw list of exiftags
278 * Generate a flat list of the exif tags
280 function makeFlatExifTags() {
281 $exif = $this->getExif();
282 $this->extractTags( $exif );
286 * A recursing extractor function used by makeFlatExifTags()
288 function extractTags( $tagset ) {
289 foreach( $tagset as $key => $val ) {
290 if( is_array( $val ) ) {
291 $this->extractTags( $val );
293 $this->mFlatExif
[$key] = $val;
299 * Produce a list of all Exif tags appropriate for user output
301 * Produce a list of all tags that we want to show in output, in order not to
302 * output binary gibberish such as raw thumbnails we strip all tags
303 * with the datatype of UNDEFINED.
305 * @todo We might actually want to display some of the UNDEFINED
306 * stuff, but we strip it for now.
308 function makeValidExifTags() {
309 foreach($this->mFlatExif
as $key => $val) {
310 if (strpos( $val, (string)UNDEFINED
) !== false)
312 $this->mValidExif
[] = $key;
317 * The version of the output format
319 * Before the actual metadata information is saved in the database we
320 * strip some of it since we don't want to save things like thumbnails
321 * which usually accompany Exif data. This value gets saved in the
322 * database along with the actual Exif data, and if the version in the
323 * database doesn't equal the value returned by this function the Exif
324 * data is regenerated.
329 return 1; // We don't need no bloddy constants!
333 * Validates if a tag value is of the type it should be according to the Exif spec
335 * @param mixed $in The input value to check
338 function isByte( $in ) {
339 return is_numeric($in) && $in >= 0 && $in <= 255;
342 function isASCII( $in ) {
343 return true; // TODO: FIXME
346 function isShort( $in ) {
347 return is_numeric($in) && $in >= 0 && $in <= 65536;
350 function isLong( $in ) {
351 return is_numeric($in) && $in >= 0 && $in <= 4294967296;
355 function isRational( $in ) {
356 $in = explode( '/', $in, 2 );
357 return $this->isLong($in[0]) && $this->isLong($in[1]);
360 function isUndefined( $in ) {
364 function isSlong( $in ) {
365 return $this->isLong( abs( $in ) );
368 function isSrational( $in ) {
369 $in = explode( '/', $in, 2 );
370 return $this->isSlong($in[0]) && $this->isSlong($in[1]);
375 * Validates if a tag has a legal value according to the Exif spec, presumes
376 * that the given tag is valid ( has been checked in advance with
377 * $this->mValidExif )
379 * @param string $tag The tag to check
380 * @param mixed $val The value of the tag
383 function validate( $tag, $val ) {
384 // Fucks up if not typecast
385 switch((string)$this->mFlatExif
[$tag]) {
387 return $this->isByte( $val );
389 return $this->isASCII( $val );
391 return $this->isShort( $val );
393 return $this->isLong( $val );
394 case (string)RATIONAL
:
395 return $this->isRational( $val );
396 case (string)UNDEFINED
:
397 return $this->isUndefined( $val );
399 return $this->isSlong( $val );
400 case (string)SRATIONAL
:
401 return $this->isSrational( $val );
402 case (string)SHORT
.','.LONG
:
403 return $this->isShort( $val ) ||
$this->isLong( $val );
405 wfDebug( "Exif: The tag \"$tag\" had an invalid value: \"$val\"\n" );
411 * Numbers given by Exif user agents are often magical, that is they
412 * should be replaced by a detailed explanation depending on their
413 * value which most of the time are plain integers. This function
414 * formats Exif values into human readable form.
416 * @param string $tag The tag to be formatted
417 * @param mixed $val The value of the tag
420 function format( $tag, $val ) {
427 return $this->msg( $tag, $val );
430 case 'PhotometricInterpretation':
433 return $this->msg( $tag, $val );
438 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
439 return $this->msg( $tag, $val );
442 // TODO: If this field does not exist use 1
443 case 'PlanarConfiguration':
446 return $this->msg( $tag, $val );
449 // TODO: YCbCrSubSampling
450 // TODO: YCbCrPositioning
451 // TODO: If this field does not exists use 2
452 case 'ResolutionUnit': #p26
455 return $this->msg( $tag, $val );
458 // TODO: YCbCrCoefficients #p27 (see annex E)
459 case 'ExifVersion': case 'FlashpixVersion':
464 case 1: case 'FFFF.H':
465 return $this->msg( $tag, $val );
468 case 'ComponentsConfiguration':
470 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
471 return $this->msg( $tag, $val );
475 case 'DateTimeOriginal':
476 case 'DateTimeDigitized':
477 return $wgLang->timeanddate( wfTimestamp(TS_MW
, $val) );
478 case 'ExposureProgram':
480 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
481 return $this->msg( $tag, $val );
485 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
486 return $this->msg( $tag, $val );
490 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
491 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
492 case 21: case 22: case 23: case 24: case 255:
493 return $this->msg( $tag, $val );
496 case 'SensingMethod':
498 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
499 return $this->msg( $tag, $val );
504 return $this->msg( $tag, $val );
509 return $this->msg( $tag, $val );
511 case 'CustomRendered':
514 return $this->msg( $tag, $val );
518 case 0: case 1: case 2:
519 return $this->msg( $tag, $val );
524 return $this->msg( $tag, $val );
526 case 'SceneCaptureType':
528 case 0: case 1: case 2: case 3:
529 return $this->msg( $tag, $val );
533 case 0: case 1: case 2: case 3: case 4:
534 return $this->msg( $tag, $val );
538 case 0: case 1: case 2:
539 return $this->msg( $tag, $val );
543 case 0: case 1: case 2:
544 return $this->msg( $tag, $val );
548 case 0: case 1: case 2:
549 return $this->msg( $tag, $val );
551 case 'SubjectDistanceRange':
553 case 0: case 1: case 2: case 3:
554 return $this->msg( $tag, $val );
556 case 'GPSLatitudeRef':
559 return $this->msg( $tag, $val );
561 case 'GPSLongitudeRef':
564 return $this->msg( $tag, $val );
569 return $this->msg( $tag, $val );
571 case 'GPSMeasureMode':
574 return $this->msg( $tag, $val );
578 case 'K': case 'M': case 'N':
579 return $this->msg( $tag, $val );
584 return $this->msg( $tag, $val );
586 case 'GPSImgDirectionRef':
589 return $this->msg( $tag, $val );
591 case 'GPSDestLatitudeRef':
594 return $this->msg( $tag, $val );
596 case 'GPSDestLongitudeRef':
599 return $this->msg( $tag, $val );
601 case 'GPSDestBearingRef':
604 return $this->msg( $tag, $val );
607 return $wgLang->date( substr($val, 0, 4) . substr($val, 5, 2) . substr($val, 8, 2) . 000000 );
609 // This is not in the Exif standard, just a special
610 // case for our purposes which enables wikis to wikify
611 // the make and model to write articles about them.
612 case 'Make': case 'Model':
613 return wfMsg( strtolower( "exif-$tag-value" ), $val );
620 * Conviniance function for format()
622 * @param string $tag The tag name to pass on
623 * @param string $val The value of the tag
624 * @return string A wfMsg of "exif-$tag-$val" in lower case
626 function msg( $tag, $val ) {
627 return wfMsg( strtolower("exif-$tag-$val") );