Skip to content

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

FIX: Use configured quality for JPEG resizes (#43806)

Alan Guo Xiang Tan59bff9e170f2
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index 2fc44f4e953..d1605b326bc 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -325,6 +325,11 @@ class OptimizedImage < ActiveRecord::Base
end
def self.resize(from, to, width, height, opts = {})
+ if opts[:quality].nil? &&
+ image_extension(path: to, ext_path: to, opts: opts).match?(/\Ajpe?g\z/i)
+ opts = opts.merge(quality: SiteSetting.ImageQuality.image_preview_jpg_quality)
+ end
+
if GlobalSetting.enable_vips_image_processing
ensure_safe_paths!(from, to)
resize_with_vips(from: from, to: to, width: width, height: height, opts: opts)
diff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rb
index bb8377611d7..fcae9fc4889 100644
--- a/spec/models/optimized_image_spec.rb
+++ b/spec/models/optimized_image_spec.rb
@@ -195,6 +195,21 @@ RSpec.describe OptimizedImage do
before { global_setting :enable_vips_image_processing, true }
include_examples "resize processing"
+
+ it "uses the configured quality for JPEG output" do
+ low_quality_path = File.join(directory, "low-quality.jpg")
+ high_quality_path = File.join(directory, "high-quality.jpg")
+ FileUtils.cp(file_from_fixtures("logo.jpg").path, low_quality_path)
+ FileUtils.cp(file_from_fixtures("logo.jpg").path, high_quality_path)
+
+ SiteSetting.image_preview_jpg_quality = 50
+ described_class.resize(low_quality_path, low_quality_path, 100, 100)
+
+ SiteSetting.image_preview_jpg_quality = 90
+ described_class.resize(high_quality_path, high_quality_path, 100, 100)
+
+ expect(File.size(low_quality_path)).to be < File.size(high_quality_path)
+ end
end
end