| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use MediaWiki\Shell\Shell; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class JpegHandler extends ExifBitmapHandler { |
| 38 |
private const SRGB_EXIF_COLOR_SPACE = 'sRGB'; |
| 39 |
private const SRGB_ICC_PROFILE_DESCRIPTION = 'sRGB IEC61966-2.1'; |
| 40 |
|
| 41 |
public function normaliseParams( $image, &$params ) { |
| 42 |
if ( !parent::normaliseParams( $image, $params ) ) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
if ( isset( $params['quality'] ) && !self::validateQuality( $params['quality'] ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
public function validateParam( $name, $value ) { |
| 52 |
if ( $name === 'quality' ) { |
| 53 |
return self::validateQuality( $value ); |
| 54 |
} |
| 55 |
return parent::validateParam( $name, $value ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private static function validateQuality( $value ) { |
| 63 |
return $value === 'low'; |
| 64 |
} |
| 65 |
|
| 66 |
public function makeParamString( $params ) { |
| 67 |
|
| 68 |
$res = parent::makeParamString( $params ); |
| 69 |
if ( $res && isset( $params['quality'] ) ) { |
| 70 |
$res = "q{$params['quality']}-$res"; |
| 71 |
} |
| 72 |
return $res; |
| 73 |
} |
| 74 |
|
| 75 |
public function parseParamString( $str ) { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
$res = false; |
| 81 |
$m = false; |
| 82 |
if ( preg_match( '/q([^-]+)-(.*)$/', $str, $m ) ) { |
| 83 |
$v = $m[1]; |
| 84 |
if ( self::validateQuality( $v ) ) { |
| 85 |
$res = parent::parseParamString( $m[2] ); |
| 86 |
if ( $res ) { |
| 87 |
$res['quality'] = $v; |
| 88 |
} |
| 89 |
} |
| 90 |
} else { |
| 91 |
$res = parent::parseParamString( $str ); |
| 92 |
} |
| 93 |
return $res; |
| 94 |
} |
| 95 |
|
| 96 |
protected function getScriptParams( $params ) { |
| 97 |
$res = parent::getScriptParams( $params ); |
| 98 |
if ( isset( $params['quality'] ) ) { |
| 99 |
$res['quality'] = $params['quality']; |
| 100 |
} |
| 101 |
return $res; |
| 102 |
} |
| 103 |
|
| 104 |
public function getSizeAndMetadata( $state, $filename ) { |
| 105 |
try { |
| 106 |
$meta = BitmapMetadataHandler::Jpeg( $filename ); |
| 107 |
if ( !is_array( $meta ) ) { |
| 108 |
|
| 109 |
throw new InvalidJpegException( 'Metadata array is not an array' ); |
| 110 |
} |
| 111 |
$meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version(); |
| 112 |
|
| 113 |
$info = [ |
| 114 |
'width' => $meta['SOF']['width'] ?? 0, |
| 115 |
'height' => $meta['SOF']['height'] ?? 0, |
| 116 |
]; |
| 117 |
if ( isset( $meta['SOF']['bits'] ) ) { |
| 118 |
$info['bits'] = $meta['SOF']['bits']; |
| 119 |
} |
| 120 |
$info = $this->applyExifRotation( $info, $meta ); |
| 121 |
unset( $meta['SOF'] ); |
| 122 |
$info['metadata'] = $meta; |
| 123 |
return $info; |
| 124 |
} catch ( InvalidJpegException $e ) { |
| 125 |
wfDebug( __METHOD__ . ': ' . $e->getMessage() ); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
return [ 'metadata' => [ '_error' => ExifBitmapHandler::BROKEN_FILE ] ]; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public function rotate( $file, $params ) { |
| 143 |
$jpegTran = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::JpegTran ); |
| 144 |
|
| 145 |
$rotation = ( $params['rotation'] + $this->getRotation( $file ) ) % 360; |
| 146 |
|
| 147 |
if ( $jpegTran && is_executable( $jpegTran ) ) { |
| 148 |
$command = Shell::command( $jpegTran, |
| 149 |
'-rotate', |
| 150 |
(string)$rotation, |
| 151 |
'-outfile', |
| 152 |
$params['dstPath'], |
| 153 |
$params['srcPath'] |
| 154 |
); |
| 155 |
$result = $command |
| 156 |
->includeStderr() |
| 157 |
->execute(); |
| 158 |
if ( $result->getExitCode() !== 0 ) { |
| 159 |
$this->logErrorForExternalProcess( $result->getExitCode(), |
| 160 |
$result->getStdout(), |
| 161 |
$command |
| 162 |
); |
| 163 |
|
| 164 |
return new MediaTransformError( 'thumbnail_error', 0, 0, $result->getStdout() ); |
| 165 |
} |
| 166 |
|
| 167 |
return false; |
| 168 |
} |
| 169 |
return parent::rotate( $file, $params ); |
| 170 |
} |
| 171 |
|
| 172 |
public function supportsBucketing() { |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
public function sanitizeParamsForBucketing( $params ) { |
| 177 |
$params = parent::sanitizeParamsForBucketing( $params ); |
| 178 |
|
| 179 |
|
| 180 |
unset( $params['quality'] ); |
| 181 |
|
| 182 |
return $params; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
protected function transformImageMagick( $image, $params ) { |
| 189 |
$useTinyRGBForJPGThumbnails = MediaWikiServices::getInstance() |
| 190 |
->getMainConfig()->get( MainConfigNames::UseTinyRGBForJPGThumbnails ); |
| 191 |
|
| 192 |
$ret = parent::transformImageMagick( $image, $params ); |
| 193 |
|
| 194 |
if ( $ret ) { |
| 195 |
return $ret; |
| 196 |
} |
| 197 |
|
| 198 |
if ( $useTinyRGBForJPGThumbnails ) { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
$colorSpaces = [ self::SRGB_EXIF_COLOR_SPACE, '-' ]; |
| 212 |
$profiles = [ self::SRGB_ICC_PROFILE_DESCRIPTION ]; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if ( isset( $params['quality'] ) && $params['quality'] > 30 ) { |
| 218 |
$profiles[] = '-'; |
| 219 |
} |
| 220 |
|
| 221 |
$this->swapICCProfile( |
| 222 |
$params['dstPath'], |
| 223 |
$colorSpaces, |
| 224 |
$profiles, |
| 225 |
realpath( __DIR__ ) . '/tinyrgb.icc' |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function swapICCProfile( $filepath, array $colorSpaces, |
| 244 |
array $oldProfileStrings, $profileFilepath |
| 245 |
) { |
| 246 |
$exiftool = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Exiftool ); |
| 247 |
|
| 248 |
if ( !$exiftool || !is_executable( $exiftool ) ) { |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
$result = Shell::command( |
| 253 |
$exiftool, |
| 254 |
'-EXIF:ColorSpace', |
| 255 |
'-ICC_Profile:ProfileDescription', |
| 256 |
'-S', |
| 257 |
'-T', |
| 258 |
$filepath |
| 259 |
) |
| 260 |
->includeStderr() |
| 261 |
->execute(); |
| 262 |
|
| 263 |
|
| 264 |
$data = explode( "\t", trim( $result->getStdout() ), 3 ); |
| 265 |
|
| 266 |
if ( $result->getExitCode() !== 0 ) { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
$colorSpaceRegex = '/' . preg_quote( $data[0], '/' ) . '/i'; |
| 273 |
if ( !preg_grep( $colorSpaceRegex, $colorSpaces ) ) { |
| 274 |
|
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
$profileRegex = '/' . preg_quote( $data[1], '/' ) . '/i'; |
| 279 |
if ( !preg_grep( $profileRegex, $oldProfileStrings ) ) { |
| 280 |
|
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
$command = Shell::command( $exiftool, |
| 285 |
'-overwrite_original', |
| 286 |
'-icc_profile<=' . $profileFilepath, |
| 287 |
$filepath |
| 288 |
); |
| 289 |
$result = $command |
| 290 |
->includeStderr() |
| 291 |
->execute(); |
| 292 |
|
| 293 |
if ( $result->getExitCode() !== 0 ) { |
| 294 |
$this->logErrorForExternalProcess( $result->getExitCode(), |
| 295 |
$result->getStdout(), |
| 296 |
$command |
| 297 |
); |
| 298 |
|
| 299 |
return false; |
| 300 |
} |
| 301 |
|
| 302 |
return true; |
| 303 |
} |
| 304 |
} |