Skip to content

A platform for community discussion. Free, open, simple.

FIX: should_alter_quality should respect png_to_jpg_quality (#12055)

jbrw70050a8ba365
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,