Skip to content

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

DEV: Use libvips for PNG conversion and JPEG recompression (#43463)

Alan Guo Xiang Tand7b2dd003389
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rb
index b864bcdc3e3..74fb53eb3c3 100644
--- a/lib/discourse_vips.rb
+++ b/lib/discourse_vips.rb
@@ -37,10 +37,26 @@ module DiscourseVips
Client.call(["animated", input_path], operation: :upload_animation_probe, timeout:)
end
- def self.heif_to_jpeg(input_path:, output_path:, timeout:)
+ def self.heif_to_jpeg(input_path:, output_path:, quality:, timeout:)
Client.call(
- ["heif-to-jpeg", input_path, output_path],
- operation: :upload_format_conversion,
+ ["heif-to-jpeg", input_path, output_path, quality],
+ operation: :upload_heif_to_jpeg,
+ timeout:,
+ )
+ end
+
+ def self.recompress_jpeg(input_path:, output_path:, quality:, timeout:)
+ Client.call(
+ ["recompress-jpeg", input_path, output_path, quality],
+ operation: :upload_jpeg_recompression,
+ timeout:,
+ )
+ end
+
+ def self.png_to_jpeg(input_path:, output_path:, quality:, timeout:)
+ Client.call(
+ ["png-to-jpeg", input_path, output_path, quality],
+ operation: :upload_png_to_jpeg,
timeout:,
)
end
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index 9ca58821e2d..dae2096f672 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -359,6 +359,7 @@ class UploadCreator
DiscourseVips.heif_to_jpeg(
input_path: from,
output_path: to,
+ quality: SiteSetting.image_quality,
timeout: MAX_CONVERT_FORMAT_SECONDS,
)
else
@@ -620,16 +621,20 @@ class UploadCreator
end
def convert_png_to_jpeg!
- replace_with_jpeg_if_sufficiently_smaller!(quality: SiteSetting.ImageQuality.png_to_jpg_quality)
+ replace_with_jpeg_if_sufficiently_smaller!(
+ quality: SiteSetting.ImageQuality.png_to_jpg_quality,
+ vips_method: :png_to_jpeg,
+ )
end
def recompress_jpeg!
replace_with_jpeg_if_sufficiently_smaller!(
quality: SiteSetting.ImageQuality.recompress_original_jpg_quality,
+ vips_method: :recompress_jpeg,
)
end
- def replace_with_jpeg_if_sufficiently_smaller!(quality:)
+ def replace_with_jpeg_if_sufficiently_smaller!(quality:, vips_method:)
return if @opts[:type] == "topic_og_image"
return if @opts[:for_site_setting] || ADMIN_ASSET_TYPES.include?(@opts[:type])
return if filesize < MIN_CONVERT_TO_JPEG_BYTES_SAVED
@@ -641,19 +646,27 @@ class UploadCreator
OptimizedImage.ensure_safe_paths!(from, to)
- from = OptimizedImage.prepend_decoder!(from, nil, filename: "image.#{@image_info.type}")
- to = OptimizedImage.prepend_decoder!(to)
-
- opts = { quality: }
-
- read = [@file.path]
- write = [File.dirname(jpeg_tempfile.path)]
+ if GlobalSetting.enable_vips_image_processing
+ DiscourseVips.public_send(
+ vips_method,
+ input_path: from,
+ output_path: to,
+ quality:,
+ timeout: MAX_CONVERT_FORMAT_SECONDS,
+ )
+ else
+ from = OptimizedImage.prepend_decoder!(from, nil, filename: "image.#{@image_info.type}")
+ to = OptimizedImage.prepend_decoder!(to)
+ opts = { quality: }
+ read = [@file.path]
+ write = [File.dirname(jpeg_tempfile.path)]
- begin
- execute_convert(from, to, opts, read:, write:)
- rescue StandardError
- # retry with debugging enabled
- execute_convert(from, to, opts.merge(debug: true), read:, write:)
+ begin
+ execute_convert(from, to, opts, read:, write:)
+ rescue StandardError
+ # retry with debugging enabled
+ execute_convert(from, to, opts.merge(debug: true), read:, write:)
+ end
end
new_size = File.size(jpeg_tempfile.path)
@@ -665,9 +678,9 @@ class UploadCreator
@file.respond_to?(:close!) ? @file.close! : @file.close
@file = jpeg_tempfile
extract_image_info!
- else
- jpeg_tempfile.close!
end
+ ensure
+ jpeg_tempfile&.close! unless @file.equal?(jpeg_tempfile)
end
def animated?
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
index 7f2b9a9cccb..e3834f6b91a 100644
--- a/script/discourse_vips_worker
+++ b/script/discourse_vips_worker
@@ -51,10 +51,14 @@ class DiscourseVipsWorker
dominant_color(*arguments)
when "animated"
animated(*arguments)
- when "heif-to-jpeg"
- heif_to_jpeg(input_path: arguments[0], output_path: arguments[1])
when "svg-dimensions"
svg_dimensions(*arguments)
+ when "heif-to-jpeg"
+ 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])
else
raise ArgumentError, "unsupported libvips operation"
end
@@ -162,29 +166,67 @@ class DiscourseVipsWorker
end
private_class_method :animated
- def self.heif_to_jpeg(input_path:, output_path:)
+ def self.heif_to_jpeg(input_path:, output_path:, quality:)
new(read_paths: [input_path], write_paths: [File.dirname(output_path)]) do
- if File.identical?(input_path, output_path)
- raise ArgumentError, "input and output must be different files"
+ block_loaders(allowed: %w[VipsForeignLoadHeif])
+
+ begin
+ save_jpeg(
+ Vips::Image.heifload(input_path, access: :sequential),
+ input_path:,
+ output_path:,
+ quality:,
+ )
+ rescue Vips::Error => error
+ raise InvalidImage, error.message
end
+ end
+ end
+ private_class_method :heif_to_jpeg
- block_loaders(allowed: %w[VipsForeignLoadHeif])
+ def self.png_to_jpeg(input_path:, output_path:, quality:)
+ new(read_paths: [input_path], write_paths: [File.dirname(output_path)]) do
+ block_loaders(allowed: %w[VipsForeignLoadPng])
begin
- image = Vips::Image.heifload(input_path, access: :sequential)
- if image.has_alpha?
- white = image.interpretation == :rgb16 ? 65_535 : 255
- image = image.flatten(background: [white, white, white])
- end
- image.jpegsave(output_path, Q: 93, interlace: false, optimize_coding: true)
+ save_jpeg(Vips::Image.pngload(input_path), input_path:, output_path:, quality:)
rescue Vips::Error => error
raise InvalidImage, error.message
end
+ end
+ end
+ private_class_method :png_to_jpeg
- nil
+ def self.recompress_jpeg(input_path:, output_path:, quality:)
+ new(read_paths: [input_path], write_paths: [File.dirname(output_path)]) do
+ block_loaders(allowed: %w[VipsForeignLoadJpeg])
+
+ begin
+ save_jpeg(Vips::Image.jpegload(input_path), input_path:, output_path:, quality:)
+ rescue Vips::Error => error
+ raise InvalidImage, error.message
+ end
end
end
- private_class_method :heif_to_jpeg
+ private_class_method :recompress_jpeg
+
+ def self.save_jpeg(image, input_path:, output_path:, quality:)
+ if File.expand_path(input_path) == File.expand_path(output_path) ||
+ (File.exist?(output_path) && File.identical?(input_path, output_path))
+ raise ArgumentError, "JPEG conversion requires separate input and output files"
+ end
+
+ image.autorot.jpegsave(
+ output_path,
+ background: [255, 255, 255],
+ Q: Integer(quality),
+ interlace: false,
+ # Optimized Huffman coding reduces JPEG size without changing decoded pixels.
+ optimize_coding: true,
+ )
+ nil
+ end
+ private_class_method :save_jpeg
def self.svg_dimensions(input_path)
new(read_paths: [input_path], rlimits: SVG_DIMENSIONS_RLIMITS) do
diff --git a/spec/lib/discourse_vips_spec.rb b/spec/lib/discourse_vips_spec.rb
index 06ce125fe6c..32ba553ec38 100644
--- a/spec/lib/discourse_vips_spec.rb
+++ b/spec/lib/discourse_vips_spec.rb
@@ -1,6 +1,31 @@
# frozen_string_literal: true
RSpec.describe DiscourseVips do
+ shared_examples "JPEG operation instrumentation" do |method, filename, operation|
+ it "records the specific image-processing operation" do
+ SiteSetting.instrument_image_processing = true
+ input_path = file_from_fixtures(filename).path
+
+ Dir.mktmpdir do |directory|
+ events =
+ DiscourseEvent.track_events(:image_processing_finished) do
+ described_class.public_send(
+ method,
+ input_path:,
+ output_path: File.join(directory, "output.jpg"),
+ quality: SiteSetting.image_quality,
+ timeout: 20,
+ )
+ end
+
+ expect(events.first[:params].first.except(:duration_seconds)).to eq(
+ operation:,
+ success: true,
+ )
+ end
+ end
+ end
+
describe ".version" do
it "returns the libvips version" do
expect(described_class.version).to match(/\A\d+\.\d+\.\d+\z/)
@@ -83,6 +108,11 @@ RSpec.describe DiscourseVips do
end
describe ".heif_to_jpeg" do
+ include_examples "JPEG operation instrumentation",
+ :heif_to_jpeg,
+ "should_be_jpeg.heic",
+ "upload_heif_to_jpeg"
+
shared_examples "HEIF conversion" do |filename|
it "converts #{filename} to JPEG without changing the source" do
input_path = file_from_fixtures(filename).path
@@ -91,7 +121,12 @@ RSpec.describe DiscourseVips do
Dir.mktmpdir do |directory|
output_path = File.join(directory, "converted.jpg")
- described_class.heif_to_jpeg(input_path:, output_path:, timeout: 20)
+ described_class.heif_to_jpeg(
+ input_path:,
+ output_path:,
+ quality: SiteSetting.image_quality,
+ timeout: 20,
+ )
expect(FastImage.type(output_path)).to eq(:jpeg)
expect(FastImage.size(output_path)).to eq([60, 40])
@@ -115,7 +150,12 @@ RSpec.describe DiscourseVips do
Dir.mktmpdir do |directory|
output_path = File.join(directory, "converted.jpg")
- described_class.heif_to_jpeg(input_path:, output_path:, timeout: 20)
+ described_class.heif_to_jpeg(
+ input_path:,
+ output_path:,
+ quality: SiteSetting.image_quality,
+ timeout: 20,
+ )
expect(FastImage.type(output_path)).to eq(:jpeg)
expect(FastImage.size(output_path)).to eq([846, 1129])
@@ -123,6 +163,30 @@ RSpec.describe DiscourseVips do
end
end
+ it "encodes HEIF inputs at the requested quality" do
+ input_path = file_from_fixtures("should_be_jpeg.heic").path
+
+ Dir.mktmpdir do |directory|
+ lower_quality_path = File.join(directory, "lower-quality.jpg")
+ higher_quality_path = File.join(directory, "higher-quality.jpg")
+
+ described_class.heif_to_jpeg(
+ input_path:,
+ output_path: lower_quality_path,
+ quality: 40,
+ timeout: 20,
+ )
+ described_class.heif_to_jpeg(
+ input_path:,
+ output_path: higher_quality_path,
+ quality: 95,
+ timeout: 20,
+ )
+
+ expect(File.size(lower_quality_path)).to be < File.size(higher_quality_path)
+ end
+ end
+
it "rejects a different image format without changing the source" do
input_path = file_from_fixtures("logo.png").path
original_content = File.binread(input_path)
@@ -131,7 +195,12 @@ RSpec.describe DiscourseVips do
output_path = File.join(directory, "converted.jpg")
expect {
- described_class.heif_to_jpeg(input_path:, output_path:, timeout: 20)
+ described_class.heif_to_jpeg(
+ input_path:,
+ output_path:,
+ quality: SiteSetting.image_quality,
+ timeout: 20,
+ )
}.to raise_error(DiscourseVips::InvalidImage)
expect(File.binread(input_path)).to eq(original_content)
@@ -147,8 +216,16 @@ RSpec.describe DiscourseVips do
File.binwrite(input_path, original_content)
expect {
- described_class.heif_to_jpeg(input_path:, output_path: input_path, timeout: 20)
- }.to raise_error(DiscourseVips::Error, "input and output must be different files")
+ described_class.heif_to_jpeg(
+ input_path:,
+ output_path: input_path,
+ quality: SiteSetting.image_quality,
+ timeout: 20,
+ )
+ }.to raise_error(
+ DiscourseVips::Error,
+ "JPEG conversion requires separate input and output files",
+ )
expect(File.binread(input_path)).to eq(original_content)
end
@@ -162,7 +239,12 @@ RSpec.describe DiscourseVips do
File.open(input_path, File::RDWR) do
expect {
- described_class.heif_to_jpeg(input_path:, output_path:, timeout: 0.05)
+ described_class.heif_to_jpeg(
+ input_path:,
+ output_path:,
+ quality: SiteSetting.image_quality,
+ timeout: 0.05,
+ )
}.to raise_error(DiscourseVips::OperationTimeout)
end
@@ -296,6 +378,90 @@ RSpec.describe DiscourseVips do
end
end
+ describe ".png_to_jpeg" do
+ include_examples "JPEG operation instrumentation",
+ :png_to_jpeg,
+ "logo.png",
+ "upload_png_to_jpeg"
+
+ it "flattens transparent PNG pixels onto white" do
+ Dir.mktmpdir do |directory|
+ output_path = File.join(directory, "converted.jpg")
+
+ described_class.png_to_jpeg(
+ input_path: file_from_fixtures("dominant-color-transparent.png").path,
+ output_path:,
+ quality: SiteSetting.ImageQuality.png_to_jpg_quality,
+ timeout: 5,
+ )
+
+ expect(FastImage.type(output_path)).to eq(:jpeg)
+ expect(described_class.dominant_color(input_path: output_path, timeout: 5)).to eq("FFFFFF")
+ end
+ end
+
+ it "rejects non-PNG input" do
+ Dir.mktmpdir do |directory|
+ output_path = File.join(directory, "converted.jpg")
+
+ expect {
+ described_class.png_to_jpeg(
+ input_path: file_from_fixtures("logo.jpg").path,
+ output_path:,
+ quality: SiteSetting.ImageQuality.png_to_jpg_quality,
+ timeout: 5,
+ )
+ }.to raise_error(DiscourseVips::InvalidImage)
+ expect(File.exist?(output_path)).to eq(false)
+ end
+ end
+ end
+
+ describe ".recompress_jpeg" do
+ include_examples "JPEG operation instrumentation",
+ :recompress_jpeg,
+ "logo.jpg",
+ "upload_jpeg_recompression"
+
+ 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(input_path:, output_path:, quality: 40, timeout: 5)
+
+ 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(
+ input_path:,
+ output_path: higher_quality_path,
+ quality: 95,
+ timeout: 5,
+ )
+ expect(File.size(output_path)).to be < File.size(higher_quality_path)
+ 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)
+
+ expect {
+ described_class.recompress_jpeg(
+ input_path:,
+ output_path: input_path,
+ quality: 40,
+ timeout: 5,
+ )
+ }.to raise_error(DiscourseVips::Error, /separate input and output/)
+ expect(File.binread(input_path)).to eq(original)
+ end
+ end
+ end
+
describe "worker lifecycle" do
it "recovers after the worker exits unexpectedly" do
described_class.version
diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb
index 8a9db1265c4..29ccb56b327 100644
--- a/spec/lib/upload_creator_spec.rb
+++ b/spec/lib/upload_creator_spec.rb
@@ -21,13 +21,13 @@ RSpec.describe UploadCreator do
end
end
- context "with libvips disabled" do
+ context "when animation detection uses ImageMagick" do
before { global_setting :enable_vips_image_processing, false }
include_examples "animated upload preservation"
end
- context "with libvips enabled" do
+ context "when animation detection uses libvips" do
before { global_setting :enable_vips_image_processing, true }
include_examples "animated upload preservation"
@@ -357,7 +357,7 @@ RSpec.describe UploadCreator do
end
end
- describe "converting to jpeg" do
+ shared_examples "JPEG upload conversion" do |enable_vips|
def image_quality(path)
local_path = File.join(Rails.root, "public", path)
Discourse::Utils.execute_command("identify", "-ping", "-format", "%Q", local_path).to_i
@@ -460,14 +460,47 @@ RSpec.describe UploadCreator do
upload =
UploadCreator.new(jpeg_file, "logo.jpg", force_optimize: true).create_for(user.id)
- expect(image_quality(upload.url)).to eq(SiteSetting.recompress_original_jpg_quality)
+ output_path = Discourse.store.path_for(upload)
+ expect(FastImage.type(output_path)).to eq(:jpeg)
+ expect(FastImage.size(output_path)).to eq(FastImage.size(jpeg_file.path))
+
+ if enable_vips
+ SiteSetting.recompress_original_jpg_quality = 70
+ higher_quality_file = file_from_fixtures("logo.jpg")
+ File.truncate(
+ higher_quality_file.path,
+ UploadCreator::MIN_CONVERT_TO_JPEG_BYTES_SAVED + higher_quality_file.size,
+ )
+ higher_quality_upload =
+ UploadCreator.new(higher_quality_file, "logo.jpg", force_optimize: true).create_for(
+ user.id,
+ )
+
+ expect(higher_quality_upload).to be_persisted
+ expect(FastImage.type(Discourse.store.path_for(higher_quality_upload))).to eq(:jpeg)
+ expect(upload.filesize).to be < higher_quality_upload.filesize
+ else
+ expect(image_quality(upload.url)).to eq(SiteSetting.recompress_original_jpg_quality)
+ end
upload.create_thumbnail!(100, 100)
upload.reload
- expect(image_quality(upload.optimized_images.first.url)).to eq(
- SiteSetting.image_preview_jpg_quality,
- )
+ if enable_vips
+ preview = upload.optimized_images.first
+ lower_quality_filesize = preview.filesize
+ preview.destroy!
+ SiteSetting.image_preview_jpg_quality = 90
+
+ upload.create_thumbnail!(100, 100)
+ upload.reload
+
+ expect(upload.optimized_images.first.filesize).to be > lower_quality_filesize
+ else
+ expect(image_quality(upload.optimized_images.first.url)).to eq(
+ SiteSetting.image_preview_jpg_quality,
+ )
+ end
end
it "does not convert animated images" do
@@ -547,6 +580,18 @@ RSpec.describe UploadCreator do
end
end
+ context "with libvips disabled" do
+ before { global_setting :enable_vips_image_processing, false }
+
+ include_examples "JPEG upload conversion", false
+ end
+
+ context "with libvips enabled" do
+ before { global_setting :enable_vips_image_processing, true }
+
+ include_examples "JPEG upload conversion", true
+ end
+
describe "converting HEIF to jpeg" do
let(:filename) { "should_be_jpeg.heic" }
let(:file) { file_from_fixtures(filename, "images") }