Skip to content

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

DEV: Add libvips support for HEIF conversion (#43457)

Alan Guo Xiang Tan8c3619765f96
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rb
index 2827432a31a..7119d3b27bb 100644
--- a/lib/discourse_vips.rb
+++ b/lib/discourse_vips.rb
@@ -29,6 +29,14 @@ module DiscourseVips
Client.call(["animated", input_path], operation: :upload_animation_probe, timeout:)
end
+ def self.heif_to_jpeg(input_path:, output_path:, timeout:)
+ Client.call(
+ ["heif-to-jpeg", input_path, output_path],
+ operation: :upload_format_conversion,
+ timeout:,
+ )
+ end
+
def self.before_fork
Client.before_fork
end
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index c8dc259e813..438f1841f5f 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -424,16 +424,26 @@ class UploadCreator
read = [@file.path]
write = [File.dirname(jpeg_tempfile.path)]
- begin
- execute_convert(from, to, {}, read:, write:)
- rescue StandardError
- # retry with debugging enabled
- execute_convert(from, to, { debug: true }, read:, write:)
+ if GlobalSetting.enable_vips_image_processing
+ DiscourseVips.heif_to_jpeg(
+ input_path: from,
+ output_path: to,
+ timeout: MAX_CONVERT_FORMAT_SECONDS,
+ )
+ else
+ begin
+ execute_convert(from, to, {}, read:, write:)
+ rescue StandardError
+ # retry with debugging enabled
+ execute_convert(from, to, { debug: true }, read:, write:)
+ end
end
@file.respond_to?(:close!) ? @file.close! : @file.close
@file = jpeg_tempfile
extract_image_info!
+ ensure
+ jpeg_tempfile&.close! if jpeg_tempfile != @file
end
MAX_CONVERT_FORMAT_SECONDS = 20
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
index 2643b7faa79..cfa0bf65c65 100644
--- a/script/discourse_vips_worker
+++ b/script/discourse_vips_worker
@@ -48,6 +48,8 @@ class DiscourseVipsWorker
dominant_color(*arguments)
when "animated"
animated(*arguments)
+ when "heif-to-jpeg"
+ heif_to_jpeg(input_path: arguments[0], output_path: arguments[1])
else
raise ArgumentError, "unsupported libvips operation"
end
@@ -150,6 +152,30 @@ class DiscourseVipsWorker
end
private_class_method :animated
+ def self.heif_to_jpeg(input_path:, output_path:)
+ 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"
+ end
+
+ block_loaders(allowed: %w[VipsForeignLoadHeif])
+
+ 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)
+ rescue Vips::Error => error
+ raise InvalidImage, error.message
+ end
+
+ nil
+ end
+ end
+ private_class_method :heif_to_jpeg
+
def self.normalize_color(image)
image = image.colourspace(:srgb)
image = (image * 255).round(:rint) if %i[float double].include?(image.format)
diff --git a/spec/fixtures/images/heif-color-grid-12bit.heic b/spec/fixtures/images/heif-color-grid-12bit.heic
new file mode 100644
index 00000000000..03b3caa66f5
Binary files /dev/null and b/spec/fixtures/images/heif-color-grid-12bit.heic differ
diff --git a/spec/fixtures/images/heif-color-grid-8bit.heic b/spec/fixtures/images/heif-color-grid-8bit.heic
new file mode 100644
index 00000000000..c9e3c79e3c7
Binary files /dev/null and b/spec/fixtures/images/heif-color-grid-8bit.heic differ
diff --git a/spec/fixtures/images/heif-color-grid-alpha-12bit.heic b/spec/fixtures/images/heif-color-grid-alpha-12bit.heic
new file mode 100644
index 00000000000..fa38f46cf48
Binary files /dev/null and b/spec/fixtures/images/heif-color-grid-alpha-12bit.heic differ
diff --git a/spec/fixtures/images/heif-color-grid-alpha-8bit.heic b/spec/fixtures/images/heif-color-grid-alpha-8bit.heic
new file mode 100644
index 00000000000..ac22b97ccd2
Binary files /dev/null and b/spec/fixtures/images/heif-color-grid-alpha-8bit.heic differ
diff --git a/spec/fixtures/images/heif-color-grid-mirrored.heic b/spec/fixtures/images/heif-color-grid-mirrored.heic
new file mode 100644
index 00000000000..266f917c686
Binary files /dev/null and b/spec/fixtures/images/heif-color-grid-mirrored.heic differ
diff --git a/spec/fixtures/images/heif-color-grid-rotated.heic b/spec/fixtures/images/heif-color-grid-rotated.heic
new file mode 100644
index 00000000000..586b3ae503b
Binary files /dev/null and b/spec/fixtures/images/heif-color-grid-rotated.heic differ
diff --git a/spec/fixtures/images/heif-truncated-payload.heic b/spec/fixtures/images/heif-truncated-payload.heic
new file mode 100644
index 00000000000..6680ce53f54
Binary files /dev/null and b/spec/fixtures/images/heif-truncated-payload.heic differ
diff --git a/spec/lib/discourse_vips_spec.rb b/spec/lib/discourse_vips_spec.rb
index a0af5dd916b..c9da77a57bb 100644
--- a/spec/lib/discourse_vips_spec.rb
+++ b/spec/lib/discourse_vips_spec.rb
@@ -82,6 +82,95 @@ RSpec.describe DiscourseVips do
end
end
+ describe ".heif_to_jpeg" do
+ shared_examples "HEIF conversion" do |filename|
+ it "converts #{filename} to JPEG without changing the source" do
+ input_path = file_from_fixtures(filename).path
+ original_content = File.binread(input_path)
+
+ Dir.mktmpdir do |directory|
+ output_path = File.join(directory, "converted.jpg")
+
+ described_class.heif_to_jpeg(input_path:, output_path:, timeout: 20)
+
+ expect(FastImage.type(output_path)).to eq(:jpeg)
+ expect(FastImage.size(output_path)).to eq([60, 40])
+ expect(File.binread(input_path)).to eq(original_content)
+ end
+ end
+ end
+
+ context "with an opaque 12-bit HEIF" do
+ include_examples "HEIF conversion", "heif-color-grid-12bit.heic"
+ end
+
+ context "with a transparent 12-bit HEIF" do
+ include_examples "HEIF conversion", "heif-color-grid-alpha-12bit.heic"
+ end
+
+ it "preserves image dimensions without changing the source" do
+ input_path = file_from_fixtures("should_be_jpeg.heic").path
+ original_content = File.binread(input_path)
+
+ Dir.mktmpdir do |directory|
+ output_path = File.join(directory, "converted.jpg")
+
+ described_class.heif_to_jpeg(input_path:, output_path:, timeout: 20)
+
+ expect(FastImage.type(output_path)).to eq(:jpeg)
+ expect(FastImage.size(output_path)).to eq([846, 1129])
+ expect(File.binread(input_path)).to eq(original_content)
+ 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)
+
+ Dir.mktmpdir do |directory|
+ output_path = File.join(directory, "converted.jpg")
+
+ expect {
+ described_class.heif_to_jpeg(input_path:, output_path:, timeout: 20)
+ }.to raise_error(DiscourseVips::InvalidImage)
+
+ expect(File.binread(input_path)).to eq(original_content)
+ expect(File.exist?(output_path)).to eq(false)
+ end
+ end
+
+ it "rejects overwriting the source image" do
+ original_content = File.binread(file_from_fixtures("should_be_jpeg.heic").path)
+
+ Dir.mktmpdir do |directory|
+ input_path = File.join(directory, "source.heic")
+ 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")
+
+ expect(File.binread(input_path)).to eq(original_content)
+ end
+ end
+
+ it "stops when reading the source exceeds the timeout" do
+ Dir.mktmpdir do |directory|
+ input_path = File.join(directory, "blocked.heic")
+ output_path = File.join(directory, "converted.jpg")
+ File.mkfifo(input_path)
+
+ File.open(input_path, File::RDWR) do
+ expect {
+ described_class.heif_to_jpeg(input_path:, output_path:, timeout: 0.05)
+ }.to raise_error(DiscourseVips::OperationTimeout)
+ end
+
+ expect(File.exist?(output_path)).to eq(false)
+ 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 8edb3e17082..00ee4d5dba9 100644
--- a/spec/lib/upload_creator_spec.rb
+++ b/spec/lib/upload_creator_spec.rb
@@ -532,16 +532,77 @@ RSpec.describe UploadCreator do
let(:filename) { "should_be_jpeg.heic" }
let(:file) { file_from_fixtures(filename, "images") }
- it "stores the upload with the expected extension" do
- expect do
- UploadCreator.new(file, filename, force_optimize: true).create_for(user.id)
- end.to change { Upload.count }.by(1)
+ shared_examples "HEIF image conversion" do |grid_filename, width, height|
+ it "stores #{grid_filename} as a JPEG with the expected dimensions" do
+ upload =
+ described_class.new(
+ file_from_fixtures(grid_filename),
+ grid_filename,
+ force_optimize: true,
+ ).create_for(user.id)
- upload = Upload.last
+ expect(upload).to be_persisted
+ expect(upload).to have_attributes(extension: "jpeg", width:, height:)
+ stored_path = Discourse.store.path_for(upload)
+ expect(FastImage.type(stored_path)).to eq(:jpeg)
+ expect(FastImage.size(stored_path)).to eq([width, height])
+ end
+ end
- expect(upload.extension).to eq("jpeg")
- expect(File.extname(upload.url)).to eq(".jpeg")
- expect(upload.original_filename).to eq("should_be_jpeg.jpg")
+ shared_examples "HEIF upload conversion" do
+ it "stores a JPEG" do
+ upload = UploadCreator.new(file, filename, force_optimize: true).create_for(user.id)
+ stored_path = Discourse.store.path_for(upload)
+
+ expect(upload).to be_persisted
+ expect(upload).to have_attributes(
+ extension: "jpeg",
+ original_filename: "should_be_jpeg.jpg",
+ width: 846,
+ height: 1129,
+ )
+ expect(File.extname(upload.url)).to eq(".jpeg")
+ expect(FastImage.type(stored_path)).to eq(:jpeg)
+ expect(FastImage.size(stored_path)).to eq([846, 1129])
+ end
+
+ it "removes conversion tempfiles after an invalid HEIF" do
+ source_file = file_from_fixtures("heif-truncated-payload.heic")
+
+ Dir.mktmpdir do |directory|
+ Dir.stubs(:tmpdir).returns(directory)
+
+ expect {
+ described_class.new(source_file, "invalid.heic", force_optimize: true).create_for(
+ user.id,
+ )
+ }.to raise_error(error_class)
+
+ expect(Dir.children(directory)).to eq([])
+ expect(source_file).to be_closed
+ end
+ end
+
+ include_examples "HEIF image conversion", "heif-color-grid-rotated.heic", 40, 60
+ include_examples "HEIF image conversion", "heif-color-grid-mirrored.heic", 60, 40
+ include_examples "HEIF image conversion", "heif-color-grid-8bit.heic", 60, 40
+ include_examples "HEIF image conversion", "heif-color-grid-alpha-8bit.heic", 60, 40
+ end
+
+ context "with libvips disabled" do
+ before { global_setting :enable_vips_image_processing, false }
+
+ let(:error_class) { Discourse::Utils::CommandError }
+
+ include_examples "HEIF upload conversion"
+ end
+
+ context "with libvips enabled" do
+ before { global_setting :enable_vips_image_processing, true }
+
+ let(:error_class) { DiscourseVips::InvalidImage }
+
+ include_examples "HEIF upload conversion"
end
end