MediaWiki master
JpegHandler.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Media;
11
16
27 private const SRGB_EXIF_COLOR_SPACE = 'sRGB';
28 private const SRGB_ICC_PROFILE_DESCRIPTION = 'sRGB IEC61966-2.1';
29
31 public function normaliseParams( $image, &$params ) {
32 if ( !parent::normaliseParams( $image, $params ) ) {
33 return false;
34 }
35 if ( isset( $params['quality'] ) && !self::validateQuality( $params['quality'] ) ) {
36 return false;
37 }
38 return true;
39 }
40
42 public function validateParam( $name, $value ) {
43 if ( $name === 'quality' ) {
44 return self::validateQuality( $value );
45 }
46 return parent::validateParam( $name, $value );
47 }
48
53 private static function validateQuality( $value ) {
54 return $value === 'low';
55 }
56
58 public function makeParamString( $params ) {
59 // Prepend quality as "qValue-". This has to match parseParamString() below
60 $res = parent::makeParamString( $params );
61 if ( $res && isset( $params['quality'] ) ) {
62 $res = "q{$params['quality']}-$res";
63 }
64 return $res;
65 }
66
68 public function parseParamString( $str ) {
69 // $str contains "qlow-200px" or "200px" strings because thumb.php would strip the filename
70 // first - check if the string begins with "qlow-", and if so, treat it as quality.
71 // Pass the first portion, or the whole string if "qlow-" not found, to the parent
72 // The parsing must match the makeParamString() above
73 $res = false;
74 $m = false;
75 if ( preg_match( '/q([^-]+)-(.*)$/', $str, $m ) ) {
76 $v = $m[1];
77 if ( self::validateQuality( $v ) ) {
78 $res = parent::parseParamString( $m[2] );
79 if ( $res ) {
80 $res['quality'] = $v;
81 }
82 }
83 } else {
84 $res = parent::parseParamString( $str );
85 }
86 return $res;
87 }
88
90 protected function getScriptParams( $params ) {
91 $res = parent::getScriptParams( $params );
92 if ( isset( $params['quality'] ) ) {
93 $res['quality'] = $params['quality'];
94 }
95 return $res;
96 }
97
99 public function getSizeAndMetadata( $state, $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 InvalidJpegException( 'Metadata array is not an array' );
105 }
106 $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
107
108 $info = [
109 'width' => $meta['SOF']['width'] ?? 0,
110 'height' => $meta['SOF']['height'] ?? 0,
111 ];
112 if ( isset( $meta['SOF']['bits'] ) ) {
113 $info['bits'] = $meta['SOF']['bits'];
114 }
115 $info = $this->applyExifRotation( $info, $meta );
116 unset( $meta['SOF'] );
117 $info['metadata'] = $meta;
118 return $info;
119 } catch ( InvalidJpegException $e ) {
120 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
121
122 // This used to return an integer-like string from getMetadata(),
123 // producing a value which could not be unserialized in
124 // img_metadata. The "_error" array key matches the legacy
125 // unserialization for such image rows.
126 return [ 'metadata' => [ '_error' => ExifBitmapHandler::BROKEN_FILE ] ];
127 }
128 }
129
137 public function rotate( $file, $params ) {
138 $jpegTran = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::JpegTran );
139
140 $rotation = ( $params['rotation'] + $this->getRotation( $file ) ) % 360;
141
142 if ( $jpegTran && is_executable( $jpegTran ) ) {
143 $command = Shell::command( $jpegTran,
144 '-rotate',
145 (string)$rotation,
146 '-outfile',
147 $params['dstPath'],
148 $params['srcPath']
149 );
150 $result = $command
151 ->includeStderr()
152 ->execute();
153 if ( $result->getExitCode() !== 0 ) {
154 $this->logErrorForExternalProcess( $result->getExitCode(),
155 $result->getStdout(),
156 $command->__toString()
157 );
158
159 return new MediaTransformError( 'thumbnail_error', 0, 0, $result->getStdout() );
160 }
161
162 return false;
163 }
164 return parent::rotate( $file, $params );
165 }
166
168 public function supportsBucketing() {
169 return true;
170 }
171
173 public function sanitizeParamsForBucketing( $params ) {
174 $params = parent::sanitizeParamsForBucketing( $params );
175
176 // Quality needs to be cleared for bucketing. Buckets need to be default quality
177 unset( $params['quality'] );
178
179 return $params;
180 }
181
185 protected function transformImageMagick( $image, $params ) {
186 $useTinyRGBForJPGThumbnails = MediaWikiServices::getInstance()
187 ->getMainConfig()->get( MainConfigNames::UseTinyRGBForJPGThumbnails );
188
189 $ret = parent::transformImageMagick( $image, $params );
190
191 if ( $ret ) {
192 return $ret;
193 }
194
195 if ( $useTinyRGBForJPGThumbnails ) {
196 // T100976 If the profile embedded in the JPG is sRGB, swap it for the smaller
197 // (and free) TinyRGB
198
208 $colorSpaces = [ self::SRGB_EXIF_COLOR_SPACE, '-' ];
209 $profiles = [ self::SRGB_ICC_PROFILE_DESCRIPTION ];
210
211 // we'll also add TinyRGB profile to images lacking a profile, but
212 // only if they're not low quality (which are meant to save bandwidth
213 // and we don't want to increase the filesize by adding a profile)
214 if ( isset( $params['quality'] ) && $params['quality'] > 30 ) {
215 $profiles[] = '-';
216 }
217
218 $this->swapICCProfile(
219 $params['dstPath'],
220 $colorSpaces,
221 $profiles,
222 realpath( __DIR__ ) . '/tinyrgb.icc'
223 );
224 }
225
226 return false;
227 }
228
240 public function swapICCProfile( $filepath, array $colorSpaces,
241 array $oldProfileStrings, $profileFilepath
242 ) {
243 $exiftool = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Exiftool );
244
245 if ( !$exiftool || !is_executable( $exiftool ) ) {
246 return false;
247 }
248
249 $result = Shell::command(
250 $exiftool,
251 '-EXIF:ColorSpace',
252 '-ICC_Profile:ProfileDescription',
253 '-S',
254 '-T',
255 $filepath
256 )
257 ->includeStderr()
258 ->execute();
259
260 // Explode EXIF data into an array with [0 => Color Space, 1 => Device Model Desc]
261 $data = explode( "\t", trim( $result->getStdout() ), 3 );
262
263 if ( $result->getExitCode() !== 0 ) {
264 return false;
265 }
266
267 // Make a regex out of the source data to match it to an array of color
268 // spaces in a case-insensitive way
269 $colorSpaceRegex = '/' . preg_quote( $data[0], '/' ) . '/i';
270 if ( !preg_grep( $colorSpaceRegex, $colorSpaces ) ) {
271 // We can't establish that this file matches the color space, don't process it
272 return false;
273 }
274
275 $profileRegex = '/' . preg_quote( $data[1], '/' ) . '/i';
276 if ( !preg_grep( $profileRegex, $oldProfileStrings ) ) {
277 // We can't establish that this file has the expected ICC profile, don't process it
278 return false;
279 }
280
281 $command = Shell::command( $exiftool,
282 '-overwrite_original',
283 '-icc_profile<=' . $profileFilepath,
284 $filepath
285 );
286 $result = $command
287 ->includeStderr()
288 ->execute();
289
290 if ( $result->getExitCode() !== 0 ) {
291 $this->logErrorForExternalProcess( $result->getExitCode(),
292 $result->getStdout(),
293 $command->__toString()
294 );
295
296 return false;
297 }
298
299 return true;
300 }
301}
302
304class_alias( JpegHandler::class, 'JpegHandler' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
A class containing constants representing the names of configuration variables.
const UseTinyRGBForJPGThumbnails
Name constant for the UseTinyRGBForJPGThumbnails setting, for use with Config::get()
const JpegTran
Name constant for the JpegTran setting, for use with Config::get()
const Exiftool
Name constant for the Exiftool setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
static Jpeg( $filename)
Main entry point for jpeg's.
Stuff specific to JPEG and (built-in) TIFF handler.
getRotation( $file)
On supporting image formats, try to read out the low-level orientation of the file and return the ang...
const BROKEN_FILE
Error extracting metadata.
static version()
The version of the output format.
Definition Exif.php:691
JPEG specific handler.
getSizeAndMetadata( $state, $filename)
Get image size information and metadata array.If this returns null, the caller will fall back to getI...
normaliseParams( $image, &$params)
to override bool
parseParamString( $str)
Parse a param string made with makeParamString back into an array.array|false Array of parameters or ...
validateParam( $name, $value)
Validate a thumbnail parameter at parse time.Return true to accept the parameter, and false to reject...
swapICCProfile( $filepath, array $colorSpaces, array $oldProfileStrings, $profileFilepath)
Swaps an embedded ICC profile for another, if found.
sanitizeParamsForBucketing( $params)
Returns a normalised params array for which parameters have been cleaned up for bucketing purposes....
supportsBucketing()
Returns whether or not this handler supports the chained generation of thumbnails according to bucket...
getScriptParams( $params)
to override array
makeParamString( $params)
Merge a parameter array into a string appropriate for inclusion in filenames.stringto override to ove...
transformImageMagick( $image, $params)
Transform an image using ImageMagick.to overrideMediaTransformError|false Error object if error occur...
logErrorForExternalProcess( $retval, $err, $cmd)
Log an error that occurred in an external process.
Basic media transform error class.
Executes shell commands.
Definition Shell.php:32