MediaWiki  1.29.1
ExifBitmap.php
Go to the documentation of this file.
1 <?php
31  const BROKEN_FILE = '-1'; // error extracting metadata
32  const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
33  const SRGB_ICC_PROFILE_NAME = 'IEC 61966-2.1 Default RGB colour space - sRGB';
34 
35  function convertMetadataVersion( $metadata, $version = 1 ) {
36  // basically flattens arrays.
37  $version = intval( explode( ';', $version, 2 )[0] );
38  if ( $version < 1 || $version >= 2 ) {
39  return $metadata;
40  }
41 
42  $avoidHtml = true;
43 
44  if ( !is_array( $metadata ) ) {
45  $metadata = unserialize( $metadata );
46  }
47  if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
48  return $metadata;
49  }
50 
51  // Treat Software as a special case because in can contain
52  // an array of (SoftwareName, Version).
53  if ( isset( $metadata['Software'] )
54  && is_array( $metadata['Software'] )
55  && is_array( $metadata['Software'][0] )
56  && isset( $metadata['Software'][0][0] )
57  && isset( $metadata['Software'][0][1] )
58  ) {
59  $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
60  . $metadata['Software'][0][1] . ')';
61  }
62 
63  $formatter = new FormatMetadata;
64 
65  // ContactInfo also has to be dealt with specially
66  if ( isset( $metadata['Contact'] ) ) {
67  $metadata['Contact'] =
68  $formatter->collapseContactInfo(
69  $metadata['Contact'] );
70  }
71 
72  foreach ( $metadata as &$val ) {
73  if ( is_array( $val ) ) {
74  $val = $formatter->flattenArrayReal( $val, 'ul', $avoidHtml );
75  }
76  }
77  $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
78 
79  return $metadata;
80  }
81 
87  function isMetadataValid( $image, $metadata ) {
89  if ( !$wgShowEXIF ) {
90  # Metadata disabled and so an empty field is expected
91  return self::METADATA_GOOD;
92  }
93  if ( $metadata === self::OLD_BROKEN_FILE ) {
94  # Old special value indicating that there is no Exif data in the file.
95  # or that there was an error well extracting the metadata.
96  wfDebug( __METHOD__ . ": back-compat version\n" );
97 
99  }
100  if ( $metadata === self::BROKEN_FILE ) {
101  return self::METADATA_GOOD;
102  }
103  MediaWiki\suppressWarnings();
104  $exif = unserialize( $metadata );
105  MediaWiki\restoreWarnings();
106  if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
107  || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()
108  ) {
109  if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
110  && $exif['MEDIAWIKI_EXIF_VERSION'] == 1
111  ) {
112  // back-compatible but old
113  wfDebug( __METHOD__ . ": back-compat version\n" );
114 
116  }
117  # Wrong (non-compatible) version
118  wfDebug( __METHOD__ . ": wrong version\n" );
119 
120  return self::METADATA_BAD;
121  }
122 
123  return self::METADATA_GOOD;
124  }
125 
131  function formatMetadata( $image, $context = false ) {
132  $meta = $this->getCommonMetaArray( $image );
133  if ( count( $meta ) === 0 ) {
134  return false;
135  }
136 
137  return $this->formatMetadataHelper( $meta, $context );
138  }
139 
140  public function getCommonMetaArray( File $file ) {
141  $metadata = $file->getMetadata();
142  if ( $metadata === self::OLD_BROKEN_FILE
143  || $metadata === self::BROKEN_FILE
144  || $this->isMetadataValid( $file, $metadata ) === self::METADATA_BAD
145  ) {
146  // So we don't try and display metadata from PagedTiffHandler
147  // for example when using InstantCommons.
148  return [];
149  }
150 
151  $exif = unserialize( $metadata );
152  if ( !$exif ) {
153  return [];
154  }
155  unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
156 
157  return $exif;
158  }
159 
160  function getMetadataType( $image ) {
161  return 'exif';
162  }
163 
172  function getImageSize( $image, $path ) {
173  $gis = parent::getImageSize( $image, $path );
174 
175  // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
176  // This may mean we read EXIF data twice on initial upload.
177  if ( $this->autoRotateEnabled() ) {
178  $meta = $this->getMetadata( $image, $path );
179  $rotation = $this->getRotationForExif( $meta );
180  } else {
181  $rotation = 0;
182  }
183 
184  if ( $rotation == 90 || $rotation == 270 ) {
185  $width = $gis[0];
186  $gis[0] = $gis[1];
187  $gis[1] = $width;
188  }
189 
190  return $gis;
191  }
192 
205  public function getRotation( $file ) {
206  if ( !$this->autoRotateEnabled() ) {
207  return 0;
208  }
209 
210  $data = $file->getMetadata();
211 
212  return $this->getRotationForExif( $data );
213  }
214 
223  protected function getRotationForExif( $data ) {
224  if ( !$data ) {
225  return 0;
226  }
227  MediaWiki\suppressWarnings();
228  $data = unserialize( $data );
229  MediaWiki\restoreWarnings();
230  if ( isset( $data['Orientation'] ) ) {
231  # See http://sylvana.net/jpegcrop/exif_orientation.html
232  switch ( $data['Orientation'] ) {
233  case 8:
234  return 90;
235  case 3:
236  return 180;
237  case 6:
238  return 270;
239  default:
240  return 0;
241  }
242  }
243 
244  return 0;
245  }
246 
247  protected function transformImageMagick( $image, $params ) {
249 
250  $ret = parent::transformImageMagick( $image, $params );
251 
252  if ( $ret ) {
253  return $ret;
254  }
255 
256  if ( $params['mimeType'] === 'image/jpeg' && $wgUseTinyRGBForJPGThumbnails ) {
257  // T100976 If the profile embedded in the JPG is sRGB, swap it for the smaller
258  // (and free) TinyRGB
259 
260  $this->swapICCProfile(
261  $params['dstPath'],
262  self::SRGB_ICC_PROFILE_NAME,
263  realpath( __DIR__ ) . '/tinyrgb.icc'
264  );
265  }
266 
267  return false;
268  }
269 
280  public function swapICCProfile( $filepath, $oldProfileString, $profileFilepath ) {
282 
283  if ( !$wgExiftool || !is_executable( $wgExiftool ) ) {
284  return false;
285  }
286 
288  '-DeviceModelDesc',
289  '-S',
290  '-T',
291  $filepath
292  );
293 
295 
296  if ( $retval !== 0 || strcasecmp( trim( $output ), $oldProfileString ) !== 0 ) {
297  // We can't establish that this file has the expected ICC profile, don't process it
298  return false;
299  }
300 
302  '-overwrite_original',
303  '-icc_profile<=' . $profileFilepath,
304  $filepath
305  );
306 
308 
309  if ( $retval !== 0 ) {
310  $this->logErrorForExternalProcess( $retval, $output, $cmd );
311 
312  return false;
313  }
314 
315  return true;
316  }
317 }
ExifBitmapHandler
Stuff specific to JPEG and (built-in) TIFF handler.
Definition: ExifBitmap.php:30
$context
error also a ContextSource you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2612
ExifBitmapHandler\SRGB_ICC_PROFILE_NAME
const SRGB_ICC_PROFILE_NAME
Definition: ExifBitmap.php:33
ExifBitmapHandler\getImageSize
getImageSize( $image, $path)
Wrapper for base classes ImageHandler::getImageSize() that checks for rotation reported from metadata...
Definition: ExifBitmap.php:172
File\getMetadata
getMetadata()
Get handler-specific metadata Overridden by LocalFile, UnregisteredLocalFile STUB.
Definition: File.php:639
$wgExiftool
$wgExiftool
Path to exiftool binary.
Definition: DefaultSettings.php:1079
captcha-old.count
count
Definition: captcha-old.py:225
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
$params
$params
Definition: styleTest.css.php:40
ExifBitmapHandler\formatMetadata
formatMetadata( $image, $context=false)
Definition: ExifBitmap.php:131
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ExifBitmapHandler\getCommonMetaArray
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.
Definition: ExifBitmap.php:140
MediaHandler\METADATA_COMPATIBLE
const METADATA_COMPATIBLE
Definition: MediaHandler.php:34
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:51
BitmapHandler
Generic handler for bitmap images.
Definition: Bitmap.php:29
Exif\version
static version()
#-
Definition: Exif.php:586
$output
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition: hooks.txt:1049
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
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:999
FormatMetadata\collapseContactInfo
collapseContactInfo( $vals)
Format the contact info field into a single value.
Definition: FormatMetadata.php:1444
$image
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition: hooks.txt:783
ExifBitmapHandler\getMetadataType
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
Definition: ExifBitmap.php:160
MediaHandler\getMetadata
getMetadata( $image, $path)
Get handler-specific metadata which will be saved in the img_metadata field.
Definition: MediaHandler.php:122
$retval
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account incomplete not yet checked for validity & $retval
Definition: hooks.txt:246
wfEscapeShellArg
wfEscapeShellArg()
Version of escapeshellarg() that works better on Windows.
Definition: GlobalFunctions.php:2195
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1956
BitmapHandler\autoRotateEnabled
autoRotateEnabled()
Definition: Bitmap.php:528
ExifBitmapHandler\convertMetadataVersion
convertMetadataVersion( $metadata, $version=1)
Convert metadata version.
Definition: ExifBitmap.php:35
FormatMetadata
Format Image metadata values into a human readable form.
Definition: FormatMetadata.php:50
ExifBitmapHandler\getRotation
getRotation( $file)
On supporting image formats, try to read out the low-level orientation of the file and return the ang...
Definition: ExifBitmap.php:205
ExifBitmapHandler\OLD_BROKEN_FILE
const OLD_BROKEN_FILE
Definition: ExifBitmap.php:32
$wgUseTinyRGBForJPGThumbnails
$wgUseTinyRGBForJPGThumbnails
When this variable is true and JPGs use the sRGB ICC profile, swaps it for the more lightweight (and ...
Definition: DefaultSettings.php:1439
MediaHandler\logErrorForExternalProcess
logErrorForExternalProcess( $retval, $err, $cmd)
Log an error that occurred in an external process.
Definition: MediaHandler.php:764
MediaHandler\formatMetadataHelper
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
Definition: MediaHandler.php:505
$path
$path
Definition: NoLocalSettings.php:26
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ExifBitmapHandler\transformImageMagick
transformImageMagick( $image, $params)
Transform an image using ImageMagick.
Definition: ExifBitmap.php:247
MediaHandler\METADATA_BAD
const METADATA_BAD
Definition: MediaHandler.php:33
ExifBitmapHandler\swapICCProfile
swapICCProfile( $filepath, $oldProfileString, $profileFilepath)
Swaps an embedded ICC profile for another, if found.
Definition: ExifBitmap.php:280
ExifBitmapHandler\isMetadataValid
isMetadataValid( $image, $metadata)
Definition: ExifBitmap.php:87
ExifBitmapHandler\BROKEN_FILE
const BROKEN_FILE
Definition: ExifBitmap.php:31
ExifBitmapHandler\getRotationForExif
getRotationForExif( $data)
Given a chunk of serialized Exif metadata, return the orientation as degrees of rotation.
Definition: ExifBitmap.php:223
$wgShowEXIF
$wgShowEXIF
Show Exif data, on by default if available.
Definition: DefaultSettings.php:669
MediaHandler\METADATA_GOOD
const METADATA_GOOD
Definition: MediaHandler.php:32
wfShellExecWithStderr
wfShellExecWithStderr( $cmd, &$retval=null, $environ=[], $limits=[])
Execute a shell command, returning both stdout and stderr.
Definition: GlobalFunctions.php:2531