119 $this->mExifTags =
array(
120 # TIFF Rev. 6.0 Attribute Information (p22)
122 # Tags relating to image structure
126 #
"When a primary image is JPEG compressed, this designation is not"
127 #
"necessary and is omitted." (p23)
128 'Compression' =>
Exif::SHORT, # Compression scheme #p23
129 'PhotometricInterpretation' =>
Exif::SHORT, # Pixel composition #p23
134 'YCbCrPositioning' =>
Exif::SHORT, # Y and C positioning #p24-25
137 'ResolutionUnit' =>
Exif::SHORT, # Unit
of X and Y resolution #(p26)
139 # Tags relating to recording offset
146 # Tags relating to image
data characteristics
150 # Color space transformation matrix coefficients #p27
157 'Make' =>
Exif::ASCII, # Image input equipment manufacturer
158 'Model' =>
Exif::ASCII, # Image input equipment model
160 'Artist' =>
Exif::ASCII, # Person who created the image
164 #
Exif IFD Attribute Information (p30-31)
166 # @todo NOTE: Nonexistence
of this field
is taken to mean nonconformance
167 # to the
Exif 2.1 AND 2.2 standards
171 # Tags relating to Image Data Characteristics
172 'ColorSpace' =>
Exif::SHORT, # Color space information #p32
174 # Tags relating to image configuration
176 'CompressedBitsPerPixel' =>
Exif::RATIONAL, # Image compression mode
180 # Tags relating to related
user information
184 # Tags relating to related
file information
187 # Tags relating to date and time
188 'DateTimeOriginal' =>
Exif::ASCII, # Date and time
of original
data generation #p36
191 'SubSecTimeOriginal' =>
Exif::ASCII, # DateTimeOriginal subseconds
192 'SubSecTimeDigitized' =>
Exif::ASCII, # DateTimeDigitized subseconds
194 # Tags relating to picture-taking conditions (p31)
197 'ExposureProgram' =>
Exif::SHORT, # Exposure Program #p38
198 'SpectralSensitivity' =>
Exif::ASCII, # Spectral sensitivity
199 'ISOSpeedRatings' =>
Exif::SHORT, # ISO speed rating
201 # Optoelectronic
conversion factor. Note: We don
't have support for this atm.
202 'ShutterSpeedValue
' => Exif::SRATIONAL, # Shutter speed
203 'ApertureValue
' => Exif::RATIONAL, # Aperture
204 'BrightnessValue
' => Exif::SRATIONAL, # Brightness
205 'ExposureBiasValue
' => Exif::SRATIONAL, # Exposure bias
206 'MaxApertureValue
' => Exif::RATIONAL, # Maximum land aperture
207 'SubjectDistance
' => Exif::RATIONAL, # Subject distance
208 'MeteringMode
' => Exif::SHORT, # Metering mode #p40
209 'LightSource
' => Exif::SHORT, # Light source #p40-41
210 'Flash
' => Exif::SHORT, # Flash #p41-42
211 'FocalLength
' => Exif::RATIONAL, # Lens focal length
212 'SubjectArea
' => array( Exif::SHORT, 4 ), # Subject area
213 'FlashEnergy
' => Exif::RATIONAL, # Flash energy
214 'SpatialFrequencyResponse
' => Exif::IGNORE, # Spatial frequency response. Not supported atm.
215 'FocalPlaneXResolution
' => Exif::RATIONAL, # Focal plane X resolution
216 'FocalPlaneYResolution
' => Exif::RATIONAL, # Focal plane Y resolution
217 'FocalPlaneResolutionUnit
' => Exif::SHORT, # Focal plane resolution unit #p46
218 'SubjectLocation
' => array( Exif::SHORT, 2 ), # Subject location
219 'ExposureIndex
' => Exif::RATIONAL, # Exposure index
220 'SensingMethod
' => Exif::SHORT, # Sensing method #p46
221 'FileSource
' => Exif::UNDEFINED, # File source #p47
222 'SceneType
' => Exif::UNDEFINED, # Scene type #p47
223 'CFAPattern
' => Exif::IGNORE, # CFA pattern. not supported atm.
224 'CustomRendered
' => Exif::SHORT, # Custom image processing #p48
225 'ExposureMode
' => Exif::SHORT, # Exposure mode #p48
226 'WhiteBalance
' => Exif::SHORT, # White Balance #p49
227 'DigitalZoomRatio
' => Exif::RATIONAL, # Digital zoom ration
228 'FocalLengthIn35mmFilm
' => Exif::SHORT, # Focal length in 35 mm film
229 'SceneCaptureType
' => Exif::SHORT, # Scene capture type #p49
230 'GainControl
' => Exif::SHORT, # Scene control #p49-50
231 'Contrast
' => Exif::SHORT, # Contrast #p50
232 'Saturation
' => Exif::SHORT, # Saturation #p50
233 'Sharpness
' => Exif::SHORT, # Sharpness #p50
234 'DeviceSettingDescription
' => Exif::IGNORE,
235 # Device settings description. This could maybe be supported. Need to find an
236 # example file that uses this to see if it has stuff of interest in it.
237 'SubjectDistanceRange
' => Exif::SHORT, # Subject distance range #p51
239 'ImageUniqueID
' => Exif::ASCII, # Unique image ID
242 # GPS Attribute Information (p52)
244 'GPSVersion
' => Exif::UNDEFINED,
245 # Should be an array of 4 Exif::BYTE's. However
php treats
it as an undefined
246 # Note exif standard calls
this GPSVersionID, but
php doesn
't like the id suffix
247 'GPSLatitudeRef
' => Exif::ASCII, # North or South Latitude #p52-53
248 'GPSLatitude
' => array( Exif::RATIONAL, 3 ), # Latitude
249 'GPSLongitudeRef
' => Exif::ASCII, # East or West Longitude #p53
250 'GPSLongitude
' => array( Exif::RATIONAL, 3 ), # Longitude
251 'GPSAltitudeRef
' => Exif::UNDEFINED,
252 # Altitude reference. Note, the exif standard says this should be an EXIF::Byte,
253 # but php seems to disagree.
254 'GPSAltitude
' => Exif::RATIONAL, # Altitude
255 'GPSTimeStamp
' => array( Exif::RATIONAL, 3 ), # GPS time (atomic clock)
256 'GPSSatellites
' => Exif::ASCII, # Satellites used for measurement
257 'GPSStatus
' => Exif::ASCII, # Receiver status #p54
258 'GPSMeasureMode
' => Exif::ASCII, # Measurement mode #p54-55
259 'GPSDOP
' => Exif::RATIONAL, # Measurement precision
260 'GPSSpeedRef
' => Exif::ASCII, # Speed unit #p55
261 'GPSSpeed
' => Exif::RATIONAL, # Speed of GPS receiver
262 'GPSTrackRef
' => Exif::ASCII, # Reference for direction of movement #p55
263 'GPSTrack
' => Exif::RATIONAL, # Direction of movement
264 'GPSImgDirectionRef
' => Exif::ASCII, # Reference for direction of image #p56
265 'GPSImgDirection
' => Exif::RATIONAL, # Direction of image
266 'GPSMapDatum
' => Exif::ASCII, # Geodetic survey data used
267 'GPSDestLatitudeRef
' => Exif::ASCII, # Reference for latitude of destination #p56
268 'GPSDestLatitude
' => array( Exif::RATIONAL, 3 ), # Latitude destination
269 'GPSDestLongitudeRef
' => Exif::ASCII, # Reference for longitude of destination #p57
270 'GPSDestLongitude
' => array( Exif::RATIONAL, 3 ), # Longitude of destination
271 'GPSDestBearingRef
' => Exif::ASCII, # Reference for bearing of destination #p57
272 'GPSDestBearing
' => Exif::RATIONAL, # Bearing of destination
273 'GPSDestDistanceRef
' => Exif::ASCII, # Reference for distance to destination #p57-58
274 'GPSDestDistance
' => Exif::RATIONAL, # Distance to destination
275 'GPSProcessingMethod
' => Exif::UNDEFINED, # Name of GPS processing method
276 'GPSAreaInformation
' => Exif::UNDEFINED, # Name of GPS area
277 'GPSDateStamp
' => Exif::ASCII, # GPS date
278 'GPSDifferential
' => Exif::SHORT, # GPS differential correction
283 $this->basename = wfBaseName( $this->file );
284 if ( $byteOrder === 'BE
' || $byteOrder === 'LE
' ) {
285 $this->byteOrder = $byteOrder;
287 // Only give a warning for b/c, since originally we didn't
290 wfWarn(
'Exif class did not have byte order specified. ' .
291 'Some properties may be decoded incorrectly.' );
292 $this->byteOrder =
'BE';
295 $this->
debugFile( $this->basename, __FUNCTION__,
true );
296 if ( function_exists(
'exif_read_data' ) ) {
298 $data = exif_read_data( $this->
file, 0,
true );
301 throw new MWException(
"Internal error: exif_read_data not present. " .
302 "\$wgShowEXIF may be incorrectly set or not checked by an extension." );
309 $this->mRawExifData = $data ?:
array();
318 function makeFilteredData() {
319 $this->mFilteredExifData =
array();
321 foreach ( array_keys( $this->mRawExifData )
as $section ) {
322 if ( !array_key_exists(
$section, $this->mExifTags ) ) {
323 $this->debug(
$section, __FUNCTION__,
"'$section' is not a valid Exif section" );
327 foreach ( array_keys( $this->mRawExifData[
$section] )
as $tag ) {
328 if ( !array_key_exists( $tag, $this->mExifTags[
$section] ) ) {
329 $this->debug( $tag, __FUNCTION__,
"'$tag' is not a valid tag in '$section'" );
333 $this->mFilteredExifData[$tag] = $this->mRawExifData[
$section][$tag];
339 $this->debug(
$value, __FUNCTION__,
"'$tag' contained invalid data" );
340 unset( $this->mFilteredExifData[$tag] );
364 function collapseData() {
366 $this->exifGPStoNumber(
'GPSLatitude' );
367 $this->exifGPStoNumber(
'GPSDestLatitude' );
368 $this->exifGPStoNumber(
'GPSLongitude' );
369 $this->exifGPStoNumber(
'GPSDestLongitude' );
371 if ( isset( $this->mFilteredExifData[
'GPSAltitude'] )
372 && isset( $this->mFilteredExifData[
'GPSAltitudeRef'] )
377 list( $num, $denom ) = explode(
'/', $this->mFilteredExifData[
'GPSAltitude'] );
378 $this->mFilteredExifData[
'GPSAltitude'] = $num / $denom;
380 if ( $this->mFilteredExifData[
'GPSAltitudeRef'] ===
"\1" ) {
381 $this->mFilteredExifData[
'GPSAltitude'] *= -1;
383 unset( $this->mFilteredExifData[
'GPSAltitudeRef'] );
386 $this->exifPropToOrd(
'FileSource' );
387 $this->exifPropToOrd(
'SceneType' );
389 $this->charCodeString(
'UserComment' );
390 $this->charCodeString(
'GPSProcessingMethod' );
391 $this->charCodeString(
'GPSAreaInformation' );
396 if ( isset( $this->mFilteredExifData[
'ComponentsConfiguration'] ) ) {
397 $val = $this->mFilteredExifData[
'ComponentsConfiguration'];
400 $strLen = strlen( $val );
401 for ( $i = 0; $i < $strLen; $i++ ) {
402 $ccVals[$i] = ord( substr( $val, $i, 1 ) );
404 $ccVals[
'_type'] =
'ol';
405 $this->mFilteredExifData[
'ComponentsConfiguration'] = $ccVals;
415 if ( isset( $this->mFilteredExifData[
'GPSVersion'] ) ) {
416 $val = $this->mFilteredExifData[
'GPSVersion'];
419 $strLen = strlen( $val );
420 for ( $i = 0; $i < $strLen; $i++ ) {
424 $newVal .= ord( substr( $val, $i, 1 ) );
427 if ( $this->byteOrder ===
'LE' ) {
430 for ( $i = strlen( $newVal ) - 1; $i >= 0; $i-- ) {
431 $newVal2 .= substr( $newVal, $i, 1 );
433 $this->mFilteredExifData[
'GPSVersionID'] = $newVal2;
435 $this->mFilteredExifData[
'GPSVersionID'] = $newVal;
437 unset( $this->mFilteredExifData[
'GPSVersion'] );
447 private function charCodeString( $prop ) {
448 if ( isset( $this->mFilteredExifData[$prop] ) ) {
450 if ( strlen( $this->mFilteredExifData[$prop] ) <= 8 ) {
453 $this->debug( $this->mFilteredExifData[$prop], __FUNCTION__,
false );
454 unset( $this->mFilteredExifData[$prop] );
458 $charCode = substr( $this->mFilteredExifData[$prop], 0, 8 );
459 $val = substr( $this->mFilteredExifData[$prop], 8 );
461 switch ( $charCode ) {
462 case "\x4A\x49\x53\x00\x00\x00\x00\x00":
464 $charset =
"Shift-JIS";
467 $charset =
"UTF-16" . $this->byteOrder;
477 $val = iconv( $charset,
'UTF-8//IGNORE', $val );
483 if ( $valCopy !== $val ) {
485 $val = iconv(
'Windows-1252',
'UTF-8//IGNORE', $val );
492 if ( strlen( $val ) === 0 ) {
494 $this->debug( $this->mFilteredExifData[$prop], __FUNCTION__,
"$prop: Is only whitespace" );
495 unset( $this->mFilteredExifData[$prop] );
501 $this->mFilteredExifData[$prop] = $val;
511 private function exifPropToOrd( $prop ) {
512 if ( isset( $this->mFilteredExifData[$prop] ) ) {
513 $this->mFilteredExifData[$prop] = ord( $this->mFilteredExifData[$prop] );
522 private function exifGPStoNumber( $prop ) {
523 $loc =& $this->mFilteredExifData[$prop];
524 $dir =& $this->mFilteredExifData[$prop .
'Ref'];
527 if ( isset( $loc ) && isset(
$dir )
530 list( $num, $denom ) = explode(
'/', $loc[0] );
531 $res = $num / $denom;
532 list( $num, $denom ) = explode(
'/', $loc[1] );
533 $res += ( $num / $denom ) * ( 1 / 60 );
534 list( $num, $denom ) = explode(
'/', $loc[2] );
535 $res += ( $num / $denom ) * ( 1 / 3600 );
537 if (
$dir ===
'S' ||
$dir ===
'W' ) {
544 if (
$res !==
false ) {
545 $this->mFilteredExifData[$prop] =
$res;
546 unset( $this->mFilteredExifData[$prop .
'Ref'] );
548 unset( $this->mFilteredExifData[$prop] );
549 unset( $this->mFilteredExifData[$prop .
'Ref'] );
563 return $this->mRawExifData;
569 function getFilteredData() {
570 return $this->mFilteredExifData;
587 public static function version() {
597 private function isByte(
$in ) {
598 if ( !is_array(
$in ) && sprintf(
'%d',
$in ) ==
$in &&
$in >= 0 &&
$in <= 255 ) {
599 $this->debug(
$in, __FUNCTION__,
true );
603 $this->debug(
$in, __FUNCTION__,
false );
613 private function isASCII(
$in ) {
614 if ( is_array(
$in ) ) {
618 if ( preg_match(
"/[^\x0a\x20-\x7e]/",
$in ) ) {
619 $this->debug(
$in, __FUNCTION__,
'found a character not in our whitelist' );
624 if ( preg_match(
'/^\s*$/',
$in ) ) {
625 $this->debug(
$in, __FUNCTION__,
'input consisted solely of whitespace' );
637 private function isShort(
$in ) {
638 if ( !is_array(
$in ) && sprintf(
'%d',
$in ) ==
$in &&
$in >= 0 &&
$in <= 65536 ) {
639 $this->debug(
$in, __FUNCTION__,
true );
643 $this->debug(
$in, __FUNCTION__,
false );
653 private function isLong(
$in ) {
654 if ( !is_array(
$in ) && sprintf(
'%d',
$in ) ==
$in &&
$in >= 0 &&
$in <= 4294967296 ) {
655 $this->debug(
$in, __FUNCTION__,
true );
659 $this->debug(
$in, __FUNCTION__,
false );
669 private function isRational(
$in ) {
672 # Avoid division by zero
673 if ( !is_array(
$in )
674 && preg_match(
'/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/',
$in, $m )
676 return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
678 $this->debug(
$in, __FUNCTION__,
'fed a non-fraction value' );
688 private function isUndefined(
$in ) {
689 $this->debug(
$in, __FUNCTION__,
true );
698 private function isSlong(
$in ) {
699 if ( $this->isLong( abs(
$in ) ) ) {
700 $this->debug(
$in, __FUNCTION__,
true );
704 $this->debug(
$in, __FUNCTION__,
false );
714 private function isSrational(
$in ) {
717 # Avoid division by zero
718 if ( !is_array(
$in ) &&
719 preg_match(
'/^(-?\d+)\/(\d+[1-9]|[1-9]\d*)$/',
$in, $m )
721 return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
723 $this->debug(
$in, __FUNCTION__,
'fed a non-fraction value' );
740 private function validate(
$section, $tag, $val, $recursive =
false ) {
742 $etype = $this->mExifTags[
$section][$tag];
744 if ( is_array( $etype ) ) {
745 list( $etype, $ecount ) = $etype;
751 if ( $ecount !=
$count ) {
752 $this->debug( $val, __FUNCTION__,
"Expected $ecount elements for $tag but got $count" );
757 foreach ( $val
as $v ) {
758 if ( !$this->validate(
$section, $tag, $v,
true ) ) {
766 switch ( (
string)$etype ) {
768 $this->debug( $val, __FUNCTION__,
$debug );
770 return $this->isByte( $val );
772 $this->debug( $val, __FUNCTION__,
$debug );
774 return $this->isASCII( $val );
776 $this->debug( $val, __FUNCTION__,
$debug );
778 return $this->isShort( $val );
780 $this->debug( $val, __FUNCTION__,
$debug );
782 return $this->isLong( $val );
784 $this->debug( $val, __FUNCTION__,
$debug );
786 return $this->isRational( $val );
788 $this->debug( $val, __FUNCTION__,
$debug );
790 return $this->isShort( $val ) || $this->isLong( $val );
792 $this->debug( $val, __FUNCTION__,
$debug );
794 return $this->isUndefined( $val );
796 $this->debug( $val, __FUNCTION__,
$debug );
798 return $this->isSlong( $val );
800 $this->debug( $val, __FUNCTION__,
$debug );
802 return $this->isSrational( $val );
804 $this->debug( $val, __FUNCTION__,
$debug );
808 $this->debug( $val, __FUNCTION__,
"The tag '$tag' is unknown" );
821 private function debug(
$in,
$fname, $action =
null ) {
826 $class = ucfirst( __CLASS__ );
827 if ( is_array(
$in ) ) {
828 $in = print_r(
$in,
true );
831 if ( $action ===
true ) {
832 wfDebugLog( $this->log,
"$class::$fname: accepted: '$in' (type: $type)" );
833 } elseif ( $action ===
false ) {
834 wfDebugLog( $this->log,
"$class::$fname: rejected: '$in' (type: $type)" );
835 } elseif ( $action ===
null ) {
836 wfDebugLog( $this->log,
"$class::$fname: input was: '$in' (type: $type)" );
838 wfDebugLog( $this->log,
"$class::$fname: $action (type: $type; content: '$in')" );
848 private function debugFile(
$fname, $io ) {
852 $class = ucfirst( __CLASS__ );
854 wfDebugLog( $this->log,
"$class::$fname: begin processing: '{$this->basename}'" );
856 wfDebugLog( $this->log,
"$class::$fname: end processing: '{$this->basename}'" );