Skip to content

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

DEV: Skip ImageMagick quality estimation with libvips enabled (#43712)

Alan Guo Xiang Tan8a902ae7a269
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index ea33d19687e..400888d45a9 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -96,12 +96,18 @@ class OptimizedImage < ActiveRecord::Base
temp_file = Tempfile.new(["discourse-thumbnail", extension])
temp_path = temp_file.path
- target_quality =
- upload.target_jpeg_image_quality(
- original_path,
- SiteSetting.ImageQuality.image_preview_jpg_quality,
- )
- opts = opts.merge(quality: target_quality) if target_quality
+ if GlobalSetting.enable_vips_image_processing
+ if %w[.jpg .jpeg].include?(extension.downcase)
+ opts = opts.merge(quality: SiteSetting.ImageQuality.image_preview_jpg_quality)
+ end
+ else
+ target_quality =
+ upload.target_jpeg_image_quality(
+ original_path,
+ SiteSetting.ImageQuality.image_preview_jpg_quality,
+ )
+ opts = opts.merge(quality: target_quality) if target_quality
+ end
opts = opts.merge(upload_id: upload.id)
# special case, when "resizing" vectors we simply copy
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index dae2096f672..3186b0dc7d3 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -614,6 +614,8 @@ class UploadCreator
end
def should_alter_jpeg_quality?
+ return false if GlobalSetting.enable_vips_image_processing
+
@upload.target_jpeg_image_quality(
@file.path,
SiteSetting.ImageQuality.recompress_original_jpg_quality,
diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb
index 29ccb56b327..147e363f1e0 100644
--- a/spec/lib/upload_creator_spec.rb
+++ b/spec/lib/upload_creator_spec.rb
@@ -450,7 +450,8 @@ RSpec.describe UploadCreator do
SiteSetting.image_preview_jpg_quality = 10
end
- it "alters the JPEG image quality" do
+ it "stores the JPEG at the configured recompression quality when libvips processing is disabled" do
+ global_setting :enable_vips_image_processing, false
jpeg_file = file_from_fixtures("logo.jpg")
File.truncate(
jpeg_file.path,
diff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rb
index 3da4ce05651..40e6edf8d93 100644
--- a/spec/models/optimized_image_spec.rb
+++ b/spec/models/optimized_image_spec.rb
@@ -313,6 +313,27 @@ RSpec.describe OptimizedImage do
)
end
+ context "with libvips enabled" do
+ before { global_setting :enable_vips_image_processing, true }
+
+ it "creates JPEG previews at the configured quality" do
+ jpeg_upload =
+ UploadCreator.new(file_from_fixtures("logo.jpg"), "logo.jpg").create_for(
+ Discourse.system_user.id,
+ )
+
+ SiteSetting.image_preview_jpg_quality = 50
+ low_quality_preview = described_class.create_for(jpeg_upload, 100, 100)
+ low_quality_filesize = low_quality_preview.filesize
+ low_quality_preview.destroy
+
+ SiteSetting.image_preview_jpg_quality = 90
+ high_quality_preview = described_class.create_for(jpeg_upload, 100, 100)
+
+ expect(low_quality_filesize).to be < high_quality_preview.filesize
+ end
+ end
+
context "when using an internal store" do
let(:store) { FakeInternalStore.new }