Revert "jquery.textSelection: Remove hardcoded checks for removed WikiEditor iframe...
[lhc/web/wiklou.git] / includes / media / Jpeg.php
1 <?php
2 /**
3 * Handler for JPEG images.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * JPEG specific handler.
26 * Inherits most stuff from BitmapHandler, just here to do the metadata handler differently.
27 *
28 * Metadata stuff common to Jpeg and built-in Tiff (not PagedTiffHandler) is
29 * in ExifBitmapHandler.
30 *
31 * @ingroup Media
32 */
33 class JpegHandler extends ExifBitmapHandler {
34 function getMetadata( $image, $filename ) {
35 try {
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' );
40 }
41 $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
42
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" );
48
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.
57 */
58
59 return ExifBitmapHandler::BROKEN_FILE;
60 }
61 }
62
63 /**
64 * @param File $file
65 * @param array $params Rotate parameters.
66 * 'rotation' clockwise rotation in degrees, allowed are multiples of 90
67 * @since 1.21
68 * @return bool
69 */
70 public function rotate( $file, $params ) {
71 global $wgJpegTran;
72
73 $rotation = ( $params['rotation'] + $this->getRotation( $file ) ) % 360;
74
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' );
82 $retval = 0;
83 $err = wfShellExecWithStderr( $cmd, $retval );
84 wfProfileOut( 'jpegtran' );
85 if ( $retval !== 0 ) {
86 $this->logErrorForExternalProcess( $retval, $err, $cmd );
87
88 return new MediaTransformError( 'thumbnail_error', 0, 0, $err );
89 }
90
91 return false;
92 } else {
93 return parent::rotate( $file, $params );
94 }
95 }
96 }