Always use 'bool' instead of 'boolean' after '@param' and '@return'
[mediawiki.git] / includes / media / Jpeg.php
blobfbdbdfe31e8f9c232bc71f7760250787542d29e6
1 <?php
2 /**
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
20 * @file
21 * @ingroup Media
24 /**
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.
31 * @ingroup Media
33 class JpegHandler extends ExifBitmapHandler {
35 function normaliseParams( $image, &$params ) {
36 if ( !parent::normaliseParams( $image, $params ) ) {
37 return false;
39 if ( isset( $params['quality'] ) && !self::validateQuality( $params['quality'] ) ) {
40 return false;
42 return true;
45 function validateParam( $name, $value ) {
46 if ( $name === 'quality' ) {
47 return self::validateQuality( $value );
48 } else {
49 return parent::validateParam( $name, $value );
53 /** Validate and normalize quality value to be between 1 and 100 (inclusive).
54 * @param int $value Quality value, will be converted to integer or 0 if invalid
55 * @return bool True if the value is valid
57 private static function validateQuality( $value ) {
58 return $value === 'low';
61 function makeParamString( $params ) {
62 // Prepend quality as "qValue-". This has to match parseParamString() below
63 $res = parent::makeParamString( $params );
64 if ( $res && isset( $params['quality'] ) ) {
65 $res = "q{$params['quality']}-$res";
67 return $res;
70 function parseParamString( $str ) {
71 // $str contains "qlow-200px" or "200px" strings because thumb.php would strip the filename
72 // first - check if the string begins with "qlow-", and if so, treat it as quality.
73 // Pass the first portion, or the whole string if "qlow-" not found, to the parent
74 // The parsing must match the makeParamString() above
75 $res = false;
76 $m = false;
77 if ( preg_match( '/q([^-]+)-(.*)$/', $str, $m ) ) {
78 $v = $m[1];
79 if ( self::validateQuality( $v ) ) {
80 $res = parent::parseParamString( $m[2] );
81 if ( $res ) {
82 $res['quality'] = $v;
85 } else {
86 $res = parent::parseParamString( $str );
88 return $res;
91 function getScriptParams( $params ) {
92 $res = parent::getScriptParams( $params );
93 if ( isset( $params['quality'] ) ) {
94 $res['quality'] = $params['quality'];
96 return $res;
99 function getMetadata( $image, $filename ) {
100 try {
101 $meta = BitmapMetadataHandler::Jpeg( $filename );
102 if ( !is_array( $meta ) ) {
103 // This should never happen, but doesn't hurt to be paranoid.
104 throw new MWException( 'Metadata array is not an array' );
106 $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
108 return serialize( $meta );
109 } catch ( MWException $e ) {
110 // BitmapMetadataHandler throws an exception in certain exceptional
111 // cases like if file does not exist.
112 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
114 /* This used to use 0 (ExifBitmapHandler::OLD_BROKEN_FILE) for the cases
115 * * No metadata in the file
116 * * Something is broken in the file.
117 * However, if the metadata support gets expanded then you can't tell if the 0 is from
118 * a broken file, or just no props found. A broken file is likely to stay broken, but
119 * a file which had no props could have props once the metadata support is improved.
120 * Thus switch to using -1 to denote only a broken file, and use an array with only
121 * MEDIAWIKI_EXIF_VERSION to denote no props.
124 return ExifBitmapHandler::BROKEN_FILE;
129 * @param File $file
130 * @param array $params Rotate parameters.
131 * 'rotation' clockwise rotation in degrees, allowed are multiples of 90
132 * @since 1.21
133 * @return bool
135 public function rotate( $file, $params ) {
136 global $wgJpegTran;
138 $rotation = ( $params['rotation'] + $this->getRotation( $file ) ) % 360;
140 if ( $wgJpegTran && is_file( $wgJpegTran ) ) {
141 $cmd = wfEscapeShellArg( $wgJpegTran ) .
142 " -rotate " . wfEscapeShellArg( $rotation ) .
143 " -outfile " . wfEscapeShellArg( $params['dstPath'] ) .
144 " " . wfEscapeShellArg( $params['srcPath'] );
145 wfDebug( __METHOD__ . ": running jpgtran: $cmd\n" );
146 wfProfileIn( 'jpegtran' );
147 $retval = 0;
148 $err = wfShellExecWithStderr( $cmd, $retval );
149 wfProfileOut( 'jpegtran' );
150 if ( $retval !== 0 ) {
151 $this->logErrorForExternalProcess( $retval, $err, $cmd );
153 return new MediaTransformError( 'thumbnail_error', 0, 0, $err );
156 return false;
157 } else {
158 return parent::rotate( $file, $params );
162 public function supportsBucketing() {
163 return true;
166 public function sanitizeParamsForBucketing( $params ) {
167 $params = parent::sanitizeParamsForBucketing( $params );
169 // Quality needs to be cleared for bucketing. Buckets need to be default quality
170 if ( isset( $params['quality'] ) ) {
171 unset( $params['quality'] );
174 return $params;