Skip to content

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

DEV: Normalize JPEG upload orientation with libvips (#43464)

Alan Guo Xiang Tan8ef452cec6f9
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rb
index 4b3134cb658..79ac675a9b3 100644
--- a/lib/discourse_vips.rb
+++ b/lib/discourse_vips.rb
@@ -63,10 +63,10 @@ module DiscourseVips
)
end
- def self.recompress_jpeg(input_path:, output_path:, quality:, timeout:, read:, write:)
+ def self.reencode_jpeg(input_path:, output_path:, quality:, timeout:, read:, write:)
Client.call(
- ["recompress-jpeg", input_path, output_path, quality],
- operation: :upload_jpeg_recompression,
+ ["reencode-jpeg", input_path, output_path, quality],
+ operation: :upload_jpeg_reencoding,
timeout:,
read:,
write:,
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index 71eddb1a813..9c47991f513 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -119,7 +119,7 @@ class UploadCreator
when :png
convert_png_to_jpeg! if convert_png_to_jpeg?
when :jpeg
- recompress_jpeg! if should_alter_jpeg_quality?
+ reencode_jpeg! if should_alter_jpeg_quality?
end
fix_orientation! if should_fix_orientation?
crop! if should_crop?
@@ -472,9 +472,10 @@ class UploadCreator
end
def should_fix_orientation?
+ # FastImage only detects orientation for JPEG among supported image uploads.
# orientation is between 1 and 8, 1 being the default
# cf. http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/
- @image_info.orientation.to_i > 1
+ @image_info.type == :jpeg && @image_info.orientation.to_i > 1
end
MAX_FIX_ORIENTATION_TIME = 5
@@ -484,15 +485,26 @@ class UploadCreator
OptimizedImage.ensure_safe_paths!(path)
path = OptimizedImage.prepend_decoder!(path, nil, filename: "image.#{@image_info.type}")
- ImageMagick.magick(
- path,
- "-auto-orient",
- path,
- operation: :upload_auto_orient,
- read: [@file.path],
- write: [@file.path, File.dirname(@file.path)],
- timeout: MAX_FIX_ORIENTATION_TIME,
- )
+ if GlobalSetting.enable_vips_image_processing
+ DiscourseVips.reencode_jpeg(
+ input_path: @file.path,
+ output_path: @file.path,
+ quality: SiteSetting.ImageQuality.recompress_original_jpg_quality,
+ timeout: MAX_FIX_ORIENTATION_TIME,
+ read: [@file.path],
+ write: [@file.path],
+ )
+ else
+ ImageMagick.magick(
+ path,
+ "-auto-orient",
+ path,
+ operation: :upload_auto_orient,
+ read: [@file.path],
+ write: [@file.path, File.dirname(@file.path)],
+ timeout: MAX_FIX_ORIENTATION_TIME,
+ )
+ end
extract_image_info!
end
@@ -631,10 +643,10 @@ class UploadCreator
)
end
- def recompress_jpeg!
+ def reencode_jpeg!
replace_with_jpeg_if_sufficiently_smaller!(
quality: SiteSetting.ImageQuality.recompress_original_jpg_quality,
- vips_method: :recompress_jpeg,
+ vips_method: :reencode_jpeg,
)
end
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
index 38db55770e8..13800f62dae 100644
--- a/script/discourse_vips_worker
+++ b/script/discourse_vips_worker
@@ -1,9 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
+require "fileutils"
require "landlock"
require "msgpack"
require "socket"
+require "tempfile"
require "tmpdir"
require "vips"
@@ -57,8 +59,8 @@ class DiscourseVipsWorker
heif_to_jpeg(input_path: arguments[0], output_path: arguments[1], quality: arguments[2])
when "png-to-jpeg"
png_to_jpeg(input_path: arguments[0], output_path: arguments[1], quality: arguments[2])
- when "recompress-jpeg"
- recompress_jpeg(input_path: arguments[0], output_path: arguments[1], quality: arguments[2])
+ when "reencode-jpeg"
+ reencode_jpeg(input_path: arguments[0], output_path: arguments[1], quality: arguments[2])
else
raise ArgumentError, "unsupported libvips operation"
end
@@ -192,18 +194,25 @@ class DiscourseVipsWorker
end
private_class_method :png_to_jpeg
- def self.recompress_jpeg(input_path:, output_path:, quality:)
+ def self.reencode_jpeg(input_path:, output_path:, quality:)
new do
- block_loaders(allowed: %w[VipsForeignLoadJpeg])
+ quality = Integer(quality)
+ raise ArgumentError, "invalid JPEG quality" unless (1..100).cover?(quality)
+ block_loaders(allowed: %w[VipsForeignLoadJpeg])
begin
- save_jpeg(Vips::Image.jpegload(input_path), input_path:, output_path:, quality:)
+ image = Vips::Image.jpegload(input_path, access: :random)
+ Tempfile.create(%w[reencoded .jpg]) do |reencoded_file|
+ save_jpeg(image, input_path:, output_path: reencoded_file.path, quality:)
+ FileUtils.copy_file(reencoded_file.path, output_path)
+ end
+ nil
rescue Vips::Error => error
raise InvalidImage, error.message
end
end
end
- private_class_method :recompress_jpeg
+ private_class_method :reencode_jpeg
def self.save_jpeg(image, input_path:, output_path:, quality:)
if File.expand_path(input_path) == File.expand_path(output_path) ||
diff --git a/spec/lib/discourse_vips_spec.rb b/spec/lib/discourse_vips_spec.rb
index 011dfbebccf..644f884857a 100644
--- a/spec/lib/discourse_vips_spec.rb
+++ b/spec/lib/discourse_vips_spec.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true
+require "chunky_png"
+
RSpec.describe DiscourseVips do
shared_examples "JPEG operation instrumentation" do |method, filename, operation|
it "records the specific image-processing operation" do
@@ -441,18 +443,18 @@ RSpec.describe DiscourseVips do
end
end
- describe ".recompress_jpeg" do
+ describe ".reencode_jpeg" do
include_examples "JPEG operation instrumentation",
- :recompress_jpeg,
+ :reencode_jpeg,
"logo.jpg",
- "upload_jpeg_recompression"
+ "upload_jpeg_reencoding"
it "encodes JPEG inputs at the requested quality" do
Dir.mktmpdir do |directory|
output_path = File.join(directory, "converted.jpg")
input_path = file_from_fixtures("logo.jpg").path
- described_class.recompress_jpeg(
+ described_class.reencode_jpeg(
input_path:,
output_path:,
quality: 40,
@@ -464,7 +466,7 @@ RSpec.describe DiscourseVips do
expect(FastImage.type(output_path)).to eq(:jpeg)
expect(FastImage.size(output_path)).to eq(FastImage.size(input_path))
higher_quality_path = File.join(directory, "higher-quality.jpg")
- described_class.recompress_jpeg(
+ described_class.reencode_jpeg(
input_path:,
output_path: higher_quality_path,
quality: 95,
@@ -476,23 +478,26 @@ RSpec.describe DiscourseVips do
end
end
- it "preserves the input when the output refers to the same file" do
- Dir.mktmpdir do |directory|
- input_path = File.join(directory, "original.jpg")
- FileUtils.cp(file_from_fixtures("logo.jpg").path, input_path)
- original = File.binread(input_path)
+ context "when replacing the original JPEG" do
+ it "leaves the original JPEG unchanged when reencoding fails" do
+ Dir.mktmpdir do |directory|
+ input_path = File.join(directory, "original.jpg")
+ original = "invalid JPEG"
+ File.binwrite(input_path, original)
- expect {
- described_class.recompress_jpeg(
- input_path:,
- output_path: input_path,
- quality: 40,
- timeout: 5,
- read: [input_path],
- write: [File.dirname(input_path)],
- )
- }.to raise_error(DiscourseVips::Error, /separate input and output/)
- expect(File.binread(input_path)).to eq(original)
+ expect {
+ described_class.reencode_jpeg(
+ input_path:,
+ output_path: input_path,
+ quality: 95,
+ timeout: 5,
+ read: [input_path],
+ write: [input_path],
+ )
+ }.to raise_error(DiscourseVips::InvalidImage)
+ expect(File.binread(input_path)).to eq(original)
+ expect(Dir.children(directory)).to contain_exactly("original.jpg")
+ end
end
end
end
diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb
index 147e363f1e0..b622678e306 100644
--- a/spec/lib/upload_creator_spec.rb
+++ b/spec/lib/upload_creator_spec.rb
@@ -316,6 +316,29 @@ RSpec.describe UploadCreator do
end
end
end
+
+ it "stores upright JPEGs at the configured quality with libvips enabled" do
+ global_setting :enable_vips_image_processing, true
+ uploads = []
+
+ [50, 95].each do |quality|
+ SiteSetting.recompress_original_jpg_quality = quality
+ with_jpeg_orientation(source_path:, orientation: 6) do |oriented_file|
+ upload =
+ described_class.new(oriented_file, "oriented.jpg", force_optimize: true).create_for(
+ user.id,
+ )
+ expect(upload).to be_persisted
+ expect(upload).to have_attributes(width: 40, height: 60)
+ image_info = FastImage.new(Discourse.store.path_for(upload))
+ expect(image_info.size).to eq([40, 60])
+ expect(image_info.orientation).to eq(1)
+ uploads << upload
+ end
+ end
+
+ expect(uploads.first.filesize).to be < uploads.last.filesize
+ end
end
context "when a detected PNG cannot be decoded during conversion" do