28 use Wikimedia\AtEase\AtEase;
120 # TIFF Rev. 6.0 Attribute Information (p22)
122 # Tags relating to image structure
125 'BitsPerSample' => [
self::SHORT, 3 ], # Number of bits per component
126 # "When a primary image is JPEG compressed, this designation is not"
127 # "necessary and is omitted." (p23)
128 'Compression' =>
self::SHORT, # Compression scheme #p23
129 'PhotometricInterpretation' =>
self::SHORT, # Pixel composition #p23
130 'Orientation' =>
self::SHORT, # Orientation of image #p24
131 'SamplesPerPixel' =>
self::SHORT, # Number of components
132 'PlanarConfiguration' =>
self::SHORT, # Image data arrangement #p24
133 'YCbCrSubSampling' => [
self::SHORT, 2 ], # Subsampling ratio of Y to C #p24
134 'YCbCrPositioning' =>
self::SHORT, # Y and C positioning #p24-25
135 'XResolution' =>
self::RATIONAL, # Image resolution in width direction
136 'YResolution' =>
self::RATIONAL, # Image resolution in height direction
137 'ResolutionUnit' =>
self::SHORT, # Unit of X and Y resolution #(p26)
139 # Tags relating to recording offset
140 'StripOffsets' => self::SHORT_OR_LONG, # Image data location
141 'RowsPerStrip' => self::SHORT_OR_LONG, # Number of rows per strip
142 'StripByteCounts' => self::SHORT_OR_LONG, # Bytes per compressed strip
143 'JPEGInterchangeFormat' => self::SHORT_OR_LONG, # Offset to JPEG SOI
144 'JPEGInterchangeFormatLength' => self::SHORT_OR_LONG, # Bytes of JPEG data
146 # Tags relating to image data characteristics
147 'TransferFunction' => self::IGNORE, # Transfer
function
148 'WhitePoint' => [ self::RATIONAL, 2 ], # White point chromaticity
149 'PrimaryChromaticities' => [ self::RATIONAL, 6 ], # Chromaticities of primarities
150 # Color space transformation matrix coefficients #p27
151 'YCbCrCoefficients' => [ self::RATIONAL, 3 ],
152 'ReferenceBlackWhite' => [ self::RATIONAL, 6 ], # Pair of black and white reference values
155 'DateTime' => self::ASCII, #
File change date and time
156 'ImageDescription' => self::ASCII, # Image title
157 'Make' => self::ASCII, # Image input equipment manufacturer
158 'Model' => self::ASCII, # Image input equipment model
159 'Software' => self::ASCII, # Software used
160 'Artist' => self::ASCII, # Person who created the image
161 'Copyright' => self::ASCII, # Copyright holder
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' =>
self::SHORT, # Color space information #p32
174 # Tags relating to image configuration
175 'ComponentsConfiguration' =>
self::UNDEFINED, # Meaning of each component #p33
176 'CompressedBitsPerPixel' =>
self::RATIONAL, # Image compression mode
180 # Tags relating to related user information
184 # Tags relating to related file information
185 'RelatedSoundFile' =>
self::ASCII, # Related audio file
187 # Tags relating to date and time
188 'DateTimeOriginal' =>
self::ASCII, # Date and time of original data generation #p36
189 'DateTimeDigitized' =>
self::ASCII, # Date and time of original data generation
191 'SubSecTimeOriginal' =>
self::ASCII, # DateTimeOriginal subseconds
192 'SubSecTimeDigitized' =>
self::ASCII, # DateTimeDigitized subseconds
194 # Tags relating to picture-taking conditions (p31)
197 'ExposureProgram' =>
self::SHORT, # Exposure Program #p38
198 'SpectralSensitivity' =>
self::ASCII, # Spectral sensitivity
199 'ISOSpeedRatings' =>
self::SHORT, # ISO speed rating
201 # Optoelectronic conversion factor. Note: We don't have support for this atm.
208 'MeteringMode' =>
self::SHORT, # Metering mode #p40
209 'LightSource' =>
self::SHORT, # Light source #p40-41
212 'SubjectArea' => [
self::SHORT, 4 ], # Subject area
214 'SpatialFrequencyResponse' =>
self::IGNORE, # Spatial frequency response. Not supported atm.
215 'FocalPlaneXResolution' =>
self::RATIONAL, # Focal plane X resolution
216 'FocalPlaneYResolution' =>
self::RATIONAL, # Focal plane Y resolution
217 'FocalPlaneResolutionUnit' =>
self::SHORT, # Focal plane resolution unit #p46
218 'SubjectLocation' => [
self::SHORT, 2 ], # Subject location
220 'SensingMethod' =>
self::SHORT, # Sensing method #p46
223 'CFAPattern' =>
self::IGNORE, # CFA pattern. not supported atm.
224 'CustomRendered' =>
self::SHORT, # Custom image processing #p48
225 'ExposureMode' =>
self::SHORT, # Exposure mode #p48
226 'WhiteBalance' =>
self::SHORT, # White Balance #p49
228 'FocalLengthIn35mmFilm' =>
self::SHORT, # Focal length in 35 mm film
229 'SceneCaptureType' =>
self::SHORT, # Scene capture type #p49
230 'GainControl' =>
self::SHORT, # Scene control #p49-50
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' =>
self::SHORT, # Subject distance range #p51
242 # GPS Attribute Information (p52)
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' =>
self::ASCII, # North or South Latitude #p52-53
249 'GPSLongitudeRef' =>
self::ASCII, # East or West Longitude #p53
252 # Altitude reference. Note, the exif standard says this should be an EXIF::Byte,
253 # but php seems to disagree.
255 'GPSTimeStamp' => [
self::RATIONAL, 3 ], # GPS time (atomic clock)
256 'GPSSatellites' =>
self::ASCII, # Satellites used
for measurement
258 'GPSMeasureMode' =>
self::ASCII, # Measurement mode #p54-55
262 'GPSTrackRef' =>
self::ASCII, # Reference
for direction of movement #p55
264 'GPSImgDirectionRef' =>
self::ASCII, # Reference
for direction of image #p56
266 'GPSMapDatum' =>
self::ASCII, # Geodetic survey data used
267 'GPSDestLatitudeRef' =>
self::ASCII, # Reference
for latitude of destination #p56
268 'GPSDestLatitude' => [
self::RATIONAL, 3 ], # Latitude destination
269 'GPSDestLongitudeRef' =>
self::ASCII, # Reference
for longitude of destination #p57
270 'GPSDestLongitude' => [
self::RATIONAL, 3 ], # Longitude of destination
271 'GPSDestBearingRef' =>
self::ASCII, # Reference
for bearing of destination #p57
273 'GPSDestDistanceRef' =>
self::ASCII, # Reference
for distance to destination #p57-58
275 'GPSProcessingMethod' =>
self::UNDEFINED, # Name of GPS processing method
278 'GPSDifferential' =>
self::SHORT, # GPS differential correction
290 wfWarn(
'Exif class did not have byte order specified. ' .
291 'Some properties may be decoded incorrectly.' );
292 $this->byteOrder =
'BE';
296 if ( function_exists(
'exif_read_data' ) ) {
297 AtEase::suppressWarnings();
298 $data = exif_read_data( $this->file,
'',
true );
299 AtEase::restoreWarnings();
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 ?: [];
319 $this->mFilteredExifData = [];
321 foreach ( $this->mRawExifData as $section => $data ) {
322 if ( !array_key_exists( $section, $this->mExifTags ) ) {
323 $this->
debug( $section, __FUNCTION__,
"'$section' is not a valid Exif section" );
327 foreach ( $data as $tag => $value ) {
328 if ( !array_key_exists( $tag, $this->mExifTags[$section] ) ) {
329 $this->
debug( $tag, __FUNCTION__,
"'$tag' is not a valid tag in '$section'" );
333 if ( $this->
validate( $section, $tag, $value ) ) {
336 $this->mFilteredExifData[$tag] = $value;
338 $this->
debug( $value, __FUNCTION__,
"'$tag' contained invalid data" );
368 if ( isset( $this->mFilteredExifData[
'GPSAltitude'] ) ) {
372 list( $num, $denom ) = explode(
'/', $this->mFilteredExifData[
'GPSAltitude'], 2 );
373 $this->mFilteredExifData[
'GPSAltitude'] = (int)$num / (
int)$denom;
375 if ( isset( $this->mFilteredExifData[
'GPSAltitudeRef'] ) ) {
376 switch ( $this->mFilteredExifData[
'GPSAltitudeRef'] ) {
382 $this->mFilteredExifData[
'GPSAltitude'] *= -1;
386 unset( $this->mFilteredExifData[
'GPSAltitude'] );
391 unset( $this->mFilteredExifData[
'GPSAltitudeRef'] );
403 if ( isset( $this->mFilteredExifData[
'ComponentsConfiguration'] ) ) {
404 $val = $this->mFilteredExifData[
'ComponentsConfiguration'];
407 $strLen = strlen( $val );
408 for ( $i = 0; $i < $strLen; $i++ ) {
409 $ccVals[$i] = ord( substr( $val, $i, 1 ) );
411 $ccVals[
'_type'] =
'ol';
412 $this->mFilteredExifData[
'ComponentsConfiguration'] = $ccVals;
422 if ( isset( $this->mFilteredExifData[
'GPSVersion'] ) ) {
423 $val = $this->mFilteredExifData[
'GPSVersion'];
426 $strLen = strlen( $val );
427 for ( $i = 0; $i < $strLen; $i++ ) {
431 $newVal .= ord( substr( $val, $i, 1 ) );
434 if ( $this->byteOrder ===
'LE' ) {
437 for ( $i = strlen( $newVal ) - 1; $i >= 0; $i-- ) {
438 $newVal2 .= substr( $newVal, $i, 1 );
440 $this->mFilteredExifData[
'GPSVersionID'] = $newVal2;
442 $this->mFilteredExifData[
'GPSVersionID'] = $newVal;
444 unset( $this->mFilteredExifData[
'GPSVersion'] );
455 if ( isset( $this->mFilteredExifData[$prop] ) ) {
456 if ( strlen( $this->mFilteredExifData[$prop] ) <= 8 ) {
459 $this->
debug( $this->mFilteredExifData[$prop], __FUNCTION__,
false );
460 unset( $this->mFilteredExifData[$prop] );
464 $charCode = substr( $this->mFilteredExifData[$prop], 0, 8 );
465 $val = substr( $this->mFilteredExifData[$prop], 8 );
467 switch ( $charCode ) {
468 case "JIS\x00\x00\x00\x00\x00":
469 $charset =
"Shift-JIS";
479 AtEase::suppressWarnings();
480 $val = iconv( $charset,
'UTF-8//IGNORE', $val );
481 AtEase::restoreWarnings();
485 UtfNormal\Validator::quickIsNFCVerify( $valCopy );
486 if ( $valCopy !== $val ) {
487 AtEase::suppressWarnings();
488 $val = iconv(
'Windows-1252',
'UTF-8//IGNORE', $val );
489 AtEase::restoreWarnings();
495 if ( strlen( $val ) === 0 ) {
497 $this->
debug( $this->mFilteredExifData[$prop], __FUNCTION__,
"$prop: Is only whitespace" );
498 unset( $this->mFilteredExifData[$prop] );
504 $this->mFilteredExifData[$prop] = $val;
515 if ( isset( $this->mFilteredExifData[$prop] ) ) {
516 $this->mFilteredExifData[$prop] = ord( $this->mFilteredExifData[$prop] );
526 $loc =& $this->mFilteredExifData[$prop];
527 $dir =& $this->mFilteredExifData[$prop .
'Ref'];
530 if ( isset( $loc ) && isset( $dir )
531 && ( $dir ===
'N' || $dir ===
'S' || $dir ===
'E' || $dir ===
'W' )
533 list( $num, $denom ) = explode(
'/', $loc[0], 2 );
534 $res = (int)$num / (
int)$denom;
535 list( $num, $denom ) = explode(
'/', $loc[1], 2 );
536 $res += ( (int)$num / (
int)$denom ) * ( 1 / 60 );
537 list( $num, $denom ) = explode(
'/', $loc[2], 2 );
538 $res += ( (int)$num / (
int)$denom ) * ( 1 / 3600 );
540 if ( $dir ===
'S' || $dir ===
'W' ) {
547 if (
$res !==
false ) {
548 $this->mFilteredExifData[$prop] =
$res;
549 unset( $this->mFilteredExifData[$prop .
'Ref'] );
551 unset( $this->mFilteredExifData[$prop] );
552 unset( $this->mFilteredExifData[$prop .
'Ref'] );
603 if ( !is_array( $in ) && sprintf(
'%d', $in ) == $in && $in >= 0 && $in <= 255 ) {
604 $this->
debug( $in, __FUNCTION__,
true );
608 $this->
debug( $in, __FUNCTION__,
false );
619 if ( is_array( $in ) ) {
623 if ( preg_match(
"/[^\x0a\x20-\x7e]/", $in ) ) {
624 $this->
debug( $in, __FUNCTION__,
'found a character that is not allowed' );
629 if ( preg_match(
'/^\s*$/', $in ) ) {
630 $this->
debug( $in, __FUNCTION__,
'input consisted solely of whitespace' );
643 if ( !is_array( $in ) && sprintf(
'%d', $in ) == $in && $in >= 0 && $in <= 65536 ) {
644 $this->
debug( $in, __FUNCTION__,
true );
648 $this->
debug( $in, __FUNCTION__,
false );
659 if ( !is_array( $in ) && sprintf(
'%d', $in ) == $in && $in >= 0 && $in <= 4294967296 ) {
660 $this->
debug( $in, __FUNCTION__,
true );
664 $this->
debug( $in, __FUNCTION__,
false );
677 # Avoid division by zero
678 if ( !is_array( $in )
679 && preg_match(
'/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m )
681 return $this->
isLong( $m[1] ) && $this->
isLong( $m[2] );
683 $this->
debug( $in, __FUNCTION__,
'fed a non-fraction value' );
694 $this->
debug( $in, __FUNCTION__,
true );
704 if ( $this->
isLong( abs( (
float)$in ) ) ) {
705 $this->
debug( $in, __FUNCTION__,
true );
709 $this->
debug( $in, __FUNCTION__,
false );
722 # Avoid division by zero
723 if ( !is_array( $in ) &&
724 preg_match(
'/^(-?\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m )
728 $this->
debug( $in, __FUNCTION__,
'fed a non-fraction value' );
745 private function validate( $section, $tag, $val, $recursive =
false ) {
747 $etype = $this->mExifTags[$section][$tag];
749 if ( is_array( $etype ) ) {
750 list( $etype, $ecount ) = $etype;
757 if ( is_array( $val ) ) {
758 $count = count( $val );
759 if ( $ecount != $count ) {
760 $this->
debug( $val, __FUNCTION__,
"Expected $ecount elements for $tag but got $count" );
766 foreach ( $val as $v ) {
767 if ( !$this->
validate( $section, $tag, $v,
true ) ) {
775 switch ( (
string)$etype ) {
779 return $this->
isByte( $val );
791 return $this->
isLong( $val );
817 $this->
debug( $val, __FUNCTION__,
"The tag '$tag' is unknown" );
830 private function debug( $in, $fname, $action =
null ) {
834 $type = gettype( $in );
835 $class = ucfirst( __CLASS__ );
836 if ( is_array( $in ) ) {
837 $in = print_r( $in,
true );
840 if ( $action ===
true ) {
841 wfDebugLog( $this->log,
"$class::$fname: accepted: '$in' (type: $type)" );
842 } elseif ( $action ===
false ) {
843 wfDebugLog( $this->log,
"$class::$fname: rejected: '$in' (type: $type)" );
844 } elseif ( $action ===
null ) {
845 wfDebugLog( $this->log,
"$class::$fname: input was: '$in' (type: $type)" );
847 wfDebugLog( $this->log,
"$class::$fname: $action (type: $type; content: '$in')" );
861 $class = ucfirst( __CLASS__ );
863 wfDebugLog( $this->log,
"$class::$fname: begin processing: '{$this->basename}'" );
865 wfDebugLog( $this->log,
"$class::$fname: end processing: '{$this->basename}'" );
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
wfBaseName( $path, $suffix='')
Return the final portion of a pathname.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Class to extract and validate Exif data from jpeg (and possibly tiff) files.
const SRATIONAL
Two SLONGs.
validate( $section, $tag, $val, $recursive=false)
#-
const BYTE
An 8-bit (1-byte) unsigned integer.
exifPropToOrd( $prop)
Convert an Exif::UNDEFINED from a raw binary string to its value.
string false $log
The private log to log to, e.g.
const SHORT_OR_LONG
A 16-bit (2-byte) or 32-bit (4-byte) unsigned integer.
string $file
The file being processed.
array $mFilteredExifData
A Filtered version of $mRawExifData that has been pruned of invalid tags and tags that contain conten...
const UNDEFINED
An 8-bit byte that can take any value depending on the field definition.
string $byteOrder
The byte order of the file.
const ASCII
An 8-bit byte containing one 7-bit ASCII code.
getFilteredData()
Get $this->mFilteredExifData.
debug( $in, $fname, $action=null)
Convenience function for debugging output.
const SHORT
A 16-bit (2-byte) unsigned integer.
charCodeString( $prop)
Do userComment tags and similar.
const LONG
A 32-bit (4-byte) unsigned integer.
array $mExifTags
Exif tags grouped by category, the tagname itself is the key and the type is the value,...
array $mRawExifData
The raw Exif data returned by exif_read_data()
__construct( $file, $byteOrder='')
makeFilteredData()
Make $this->mFilteredExifData.
string $basename
The basename of the file being processed.
collapseData()
Collapse some fields together.
const IGNORE
A fake value for things we don't want or don't support.
debugFile( $fname, $io)
Convenience function for debugging output.
exifGPStoNumber( $prop)
Convert gps in exif form to a single floating point number for example 10 degrees 2040` S -> -10....
isByte( $in)
Validates if a tag value is of the type it should be according to the Exif spec.
const SLONG
A 32-bit (4-byte) signed integer (2's complement notation),.
Implements some public methods and some protected utility functions which are required by multiple ch...
The User object encapsulates all of the user-specific settings (user_id, name, rights,...