FIX: should_alter_quality should respect png_to_jpg_quality (#12055)

`convert_to_jpeg!` is only called if `convert_png_to_jpeg?` and/or `should_alter_quality?` is true.

`convert_png_to_jpeg?` can be disabled by setting `SiteSetting.png_to_jpg_quality` to 100.

However, `should_alter_quality?` could be true if `SiteSetting.recompress_original_jpg_quality` was lower than the quality of the uploaded file, regardless of file type.

This commits changes `should_alter_quality?` so that uploaded png files will use the `SiteSetting.png_to_jpg_quality` value, rather than ``SiteSetting.recompress_original_jpg_quality` value.
This commit is contained in:
jbrw 2021-02-12 13:37:35 -05:00 committed by GitHub
parent 5d8673321f
commit 70050a8ba3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View File

@ -276,7 +276,8 @@ class UploadCreator
def should_alter_quality? def should_alter_quality?
return false if animated? return false if animated?
@upload.target_image_quality(@file.path, SiteSetting.recompress_original_jpg_quality).present? desired_quality = @image_info.type == :png ? SiteSetting.png_to_jpg_quality : SiteSetting.recompress_original_jpg_quality
@upload.target_image_quality(@file.path, desired_quality).present?
end end
def should_downsize? def should_downsize?

View File

Before

Width:  |  Height:  |  Size: 412 KiB

After

Width:  |  Height:  |  Size: 412 KiB

View File

@ -134,6 +134,9 @@ RSpec.describe UploadCreator do
let(:small_filename) { "logo.png" } let(:small_filename) { "logo.png" }
let(:small_file) { file_from_fixtures(small_filename) } let(:small_file) { file_from_fixtures(small_filename) }
let(:large_filename) { "large_and_unoptimized.png" }
let(:large_file) { file_from_fixtures(large_filename) }
let(:animated_filename) { "animated.gif" } let(:animated_filename) { "animated.gif" }
let(:animated_file) { file_from_fixtures(animated_filename) } let(:animated_file) { file_from_fixtures(animated_filename) }
@ -212,6 +215,23 @@ RSpec.describe UploadCreator do
expect(upload.original_filename).to eq('animated.gif') expect(upload.original_filename).to eq('animated.gif')
end end
context "png image quality settings" do
before do
SiteSetting.png_to_jpg_quality = 100
SiteSetting.recompress_original_jpg_quality = 90
SiteSetting.image_preview_jpg_quality = 10
end
it "should not convert to jpeg when png_to_jpg_quality is 100" do
upload = UploadCreator.new(large_file, large_filename, force_optimize: true).create_for(user.id)
expect(upload.extension).to eq('png')
expect(File.extname(upload.url)).to eq('.png')
expect(upload.original_filename).to eq('large_and_unoptimized.png')
end
end
it 'should not convert animated WEBP images' do it 'should not convert animated WEBP images' do
expect do expect do
UploadCreator.new(animated_webp_file, animated_webp_filename, UploadCreator.new(animated_webp_file, animated_webp_filename,