3 * Handler for bitmap images with exif metadata.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Stuff specific to JPEG and (built-in) TIFF handler.
26 * All metadata related, since both JPEG and TIFF support Exif.
30 class ExifBitmapHandler
extends BitmapHandler
{
32 const BROKEN_FILE
= '-1'; // error extracting metadata
33 const OLD_BROKEN_FILE
= '0'; // outdated error extracting metadata.
35 function convertMetadataVersion( $metadata, $version = 1 ) {
36 // basically flattens arrays.
37 $version = explode( ';', $version, 2 );
38 $version = intval( $version[0] );
39 if ( $version < 1 ||
$version >= 2 ) {
45 if ( !is_array( $metadata ) ) {
46 $metadata = unserialize( $metadata );
48 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) ||
$metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
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] )
60 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
61 . $metadata['Software'][0][1] . ')';
64 // ContactInfo also has to be dealt with specially
65 if ( isset( $metadata['Contact'] ) ) {
66 $metadata['Contact'] =
67 FormatMetadata
::collapseContactInfo(
68 $metadata['Contact'] );
71 foreach ( $metadata as &$val ) {
72 if ( is_array( $val ) ) {
73 $val = FormatMetadata
::flattenArray( $val, 'ul', $avoidHtml );
76 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
80 function isMetadataValid( $image, $metadata ) {
83 # Metadata disabled and so an empty field is expected
84 return self
::METADATA_GOOD
;
86 if ( $metadata === self
::OLD_BROKEN_FILE
) {
87 # Old special value indicating that there is no EXIF data in the file.
88 # or that there was an error well extracting the metadata.
89 wfDebug( __METHOD__
. ": back-compat version\n" );
90 return self
::METADATA_COMPATIBLE
;
92 if ( $metadata === self
::BROKEN_FILE
) {
93 return self
::METADATA_GOOD
;
96 $exif = unserialize( $metadata );
98 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
99 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif
::version() )
101 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) &&
102 $exif['MEDIAWIKI_EXIF_VERSION'] == 1 )
104 //back-compatible but old
105 wfDebug( __METHOD__
. ": back-compat version\n" );
106 return self
::METADATA_COMPATIBLE
;
108 # Wrong (non-compatible) version
109 wfDebug( __METHOD__
. ": wrong version\n" );
110 return self
::METADATA_BAD
;
112 return self
::METADATA_GOOD
;
119 function formatMetadata( $image ) {
120 $metadata = $image->getMetadata();
121 if ( $metadata === self
::OLD_BROKEN_FILE ||
122 $metadata === self
::BROKEN_FILE ||
123 $this->isMetadataValid( $image, $metadata ) === self
::METADATA_BAD
)
125 // So we don't try and display metadata from PagedTiffHandler
126 // for example when using InstantCommons.
130 $exif = unserialize( $metadata );
134 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
135 if ( count( $exif ) == 0 ) {
138 return $this->formatMetadataHelper( $exif );
141 function getMetadataType( $image ) {
146 * Wrapper for base classes ImageHandler::getImageSize() that checks for
147 * rotation reported from metadata and swaps the sizes to match.
150 * @param string $path
153 function getImageSize( $image, $path ) {
154 global $wgEnableAutoRotation;
155 $gis = parent
::getImageSize( $image, $path );
157 // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
158 // This may mean we read EXIF data twice on initial upload.
159 if ( $wgEnableAutoRotation ) {
160 $meta = $this->getMetadata( $image, $path );
161 $rotation = $this->getRotationForExif( $meta );
166 if ( $rotation == 90 ||
$rotation == 270 ) {
175 * On supporting image formats, try to read out the low-level orientation
176 * of the file and return the angle that the file needs to be rotated to
179 * This information is only useful when manipulating the original file;
180 * the width and height we normally work with is logical, and will match
181 * any produced output views.
184 * @return int 0, 90, 180 or 270
186 public function getRotation( $file ) {
187 global $wgEnableAutoRotation;
188 if ( !$wgEnableAutoRotation ) {
192 $data = $file->getMetadata();
193 return $this->getRotationForExif( $data );
197 * Given a chunk of serialized Exif metadata, return the orientation as
198 * degrees of rotation.
200 * @param string $data
201 * @return int 0, 90, 180 or 270
202 * @todo FIXME orientation can include flipping as well; see if this is an
205 protected function getRotationForExif( $data ) {
209 wfSuppressWarnings();
210 $data = unserialize( $data );
212 if ( isset( $data['Orientation'] ) ) {
213 # See http://sylvana.net/jpegcrop/exif_orientation.html
214 switch ( $data['Orientation'] ) {