From f1d0d364c4dfe2e2a11eb1e34d6c620f2c0bf8f2 Mon Sep 17 00:00:00 2001 From: Ryan Schmidt Date: Thu, 19 Apr 2018 10:00:16 -0700 Subject: [PATCH] Add $wgJpegQuality to control jpg thumb quality This new configuration variable defaults to 80, which is the current quality in use when scaling jpeg thumbnails with ImageMagick. GD used a quality of 95, which was reduced to 80 as a result of this change. Bug: T192248 Change-Id: I94cad01cc876ef9083250f5d1989a73cd19617fb --- RELEASE-NOTES-1.32 | 2 ++ includes/DefaultSettings.php | 9 +++++++++ includes/media/Bitmap.php | 23 +++++++++++++++++------ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/RELEASE-NOTES-1.32 b/RELEASE-NOTES-1.32 index 7971bccdf1..9eb751a049 100644 --- a/RELEASE-NOTES-1.32 +++ b/RELEASE-NOTES-1.32 @@ -9,6 +9,8 @@ production. * (T115414) The $wgEnableAPI and $wgEnableWriteAPI settings, deprecated in 1.31, have been removed. * The $wgUseAjax setting is now formally deprecated, and MediaWiki will act as if it is always set. * The $wgSiteSupportPage setting, unused since 1.5, was removed. +* $wgJpegQuality was added to allow configuring the quality of JPEG thumbnails (default 80). +* The default quality of JPEG thumbnails generated by GD was reduced from 95 to 80. * … === New features in 1.32 === diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 1ea9a420d8..975e20a463 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1085,6 +1085,15 @@ $wgJpegTran = '/usr/bin/jpegtran'; */ $wgJpegPixelFormat = 'yuv420'; +/** + * When scaling a JPEG thumbnail, this is the quality we request + * from the backend. It should be an int between 1 and 100, + * with 100 indicating 100% quality. + * + * @since 1.32 + */ +$wgJpegQuality = 80; + /** * Some tests and extensions use exiv2 to manipulate the Exif metadata in some * image formats. diff --git a/includes/media/Bitmap.php b/includes/media/Bitmap.php index b88a34dcad..cda037c16d 100644 --- a/includes/media/Bitmap.php +++ b/includes/media/Bitmap.php @@ -134,7 +134,8 @@ class BitmapHandler extends TransformationalImageHandler { protected function transformImageMagick( $image, $params ) { # use ImageMagick global $wgSharpenReductionThreshold, $wgSharpenParameter, $wgMaxAnimatedGifArea, - $wgImageMagickTempDir, $wgImageMagickConvertCommand, $wgJpegPixelFormat; + $wgImageMagickTempDir, $wgImageMagickConvertCommand, $wgJpegPixelFormat, + $wgJpegQuality; $quality = []; $sharpen = []; @@ -146,7 +147,7 @@ class BitmapHandler extends TransformationalImageHandler { if ( $params['mimeType'] == 'image/jpeg' ) { $qualityVal = isset( $params['quality'] ) ? (string)$params['quality'] : null; - $quality = [ '-quality', $qualityVal ?: '80' ]; // 80% + $quality = [ '-quality', $qualityVal ?: (string)$wgJpegQuality ]; // 80% by default if ( $params['interlace'] ) { $animation_post = [ '-interlace', 'JPEG' ]; } @@ -276,7 +277,7 @@ class BitmapHandler extends TransformationalImageHandler { */ protected function transformImageMagickExt( $image, $params ) { global $wgSharpenReductionThreshold, $wgSharpenParameter, $wgMaxAnimatedGifArea, - $wgJpegPixelFormat; + $wgJpegPixelFormat, $wgJpegQuality; try { $im = new Imagick(); @@ -293,7 +294,7 @@ class BitmapHandler extends TransformationalImageHandler { $im->sharpenImage( $radius, $sigma ); } $qualityVal = isset( $params['quality'] ) ? (string)$params['quality'] : null; - $im->setCompressionQuality( $qualityVal ?: 80 ); + $im->setCompressionQuality( $qualityVal ?: $wgJpegQuality ); if ( $params['interlace'] ) { $im->setInterlaceScheme( Imagick::INTERLACE_JPEG ); } @@ -498,9 +499,19 @@ class BitmapHandler extends TransformationalImageHandler { /** * Callback for transformGd when transforming jpeg images. + * + * @param resource $dst_image Image resource of the original image + * @param string $thumbPath File path to write the thumbnail image to + * @param int|null $quality Quality of the thumbnail from 1-100, + * or null to use default quality. */ - // FIXME: transformImageMagick() & transformImageMagickExt() uses JPEG quality 80, here it's 95? - static function imageJpegWrapper( $dst_image, $thumbPath, $quality = 95 ) { + static function imageJpegWrapper( $dst_image, $thumbPath, $quality = null ) { + global $wgJpegQuality; + + if ( $quality === null ) { + $quality = $wgJpegQuality; + } + imageinterlace( $dst_image ); imagejpeg( $dst_image, $thumbPath, $quality ); } -- 2.20.1