MediaWiki  master
ExifBitmapHandler.php
Go to the documentation of this file.
1 <?php
2 
26 
36  public const BROKEN_FILE = '-1';
37 
39  public const OLD_BROKEN_FILE = '0';
40 
41  public function convertMetadataVersion( $metadata, $version = 1 ) {
42  // basically flattens arrays.
43  $version = intval( explode( ';', $version, 2 )[0] );
44  if ( $version < 1 || $version >= 2 ) {
45  return $metadata;
46  }
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  // @phan-suppress-next-line SecurityCheck-DoubleEscaped Ambiguous with the true for nohtml
76  $val = $formatter->flattenArrayReal( $val, 'ul', true );
77  }
78  }
79  $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
80 
81  return $metadata;
82  }
83 
88  public function isFileMetadataValid( $image ) {
89  $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( 'ShowEXIF' );
90  if ( !$showEXIF ) {
91  # Metadata disabled and so an empty field is expected
92  return self::METADATA_GOOD;
93  }
94  $exif = $image->getMetadataArray();
95  if ( !$exif ) {
96  wfDebug( __METHOD__ . ': error unserializing?' );
97  return self::METADATA_BAD;
98  }
99  if ( $exif === [ '_error' => self::OLD_BROKEN_FILE ] ) {
100  # Old special value indicating that there is no Exif data in the file.
101  # or that there was an error well extracting the metadata.
102  wfDebug( __METHOD__ . ": back-compat version" );
104  }
105 
106  if ( $exif === [ '_error' => self::BROKEN_FILE ] ) {
107  return self::METADATA_GOOD;
108  }
109 
110  if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
111  || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()
112  ) {
113  if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
114  && $exif['MEDIAWIKI_EXIF_VERSION'] == 1
115  ) {
116  // back-compatible but old
117  wfDebug( __METHOD__ . ": back-compat version" );
118 
120  }
121  # Wrong (non-compatible) version
122  wfDebug( __METHOD__ . ": wrong version" );
123 
124  return self::METADATA_BAD;
125  }
126 
127  return self::METADATA_GOOD;
128  }
129 
135  public function formatMetadata( $image, $context = false ) {
136  $meta = $this->getCommonMetaArray( $image );
137  if ( !$meta ) {
138  return false;
139  }
140 
141  return $this->formatMetadataHelper( $meta, $context );
142  }
143 
144  public function getCommonMetaArray( File $file ) {
145  $exif = $file->getMetadataArray();
146  if ( !$exif ) {
147  return [];
148  }
149  unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
150 
151  return $exif;
152  }
153 
154  public function getMetadataType( $image ) {
155  return 'exif';
156  }
157 
158  protected function applyExifRotation( $info, $metadata ) {
159  if ( $this->autoRotateEnabled() ) {
160  $rotation = $this->getRotationForExifFromOrientation( $metadata['Orientation'] ?? null );
161  } else {
162  $rotation = 0;
163  }
164 
165  if ( $rotation == 90 || $rotation == 270 ) {
166  $width = $info['width'];
167  $info['width'] = $info['height'];
168  $info['height'] = $width;
169  }
170  return $info;
171  }
172 
185  public function getRotation( $file ) {
186  if ( !$this->autoRotateEnabled() ) {
187  return 0;
188  }
189 
190  $orientation = $file->getMetadataItem( 'Orientation' );
191  return $this->getRotationForExifFromOrientation( $orientation );
192  }
193 
202  protected function getRotationForExifFromOrientation( $orientation ) {
203  if ( $orientation === null ) {
204  return 0;
205  }
206  # See http://sylvana.net/jpegcrop/exif_orientation.html
207  switch ( $orientation ) {
208  case 8:
209  return 90;
210  case 3:
211  return 180;
212  case 6:
213  return 270;
214  default:
215  return 0;
216  }
217  }
218 }
ExifBitmapHandler
Stuff specific to JPEG and (built-in) TIFF handler.
Definition: ExifBitmapHandler.php:34
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
FormatMetadata\collapseContactInfo
collapseContactInfo(array $vals)
Format the contact info field into a single value.
Definition: FormatMetadata.php:1598
$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:135
ExifBitmapHandler\getCommonMetaArray
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.
Definition: ExifBitmapHandler.php:144
ExifBitmapHandler\getRotationForExifFromOrientation
getRotationForExifFromOrientation( $orientation)
Given a chunk of serialized Exif metadata, return the orientation as degrees of rotation.
Definition: ExifBitmapHandler.php:202
MediaHandler\METADATA_COMPATIBLE
const METADATA_COMPATIBLE
Definition: MediaHandler.php:42
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:67
BitmapHandler
Generic handler for bitmap images.
Definition: BitmapHandler.php:33
Exif\version
static version()
#-
Definition: Exif.php:590
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:894
ExifBitmapHandler\getMetadataType
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
Definition: ExifBitmapHandler.php:154
BitmapHandler\autoRotateEnabled
autoRotateEnabled()
Definition: BitmapHandler.php:559
ExifBitmapHandler\convertMetadataVersion
convertMetadataVersion( $metadata, $version=1)
Convert metadata version.
Definition: ExifBitmapHandler.php:41
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:185
ExifBitmapHandler\OLD_BROKEN_FILE
const OLD_BROKEN_FILE
Outdated error extracting metadata.
Definition: ExifBitmapHandler.php:39
ExifBitmapHandler\applyExifRotation
applyExifRotation( $info, $metadata)
Definition: ExifBitmapHandler.php:158
MediaHandler\formatMetadataHelper
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
Definition: MediaHandler.php:694
ExifBitmapHandler\isFileMetadataValid
isFileMetadataValid( $image)
Definition: ExifBitmapHandler.php:88
MediaHandler\METADATA_BAD
const METADATA_BAD
Definition: MediaHandler.php:41
ExifBitmapHandler\BROKEN_FILE
const BROKEN_FILE
Error extracting metadata.
Definition: ExifBitmapHandler.php:36
MediaHandler\METADATA_GOOD
const METADATA_GOOD
Definition: MediaHandler.php:40