From 70050a8ba365628394fbf7597daf75891328d991 Mon Sep 17 00:00:00 2001 From: jbrw Date: Fri, 12 Feb 2021 13:37:35 -0500 Subject: [PATCH] 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. --- lib/upload_creator.rb | 3 ++- ...ptimized.png => large_and_unoptimized.png} | Bin spec/lib/upload_creator_spec.rb | 20 ++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) rename spec/fixtures/images/{large & unoptimized.png => large_and_unoptimized.png} (100%) diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb index 98df85db338..06ea0606614 100644 --- a/lib/upload_creator.rb +++ b/lib/upload_creator.rb @@ -276,7 +276,8 @@ class UploadCreator def should_alter_quality? 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 def should_downsize? diff --git a/spec/fixtures/images/large & unoptimized.png b/spec/fixtures/images/large_and_unoptimized.png similarity index 100% rename from spec/fixtures/images/large & unoptimized.png rename to spec/fixtures/images/large_and_unoptimized.png diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb index 2de42a6cd24..8d5cee79520 100644 --- a/spec/lib/upload_creator_spec.rb +++ b/spec/lib/upload_creator_spec.rb @@ -134,6 +134,9 @@ RSpec.describe UploadCreator do let(:small_filename) { "logo.png" } 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_file) { file_from_fixtures(animated_filename) } @@ -212,6 +215,23 @@ RSpec.describe UploadCreator do expect(upload.original_filename).to eq('animated.gif') 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 expect do UploadCreator.new(animated_webp_file, animated_webp_filename,