8 * Stuff specific to JPEG and (built-in) TIFF handler.
9 * All metadata related, since both JPEG and TIFF support Exif.
13 class ExifBitmapHandler
extends BitmapHandler
{
15 const BROKEN_FILE
= '-1'; // error extracting metadata
16 const OLD_BROKEN_FILE
= '0'; // outdated error extracting metadata.
18 function convertMetadataVersion( $metadata, $version = 1 ) {
19 // basically flattens arrays.
20 $version = explode(';', $version, 2);
21 $version = intval($version[0]);
22 if ( $version < 1 ||
$version >= 2 ) {
28 if ( !is_array( $metadata ) ) {
29 $metadata = unserialize( $metadata );
31 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) ||
$metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
35 // Treat Software as a special case because in can contain
36 // an array of (SoftwareName, Version).
37 if (isset( $metadata['Software'] )
38 && is_array( $metadata['Software'] )
39 && is_array( $metadata['Software'][0])
40 && isset( $metadata['Software'][0][0] )
41 && isset( $metadata['Software'][0][1])
43 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
44 . $metadata['Software'][0][1] . ')';
47 // ContactInfo also has to be dealt with specially
48 if ( isset( $metadata['Contact'] ) ) {
49 $metadata['Contact'] =
50 FormatMetadata
::collapseContactInfo(
51 $metadata['Contact'] );
54 foreach ( $metadata as &$val ) {
55 if ( is_array( $val ) ) {
56 $val = FormatMetadata
::flattenArray( $val, 'ul', $avoidHtml );
59 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
63 function isMetadataValid( $image, $metadata ) {
66 # Metadata disabled and so an empty field is expected
67 return self
::METADATA_GOOD
;
69 if ( $metadata === self
::OLD_BROKEN_FILE
) {
70 # Old special value indicating that there is no EXIF data in the file.
71 # or that there was an error well extracting the metadata.
72 wfDebug( __METHOD__
. ": back-compat version\n");
73 return self
::METADATA_COMPATIBLE
;
75 if ( $metadata === self
::BROKEN_FILE
) {
76 return self
::METADATA_GOOD
;
79 $exif = unserialize( $metadata );
81 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
82 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif
::version() )
84 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) &&
85 $exif['MEDIAWIKI_EXIF_VERSION'] == 1 )
87 //back-compatible but old
88 wfDebug( __METHOD__
.": back-compat version\n" );
89 return self
::METADATA_COMPATIBLE
;
91 # Wrong (non-compatible) version
92 wfDebug( __METHOD__
.": wrong version\n" );
93 return self
::METADATA_BAD
;
95 return self
::METADATA_GOOD
;
102 function formatMetadata( $image ) {
103 $metadata = $image->getMetadata();
104 if ( $metadata === self
::OLD_BROKEN_FILE ||
105 $metadata === self
::BROKEN_FILE ||
106 $this->isMetadataValid( $image, $metadata ) === self
::METADATA_BAD
)
108 // So we don't try and display metadata from PagedTiffHandler
109 // for example when using InstantCommons.
113 $exif = unserialize( $metadata );
117 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
118 if ( count( $exif ) == 0 ) {
121 return $this->formatMetadataHelper( $exif );
124 function getMetadataType( $image ) {
129 * Wrapper for base classes ImageHandler::getImageSize() that checks for
130 * rotation reported from metadata and swaps the sizes to match.
133 * @param string $path
136 function getImageSize( $image, $path ) {
137 global $wgEnableAutoRotation;
138 $gis = parent
::getImageSize( $image, $path );
140 // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
141 // This may mean we read EXIF data twice on initial upload.
142 if ( $wgEnableAutoRotation ) {
143 $meta = $this->getMetadata( $image, $path );
144 $rotation = $this->getRotationForExif( $meta );
149 if ($rotation == 90 ||
$rotation == 270) {
158 * On supporting image formats, try to read out the low-level orientation
159 * of the file and return the angle that the file needs to be rotated to
162 * This information is only useful when manipulating the original file;
163 * the width and height we normally work with is logical, and will match
164 * any produced output views.
167 * @return int 0, 90, 180 or 270
169 public function getRotation( $file ) {
170 global $wgEnableAutoRotation;
171 if ( !$wgEnableAutoRotation ) {
175 $data = $file->getMetadata();
176 return $this->getRotationForExif( $data );
180 * Given a chunk of serialized Exif metadata, return the orientation as
181 * degrees of rotation.
183 * @param string $data
184 * @return int 0, 90, 180 or 270
185 * @fixme orientation can include flipping as well; see if this is an issue!
187 protected function getRotationForExif( $data ) {
191 wfSuppressWarnings();
192 $data = unserialize( $data );
194 if ( isset( $data['Orientation'] ) ) {
195 # See http://sylvana.net/jpegcrop/exif_orientation.html
196 switch ( $data['Orientation'] ) {