MediaWiki  1.23.14
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 
34  function convertMetadataVersion( $metadata, $version = 1 ) {
35  // basically flattens arrays.
36  $version = explode( ';', $version, 2 );
37  $version = intval( $version[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 ) {
88  global $wgShowEXIF;
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  }
104  $exif = unserialize( $metadata );
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 
130  function formatMetadata( $image ) {
131  $meta = $this->getCommonMetaArray( $image );
132  if ( count( $meta ) === 0 ) {
133  return false;
134  }
135 
136  return $this->formatMetadataHelper( $meta );
137  }
138 
139  public function getCommonMetaArray( File $file ) {
140  $metadata = $file->getMetadata();
141  if ( $metadata === self::OLD_BROKEN_FILE
142  || $metadata === self::BROKEN_FILE
143  || $this->isMetadataValid( $file, $metadata ) === self::METADATA_BAD
144  ) {
145  // So we don't try and display metadata from PagedTiffHandler
146  // for example when using InstantCommons.
147  return array();
148  }
149 
150  $exif = unserialize( $metadata );
151  if ( !$exif ) {
152  return array();
153  }
154  unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
155 
156  return $exif;
157  }
158 
159  function getMetadataType( $image ) {
160  return 'exif';
161  }
162 
171  function getImageSize( $image, $path ) {
172  $gis = parent::getImageSize( $image, $path );
173 
174  // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
175  // This may mean we read EXIF data twice on initial upload.
177  $meta = $this->getMetadata( $image, $path );
178  $rotation = $this->getRotationForExif( $meta );
179  } else {
180  $rotation = 0;
181  }
182 
183  if ( $rotation == 90 || $rotation == 270 ) {
184  $width = $gis[0];
185  $gis[0] = $gis[1];
186  $gis[1] = $width;
187  }
188 
189  return $gis;
190  }
191 
204  public function getRotation( $file ) {
206  return 0;
207  }
208 
209  $data = $file->getMetadata();
210 
211  return $this->getRotationForExif( $data );
212  }
213 
222  protected function getRotationForExif( $data ) {
223  if ( !$data ) {
224  return 0;
225  }
227  $data = unserialize( $data );
229  if ( isset( $data['Orientation'] ) ) {
230  # See http://sylvana.net/jpegcrop/exif_orientation.html
231  switch ( $data['Orientation'] ) {
232  case 8:
233  return 90;
234  case 3:
235  return 180;
236  case 6:
237  return 270;
238  default:
239  return 0;
240  }
241  }
242 
243  return 0;
244  }
245 }
BitmapHandler\autoRotateEnabled
static autoRotateEnabled()
Definition: Bitmap.php:768
ExifBitmapHandler
Stuff specific to JPEG and (built-in) TIFF handler.
Definition: ExifBitmap.php:30
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ExifBitmapHandler\getImageSize
getImageSize( $image, $path)
Wrapper for base classes ImageHandler::getImageSize() that checks for rotation reported from metadata...
Definition: ExifBitmap.php:171
wfSuppressWarnings
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
Definition: GlobalFunctions.php:2434
ExifBitmapHandler\getCommonMetaArray
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.
Definition: ExifBitmap.php:139
MediaHandler\METADATA_COMPATIBLE
const METADATA_COMPATIBLE
Definition: MediaHandler.php:33
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:50
wfRestoreWarnings
wfRestoreWarnings()
Restore error level to previous value.
Definition: GlobalFunctions.php:2464
BitmapHandler
Generic handler for bitmap images.
Definition: Bitmap.php:29
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
Exif\version
static version()
#-
Definition: Exif.php:580
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
MediaHandler\formatMetadataHelper
formatMetadataHelper( $metadataArray)
sorts the visible/invisible field.
Definition: MediaHandler.php:500
FormatMetadata\collapseContactInfo
collapseContactInfo( $vals)
Format the contact info field into a single value.
Definition: FormatMetadata.php:1456
ExifBitmapHandler\getMetadataType
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
Definition: ExifBitmap.php:159
MediaHandler\getMetadata
getMetadata( $image, $path)
Get handler-specific metadata which will be saved in the img_metadata field.
Definition: MediaHandler.php:126
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:980
ExifBitmapHandler\formatMetadata
formatMetadata( $image)
Definition: ExifBitmap.php:130
$version
$version
Definition: parserTests.php:86
ExifBitmapHandler\convertMetadataVersion
convertMetadataVersion( $metadata, $version=1)
Convert metadata version.
Definition: ExifBitmap.php:34
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
FormatMetadata
Format Image metadata values into a human readable form.
Definition: FormatMetadata.php:49
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:204
ExifBitmapHandler\OLD_BROKEN_FILE
const OLD_BROKEN_FILE
Definition: ExifBitmap.php:32
$path
$path
Definition: NoLocalSettings.php:35
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
MediaHandler\METADATA_BAD
const METADATA_BAD
Definition: MediaHandler.php:32
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:222
MediaHandler\METADATA_GOOD
const METADATA_GOOD
Definition: MediaHandler.php:31