Skip to content

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

DEV: Add support to downsize optimized images downsize with `DiscourseVips` (#43465)

Alan Guo Xiang Tan789d012d7fb6
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index 400888d45a9..bd34bfcf33f 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -7,6 +7,8 @@ class OptimizedImage < ActiveRecord::Base
# BUMP UP if optimized image algorithm changes
VERSION = 2
URL_REGEX = %r{(/optimized/\dX[/\.\w]*/([a-zA-Z0-9]+)[\.\w]*)}
+ MAX_PNGQUANT_SIZE = 500_000
+ MAX_CONVERT_SECONDS = 20
def self.lock(upload_id, width, height)
@hostname ||= Discourse.os_hostname
@@ -204,8 +206,10 @@ class OptimizedImage < ActiveRecord::Base
IM_DECODERS = /\A(jpe?g|png|gif|webp|avif|svg)\z/i
def self.prepend_decoder!(path, ext_path = nil, opts = nil)
- opts ||= {}
+ "#{image_extension(path: path, ext_path: ext_path, opts: opts || {})}:#{path}"
+ end
+ def self.image_extension(path:, ext_path:, opts:)
# This logic is a little messy but the result of using mocks for most
# of the image tests. The idea here is you shouldn't trust the "original"
# path of a file to figure out its extension. However, in certain cases
@@ -221,8 +225,9 @@ class OptimizedImage < ActiveRecord::Base
if !extension || !extension.match?(IM_DECODERS)
raise Discourse::InvalidAccess.new("Unsupported extension: #{extension}")
end
- "#{extension}:#{path}"
+ extension
end
+ private_class_method :image_extension
def self.thumbnail_or_resize
SiteSetting.strip_image_metadata ? "thumbnail" : "resize"
@@ -327,9 +332,49 @@ class OptimizedImage < ActiveRecord::Base
optimize(:optimized_image_crop, from, to, "#{width}x#{height}", opts)
end
- def self.downsize(from, to, dimensions, opts = {})
- optimize(:optimized_image_downsize, from, to, dimensions, opts)
+ def self.downsize(from:, to:, scale: nil, width: nil, height: nil, max_pixels: nil, **opts)
+ if GlobalSetting.enable_vips_image_processing
+ ensure_safe_paths!(from, to)
+ downsize_with_vips(
+ from: from,
+ to: to,
+ scale: scale,
+ width: width,
+ height: height,
+ max_pixels: max_pixels,
+ opts: opts,
+ )
+ else
+ dimensions =
+ if scale
+ "#{scale * 100}%"
+ elsif max_pixels
+ "#{max_pixels}@"
+ else
+ "#{width}x#{height}>"
+ end
+ optimize(:optimized_image_downsize, from, to, dimensions, opts)
+ end
+ end
+
+ def self.downsize_with_vips(from:, to:, scale:, width:, height:, max_pixels:, opts:)
+ DiscourseVips.downsize(
+ input_path: from,
+ output_path: to,
+ scale: scale,
+ width: width,
+ height: height,
+ max_pixels: max_pixels,
+ strip_metadata: SiteSetting.strip_image_metadata,
+ timeout: MAX_CONVERT_SECONDS,
+ read: [from],
+ write: [File.dirname(to)],
+ )
+ optimize_image(to: to)
+ rescue => error
+ handle_optimization_error(error: error, to: to, opts: opts)
end
+ private_class_method :downsize_with_vips
INSTRUCTION_METHODS = {
optimized_image_resize: :resize_instructions,
@@ -340,47 +385,50 @@ class OptimizedImage < ActiveRecord::Base
def self.optimize(operation, from, to, dimensions, opts = {})
instructions = public_send(INSTRUCTION_METHODS.fetch(operation), from, to, dimensions, opts)
- convert_with(instructions, from, to, opts, operation:)
+ begin
+ ImageMagick.magick(
+ *instructions,
+ operation: operation,
+ read: [from],
+ write: [File.dirname(to)],
+ nice: 10,
+ timeout: MAX_CONVERT_SECONDS,
+ )
+ optimize_image(to: to)
+ rescue => error
+ handle_optimization_error(error: error, to: to, opts: opts, instructions: instructions)
+ end
end
- MAX_PNGQUANT_SIZE = 500_000
- MAX_CONVERT_SECONDS = 20
-
- def self.convert_with(instructions, from, to, opts = {}, operation:)
- ImageMagick.magick(
- *instructions,
- operation:,
- read: [from],
- write: [File.dirname(to)],
- nice: 10,
- timeout: MAX_CONVERT_SECONDS,
- )
-
+ def self.optimize_image(to:)
allow_pngquant = to.downcase.ends_with?(".png") && File.size(to) < MAX_PNGQUANT_SIZE
FileHelper.optimize_image!(to, allow_pngquant: allow_pngquant)
true
- rescue => e
- if opts[:raise_on_error]
- raise e
- else
- error = +"Failed to optimize image:"
+ end
+ private_class_method :optimize_image
- if e.message =~ /\A(?:convert|magick):([^`]+)/
- error << $1
- else
- error << " unknown reason"
- end
+ def self.handle_optimization_error(error:, to:, opts:, instructions: nil)
+ raise error if opts[:raise_on_error]
- Discourse.warn(
- error,
- upload_id: opts[:upload_id],
- location: to,
- error_message: e.message,
- instructions: instructions,
- )
- false
+ message = +"Failed to optimize image:"
+ if error.message =~ /\A(?:convert|magick):([^`]+)/
+ message << $1
+ elsif error.is_a?(DiscourseVips::Error)
+ message << " #{error.message}"
+ else
+ message << " unknown reason"
end
+
+ Discourse.warn(
+ message,
+ upload_id: opts[:upload_id],
+ location: to,
+ error_message: error.message,
+ instructions: instructions,
+ )
+ false
end
+ private_class_method :handle_optimization_error
end
# == Schema Information
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rb
index 9c5e9651cae..2c50cbe3c97 100644
--- a/lib/discourse_vips.rb
+++ b/lib/discourse_vips.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require_relative "discourse_vips/client"
+require "tempfile"
module DiscourseVips
SVG_DIMENSIONS_TIMEOUT_SECONDS = 3
@@ -106,6 +107,55 @@ module DiscourseVips
)
end
+ def self.downsize(
+ input_path:,
+ output_path:,
+ timeout:,
+ read:,
+ write:,
+ scale: nil,
+ width: nil,
+ height: nil,
+ max_pixels: nil,
+ strip_metadata: false
+ )
+ if [scale, width || height, max_pixels].compact.length != 1 || width.nil? != height.nil?
+ raise ArgumentError,
+ "provide exactly one resize target: scale, width and height, or max_pixels"
+ end
+
+ format = File.extname(input_path).delete_prefix(".").downcase
+ raise ArgumentError, "unsupported format" if !%w[jpg jpeg png gif webp avif].include?(format)
+
+ output_mode =
+ File.exist?(output_path) ? File.stat(output_path).mode & 0o777 : 0o666 & ~File.umask
+
+ Tempfile.create(["downsize-", ".#{format}"], File.dirname(output_path)) do |output|
+ output.close
+ Client.call(
+ [
+ "downsize",
+ input_path,
+ output.path,
+ format,
+ scale,
+ width,
+ height,
+ max_pixels,
+ strip_metadata,
+ ],
+ operation: :optimized_image_downsize,
+ read:,
+ write:,
+ timeout:,
+ nice: 10,
+ )
+ File.chmod(output_mode, output.path)
+ File.rename(output.path, output_path)
+ end
+ nil
+ end
+
def self.before_fork
Client.before_fork
end
diff --git a/lib/shrink_uploaded_image.rb b/lib/shrink_uploaded_image.rb
index 042a9fcce3f..c6dd1cc6834 100644
--- a/lib/shrink_uploaded_image.rb
+++ b/lib/shrink_uploaded_image.rb
@@ -32,7 +32,12 @@ class ShrinkUploadedImage
return false
end
- OptimizedImage.downsize(path, path, "#{@max_pixels}@", filename: upload.original_filename)
+ OptimizedImage.downsize(
+ from: path,
+ to: path,
+ max_pixels: @max_pixels,
+ filename: upload.original_filename,
+ )
sha1 = Upload.generate_digest(path)
if sha1 == upload.sha1
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index 9c47991f513..6d06124170c 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -412,7 +412,13 @@ class UploadCreator
OptimizedImage.ensure_safe_paths!(from, to)
- OptimizedImage.downsize(from, to, "50%", scale_image: true, raise_on_error: true)
+ OptimizedImage.downsize(
+ from: from,
+ to: to,
+ scale: 0.5,
+ scale_image: true,
+ raise_on_error: true,
+ )
@file.respond_to?(:close!) ? @file.close! : @file.close
@file = down_tempfile
@@ -541,9 +547,10 @@ class UploadCreator
max_height: max_width,
)
OptimizedImage.downsize(
- @file.path,
- @file.path,
- "#{width}x#{height}\>",
+ from: @file.path,
+ to: @file.path,
+ width: width,
+ height: height,
filename: filename_with_correct_ext,
)
when "card_background"
@@ -556,16 +563,18 @@ class UploadCreator
max_height: max_width,
)
OptimizedImage.downsize(
- @file.path,
- @file.path,
- "#{width}x#{height}\>",
+ from: @file.path,
+ to: @file.path,
+ width: width,
+ height: height,
filename: filename_with_correct_ext,
)
when "custom_emoji"
OptimizedImage.downsize(
- @file.path,
- @file.path,
- "100x100\>",
+ from: @file.path,
+ to: @file.path,
+ width: 100,
+ height: 100,
filename: filename_with_correct_ext,
)
end
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
index 933a4aeb7e7..c7b39dea5dc 100644
--- a/script/discourse_vips_worker
+++ b/script/discourse_vips_worker
@@ -73,6 +73,18 @@ class DiscourseVipsWorker
reencode_jpeg(input_path: arguments[0], output_path: arguments[1], quality: arguments[2])
when "svg-to-png"
svg_to_png(input_path: arguments[0], output_path: arguments[1])
+
+ when "downsize"
+ downsize(
+ input_path: arguments[0],
+ output_path: arguments[1],
+ format: arguments[2],
+ scale: arguments[3],
+ width: arguments[4],
+ height: arguments[5],
+ max_pixels: arguments[6],
+ strip_metadata: arguments[7],
+ )
else
raise ArgumentError, "unsupported libvips operation"
end
@@ -279,6 +291,66 @@ class DiscourseVipsWorker
end
private_class_method :svg_to_png
+ def self.downsize(
+ input_path:,
+ output_path:,
+ format:,
+ scale:,
+ width:,
+ height:,
+ max_pixels:,
+ strip_metadata:
+ )
+ new do
+ begin
+ if !%w[true false].include?(strip_metadata)
+ raise ArgumentError, "invalid metadata stripping flag"
+ end
+ loader =
+ {
+ "jpg" => "VipsForeignLoadJpeg",
+ "jpeg" => "VipsForeignLoadJpeg",
+ "png" => "VipsForeignLoadPng",
+ "gif" => "VipsForeignLoadNsgif",
+ "webp" => "VipsForeignLoadWebp",
+ "avif" => "VipsForeignLoadHeif",
+ }.fetch(format) { raise ArgumentError, "unsupported input format" }
+ block_loaders(allowed: [loader])
+
+ if !width.empty? && !height.empty?
+ width = Integer(width)
+ height = Integer(height)
+ raise ArgumentError, "invalid resize bounds" if !width.positive? || !height.positive?
+
+ image =
+ Vips::Image.thumbnail(input_path, width, height:, size: :down, output_profile: "srgb")
+ else
+ source = Vips::Image.new_from_file(input_path)
+ scale =
+ if max_pixels.empty?
+ Float(scale)
+ else
+ Math.sqrt(Integer(max_pixels).fdiv(source.width * source.height))
+ end
+ raise ArgumentError, "invalid resize scale" if !scale.finite? || !scale.positive?
+
+ edge = [([source.width, source.height].max * scale).round, 1].max
+ image = Vips::Image.thumbnail(input_path, edge, output_profile: "srgb")
+ end
+
+ options = { strip: strip_metadata == "true" }
+ options[:interlace] = false if %w[.jpg .jpeg .png .gif].include?(
+ File.extname(output_path),
+ )
+ image.write_to_file(output_path, **options)
+ rescue Vips::Error => error
+ raise InvalidImage, error.message
+ end
+ nil
+ end
+ end
+ private_class_method :downsize
+
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/lib/discourse_vips/downsize_spec.rb b/spec/lib/discourse_vips/downsize_spec.rb
new file mode 100644
index 00000000000..9a2e5d8d379
--- /dev/null
+++ b/spec/lib/discourse_vips/downsize_spec.rb
@@ -0,0 +1,134 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseVips do
+ describe ".downsize" do
+ let(:directory) { Dir.mktmpdir }
+ let(:input_path) { File.join(directory, "source.png") }
+ let(:output_path) { File.join(directory, "output.png") }
+
+ after { FileUtils.remove_entry(directory) }
+
+ it "scales an image by the requested factor" do
+ FileUtils.cp(file_from_fixtures("logo.png").path, input_path)
+ output_path = File.join(directory, "output")
+
+ described_class.downsize(
+ input_path:,
+ output_path:,
+ scale: 0.5,
+ timeout: 20,
+ read: [input_path],
+ write: [directory],
+ )
+
+ expect(FastImage.size(output_path)).to eq([122, 33])
+ expect(FastImage.type(output_path)).to eq(:png)
+ end
+
+ it "rejects an image disguised as another format" do
+ input_path = file_from_fixtures("svg.png").path
+
+ expect {
+ described_class.downsize(
+ input_path:,
+ output_path:,
+ scale: 0.5,
+ timeout: 20,
+ read: [input_path],
+ write: [directory],
+ )
+ }.to raise_error(DiscourseVips::InvalidImage)
+
+ expect(File).not_to exist(output_path)
+ end
+
+ it "rejects an unsupported format" do
+ input_path = File.join(directory, "source.ico")
+
+ expect {
+ described_class.downsize(
+ input_path:,
+ output_path:,
+ scale: 0.5,
+ timeout: 20,
+ read: [input_path],
+ write: [directory],
+ )
+ }.to raise_error(ArgumentError, "unsupported format")
+ end
+
+ it "rejects conflicting resize targets" do
+ input_path = file_from_fixtures("logo.png").path
+
+ expect {
+ described_class.downsize(
+ input_path:,
+ output_path:,
+ scale: 0.5,
+ width: 100,
+ height: 100,
+ timeout: 20,
+ read: [input_path],
+ write: [directory],
+ )
+ }.to raise_error(
+ ArgumentError,
+ "provide exactly one resize target: scale, width and height, or max_pixels",
+ )
+ end
+
+ it "reports invalid bounds as an operation error" do
+ FileUtils.cp(file_from_fixtures("logo.png").path, input_path)
+
+ expect {
+ described_class.downsize(
+ input_path:,
+ output_path:,
+ width: 0,
+ height: 100,
+ timeout: 20,
+ read: [input_path],
+ write: [directory],
+ )
+ }.to raise_error(DiscourseVips::Error, "invalid resize bounds")
+ end
+
+ it "reports an invalid scale as an operation error" do
+ FileUtils.cp(file_from_fixtures("logo.png").path, input_path)
+
+ expect {
+ described_class.downsize(
+ input_path:,
+ output_path:,
+ scale: 0,
+ timeout: 20,
+ read: [input_path],
+ write: [directory],
+ )
+ }.to raise_error(DiscourseVips::Error, "invalid resize scale")
+ end
+
+ it "preserves the destination and removes temporary files when decoding fails" do
+ File.binwrite(input_path, "invalid image")
+ original_content = "original destination"
+ File.binwrite(output_path, original_content)
+
+ expect {
+ described_class.downsize(
+ input_path:,
+ output_path:,
+ scale: 0.5,
+ timeout: 20,
+ read: [input_path],
+ write: [directory],
+ )
+ }.to raise_error(DiscourseVips::InvalidImage)
+
+ expect(File.binread(output_path)).to eq(original_content)
+ expect(Dir.children(directory)).to contain_exactly(
+ File.basename(input_path),
+ File.basename(output_path),
+ )
+ end
+ end
+end
diff --git a/spec/lib/shrink_uploaded_image_spec.rb b/spec/lib/shrink_uploaded_image_spec.rb
index 1454d8f9589..ef900ec7b87 100644
--- a/spec/lib/shrink_uploaded_image_spec.rb
+++ b/spec/lib/shrink_uploaded_image_spec.rb
@@ -31,9 +31,9 @@ RSpec.describe ShrinkUploadedImage do
it "updates HotlinkedMedia records when there is an upload for downsized image" do
OptimizedImage.downsize(
- Discourse.store.path_for(upload),
- "/tmp/smaller.png",
- "10000@",
+ from: Discourse.store.path_for(upload),
+ to: "/tmp/smaller.png",
+ max_pixels: 10_000,
filename: upload.original_filename,
)
smaller_sha1 = Upload.generate_digest("/tmp/smaller.png")
diff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rb
index 40e6edf8d93..639ada9e50a 100644
--- a/spec/models/optimized_image_spec.rb
+++ b/spec/models/optimized_image_spec.rb
@@ -163,41 +163,64 @@ RSpec.describe OptimizedImage do
end
end
end
+ end
- describe ".downsize" do
- it "downsizes the logo with ImageMagick 7" do
- tmp_path = "/tmp/downsized.png"
+ describe ".downsize" do
+ let(:directory) { Dir.mktmpdir }
+ let(:input_path) { File.join(directory, "source.png") }
+ let(:output_path) { File.join(directory, "output.png") }
- begin
- OptimizedImage.downsize(
- "#{Rails.root.join("spec/fixtures/images/logo.png")}",
- tmp_path,
- "100x100\>",
- )
+ before { FileUtils.cp(file_from_fixtures("logo.png").path, input_path) }
- info = FastImage.new(tmp_path)
- expect(info.size).to eq([100, 27])
- expect(File.size(tmp_path)).to be < 2300
- ensure
- File.delete(tmp_path) if File.exist?(tmp_path)
- end
+ after { FileUtils.remove_entry(directory) }
+
+ shared_examples "downsizing" do
+ it "downsizes an image in place" do
+ result = described_class.downsize(from: input_path, to: input_path, scale: 0.5)
+
+ expect(result).to eq(true)
+ expect(FastImage.size(input_path)).to eq([122, 33])
end
+ end
- it "keeps a tiny image at its original dimensions when it is within the requested bounds" do
- FileHelper.stubs(:optimize_image!).returns(true)
+ context "with libvips disabled" do
+ before { global_setting :enable_vips_image_processing, false }
- Dir.mktmpdir do |dir|
- output_path = File.join(dir, "downsized.png")
+ include_examples "downsizing"
+ end
- OptimizedImage.downsize(
- "#{Rails.root.join("spec/fixtures/images/resized.png")}",
- output_path,
- "10x10\>",
- )
+ context "with libvips enabled" do
+ before { global_setting :enable_vips_image_processing, true }
- expect(FastImage.type(output_path)).to eq(:png)
- expect(FastImage.size(output_path)).to eq([5, 5])
- end
+ include_examples "downsizing"
+ end
+
+ context "when libvips processing fails" do
+ before do
+ global_setting :enable_vips_image_processing, true
+ File.binwrite(input_path, "invalid image")
+ end
+
+ it "logs the libvips error and returns false by default" do
+ logger =
+ track_log_messages do
+ result = described_class.downsize(from: input_path, to: output_path, scale: 0.5)
+
+ expect(result).to eq(false)
+ end
+
+ expect(logger.warnings.first).to match(/Failed to optimize image: VipsForeignLoad/)
+ end
+
+ it "raises the processing error when requested" do
+ expect {
+ described_class.downsize(
+ from: input_path,
+ to: output_path,
+ scale: 0.5,
+ raise_on_error: true,
+ )
+ }.to raise_error(DiscourseVips::InvalidImage)
end
end
end