MediaWiki  master
ExifBitmapHandler.php
Go to the documentation of this file.
1 <?php
33  public const BROKEN_FILE = '-1';
34 
36  public const OLD_BROKEN_FILE = '0';
37 
38  public function convertMetadataVersion( $metadata, $version = 1 ) {
39  // basically flattens arrays.
40  $version = intval( explode( ';', $version, 2 )[0] );
41  if ( $version < 1 || $version >= 2 ) {
42  return $metadata;
43  }
44 
45  if ( !is_array( $metadata ) ) {
46  $metadata = unserialize( $metadata );
47  }
48  if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
49  return $metadata;
50  }
51 
52  // Treat Software as a special case because in can contain
53  // an array of (SoftwareName, Version).
54  if ( isset( $metadata['Software'] )
55  && is_array( $metadata['Software'] )
56  && is_array( $metadata['Software'][0] )
57  && isset( $metadata['Software'][0][0] )
58  && isset( $metadata['Software'][0][1] )
59  ) {
60  $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
61  . $metadata['Software'][0][1] . ')';
62  }
63 
64  $formatter = new FormatMetadata;
65 
66  // ContactInfo also has to be dealt with specially
67  if ( isset( $metadata['Contact'] ) ) {
68  $metadata['Contact'] = $formatter->collapseContactInfo(
69  is_array( $metadata['Contact'] ) ? $metadata['Contact'] : [ $metadata['Contact'] ]
70  );
71  }
72 
73  foreach ( $metadata as &$val ) {
74  if ( is_array( $val ) ) {
75  $val = $formatter->flattenArrayReal( $val, 'ul', true );
76  }
77  }
78  $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
79 
80  return $metadata;
81  }
82 
88  public function isMetadataValid( $image, $metadata ) {
89  global $wgShowEXIF;
90  if ( !$wgShowEXIF ) {
91  # Metadata disabled and so an empty field is expected
92  return self::METADATA_GOOD;
93  }
94  if ( $metadata === self::OLD_BROKEN_FILE ) {
95  # Old special value indicating that there is no Exif data in the file.
96  # or that there was an error well extracting the metadata.
97  wfDebug( __METHOD__ . ": back-compat version" );
98 
100  }
101  if ( $metadata === self::BROKEN_FILE ) {
102  return self::METADATA_GOOD;
103  }
104  Wikimedia\suppressWarnings();
105  $exif = unserialize( $metadata );
106  Wikimedia\restoreWarnings();
107  if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
108  || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()
109  ) {
110  if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
111  && $exif['MEDIAWIKI_EXIF_VERSION'] == 1
112  ) {
113  // back-compatible but old
114  wfDebug( __METHOD__ . ": back-compat version" );
115 
117  }
118  # Wrong (non-compatible) version
119  wfDebug( __METHOD__ . ": wrong version" );
120 
121  return self::METADATA_BAD;
122  }
123 
124  return self::METADATA_GOOD;
125  }
126 
132  public function formatMetadata( $image, $context = false ) {
133  $meta = $this->getCommonMetaArray( $image );
134  if ( !$meta ) {
135  return false;
136  }
137 
138  return $this->formatMetadataHelper( $meta, $context );
139  }
140 
141  public function getCommonMetaArray( File $file ) {
142  $metadata = $file->getMetadata();
143  if ( $metadata === self::OLD_BROKEN_FILE
144  || $metadata === self::BROKEN_FILE
145  || $this->isMetadataValid( $file, $metadata ) === self::METADATA_BAD
146  ) {
147  // So we don't try and display metadata from PagedTiffHandler
148  // for example when using InstantCommons.
149  return [];
150  }
151 
152  $exif = unserialize( $metadata );
153  if ( !$exif ) {
154  return [];
155  }
156  unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
157 
158  return $exif;
159  }
160 
161  public function getMetadataType( $image ) {
162  return 'exif';
163  }
164 
173  public function getImageSize( $image, $path ) {
174  $gis = parent::getImageSize( $image, $path );
175 
176  // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
177  // This may mean we read EXIF data twice on initial upload.
178  if ( $this->autoRotateEnabled() ) {
179  $meta = $this->getMetadata( $image, $path );
180  $rotation = $this->getRotationForExif( $meta );
181  } else {
182  $rotation = 0;
183  }
184 
185  if ( $rotation == 90 || $rotation == 270 ) {
186  $width = $gis[0];
187  $gis[0] = $gis[1];
188  $gis[1] = $width;
189  }
190 
191  return $gis;
192  }
193 
206  public function getRotation( $file ) {
207  if ( !$this->autoRotateEnabled() ) {
208  return 0;
209  }
210 
211  $data = $file->getMetadata();
212 
213  return $this->getRotationForExif( $data );
214  }
215 
224  protected function getRotationForExif( $data ) {
225  if ( !$data ) {
226  return 0;
227  }
228  Wikimedia\suppressWarnings();
229  $data = unserialize( $data );
230  Wikimedia\restoreWarnings();
231  if ( isset( $data['Orientation'] ) ) {
232  # See http://sylvana.net/jpegcrop/exif_orientation.html
233  switch ( $data['Orientation'] ) {
234  case 8:
235  return 90;
236  case 3:
237  return 180;
238  case 6:
239  return 270;
240  default:
241  return 0;
242  }
243  }
244 
245  return 0;
246  }
247 }
ExifBitmapHandler
Stuff specific to JPEG and (built-in) TIFF handler.
Definition: ExifBitmapHandler.php:31
ExifBitmapHandler\getImageSize
getImageSize( $image, $path)
Wrapper for base classes ImageHandler::getImageSize() that checks for rotation reported from metadata...
Definition: ExifBitmapHandler.php:173
FormatMetadata\collapseContactInfo
collapseContactInfo(array $vals)
Format the contact info field into a single value.
Definition: FormatMetadata.php:1596
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
ExifBitmapHandler\formatMetadata
formatMetadata( $image, $context=false)
Definition: ExifBitmapHandler.php:132
ExifBitmapHandler\getCommonMetaArray
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.
Definition: ExifBitmapHandler.php:141
MediaHandler\METADATA_COMPATIBLE
const METADATA_COMPATIBLE
Definition: MediaHandler.php:41
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:63
BitmapHandler
Generic handler for bitmap images.
Definition: BitmapHandler.php:32
Exif\version
static version()
#-
Definition: Exif.php:592
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:910
ExifBitmapHandler\getMetadataType
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
Definition: ExifBitmapHandler.php:161
MediaHandler\getMetadata
getMetadata( $image, $path)
Get handler-specific metadata which will be saved in the img_metadata field.
Definition: MediaHandler.php:130
BitmapHandler\autoRotateEnabled
autoRotateEnabled()
Definition: BitmapHandler.php:549
ExifBitmapHandler\convertMetadataVersion
convertMetadataVersion( $metadata, $version=1)
Convert metadata version.
Definition: ExifBitmapHandler.php:38
FormatMetadata
Format Image metadata values into a human readable form.
Definition: FormatMetadata.php:54
ExifBitmapHandler\getRotation
getRotation( $file)
On supporting image formats, try to read out the low-level orientation of the file and return the ang...
Definition: ExifBitmapHandler.php:206
ExifBitmapHandler\OLD_BROKEN_FILE
const OLD_BROKEN_FILE
Outdated error extracting metadata.
Definition: ExifBitmapHandler.php:36
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
MediaHandler\formatMetadataHelper
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
Definition: MediaHandler.php:543
$path
$path
Definition: NoLocalSettings.php:25
MediaHandler\METADATA_BAD
const METADATA_BAD
Definition: MediaHandler.php:40
ExifBitmapHandler\isMetadataValid
isMetadataValid( $image, $metadata)
Definition: ExifBitmapHandler.php:88
ExifBitmapHandler\BROKEN_FILE
const BROKEN_FILE
Error extracting metadata.
Definition: ExifBitmapHandler.php:33
ExifBitmapHandler\getRotationForExif
getRotationForExif( $data)
Given a chunk of serialized Exif metadata, return the orientation as degrees of rotation.
Definition: ExifBitmapHandler.php:224
$wgShowEXIF
$wgShowEXIF
Show Exif data, on by default if available.
Definition: DefaultSettings.php:883
MediaHandler\METADATA_GOOD
const METADATA_GOOD
Definition: MediaHandler.php:39