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
{
34 function getMetadata( $image, $filename ) {
36 $meta = BitmapMetadataHandler
::Jpeg( $filename );
37 if ( !is_array( $meta ) ) {
38 // This should never happen, but doesn't hurt to be paranoid.
39 throw new MWException( 'Metadata array is not an array' );
41 $meta['MEDIAWIKI_EXIF_VERSION'] = Exif
::version();
43 return serialize( $meta );
44 } catch ( MWException
$e ) {
45 // BitmapMetadataHandler throws an exception in certain exceptional
46 // 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.
59 return ExifBitmapHandler
::BROKEN_FILE
;
65 * @param array $params Rotate parameters.
66 * 'rotation' clockwise rotation in degrees, allowed are multiples of 90
70 public function rotate( $file, $params ) {
73 $rotation = ( $params['rotation'] +
$this->getRotation( $file ) ) %
360;
75 if ( $wgJpegTran && is_file( $wgJpegTran ) ) {
76 $cmd = wfEscapeShellArg( $wgJpegTran ) .
77 " -rotate " . wfEscapeShellArg( $rotation ) .
78 " -outfile " . wfEscapeShellArg( $params['dstPath'] ) .
79 " " . wfEscapeShellArg( $params['srcPath'] );
80 wfDebug( __METHOD__
. ": running jpgtran: $cmd\n" );
81 wfProfileIn( 'jpegtran' );
83 $err = wfShellExecWithStderr( $cmd, $retval );
84 wfProfileOut( 'jpegtran' );
85 if ( $retval !== 0 ) {
86 $this->logErrorForExternalProcess( $retval, $err, $cmd );
88 return new MediaTransformError( 'thumbnail_error', 0, 0, $err );
93 return parent
::rotate( $file, $params );