3 * Handler for JPEG images.
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 * JPEG specific handler.
26 * Inherits most stuff from BitmapHandler, just here to do the metadata handler differently.
28 * Metadata stuff common to Jpeg and built-in Tiff (not PagedTiffHandler) is
29 * in ExifBitmapHandler.
33 class JpegHandler
extends ExifBitmapHandler
{
35 function getMetadata( $image, $filename ) {
37 $meta = BitmapMetadataHandler
::Jpeg( $filename );
38 if ( !is_array( $meta ) ) {
39 // This should never happen, but doesn't hurt to be paranoid.
40 throw new MWException( 'Metadata array is not an array' );
42 $meta['MEDIAWIKI_EXIF_VERSION'] = Exif
::version();
43 return serialize( $meta );
45 catch ( MWException
$e ) {
46 // BitmapMetadataHandler throws an exception in certain exceptional cases like if file does not exist.
47 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
49 /* This used to use 0 (ExifBitmapHandler::OLD_BROKEN_FILE) for the cases
50 * * No metadata in the file
51 * * Something is broken in the file.
52 * However, if the metadata support gets expanded then you can't tell if the 0 is from
53 * a broken file, or just no props found. A broken file is likely to stay broken, but
54 * a file which had no props could have props once the metadata support is improved.
55 * Thus switch to using -1 to denote only a broken file, and use an array with only
56 * MEDIAWIKI_EXIF_VERSION to denote no props.
58 return ExifBitmapHandler
::BROKEN_FILE
;
64 * @param array $params Rotate parameters.
65 * 'rotation' clockwise rotation in degrees, allowed are multiples of 90
69 public function rotate( $file, $params ) {
72 $rotation = ( $params['rotation'] +
$this->getRotation( $file ) ) %
360;
74 if ( $wgJpegTran && is_file( $wgJpegTran ) ) {
75 $cmd = wfEscapeShellArg( $wgJpegTran ) .
76 " -rotate " . wfEscapeShellArg( $rotation ) .
77 " -outfile " . wfEscapeShellArg( $params['dstPath'] ) .
78 " " . wfEscapeShellArg( $params['srcPath'] ) . " 2>&1";
79 wfDebug( __METHOD__
. ": running jpgtran: $cmd\n" );
80 wfProfileIn( 'jpegtran' );
82 $err = wfShellExec( $cmd, $retval, $env );
83 wfProfileOut( 'jpegtran' );
84 if ( $retval !== 0 ) {
85 $this->logErrorForExternalProcess( $retval, $err, $cmd );
86 return new MediaTransformError( 'thumbnail_error', 0, 0, $err );
90 return parent
::rotate( $file, $params );