Skip to content

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

DEV: Add animated image support to `DiscourseVips` (#45482)

Steven Chang0e33b81d27a0
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rb
index 199897fd5d8..6ab102d5ced 100644
--- a/lib/discourse_vips.rb
+++ b/lib/discourse_vips.rb
@@ -132,7 +132,8 @@ module DiscourseVips
gravity: nil,
sharpen: false,
quality: nil,
- strip_metadata: false
+ strip_metadata: false,
+ animated: false
)
if [scale, width || height, max_pixels].compact.length != 1 || width.nil? != height.nil?
raise ArgumentError,
@@ -157,6 +158,7 @@ module DiscourseVips
gravity,
input_format,
output_format,
+ animated,
],
operation:,
read: [input_path],
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
index e84e9a270fc..f2fe9a00e44 100644
--- a/script/discourse_vips_worker
+++ b/script/discourse_vips_worker
@@ -87,6 +87,7 @@ class DiscourseVipsWorker
gravity: arguments[11],
input_format: arguments[12],
output_format: arguments[13],
+ animated: arguments[14],
)
else
raise ArgumentError, "unsupported libvips operation"
@@ -311,14 +312,21 @@ class DiscourseVipsWorker
strip_metadata:,
gravity:,
input_format:,
- output_format:
+ output_format:,
+ animated:
)
new do
raise ArgumentError, "invalid sharpening flag" if !%w[true false].include?(sharpen)
if !%w[true false].include?(strip_metadata)
raise ArgumentError, "invalid metadata stripping flag"
end
+ raise ArgumentError, "invalid animated flag" if !%w[true false].include?(animated)
+
quality = quality.empty? ? nil : Integer(quality)
+ animated = animated == "true"
+ if animated && !gravity.empty?
+ raise ArgumentError, "gravity is not supported for animated thumbnails"
+ end
loaders = {
"jpg" => "VipsForeignLoadJpeg",
@@ -338,11 +346,16 @@ class DiscourseVipsWorker
source =
begin
- Vips::Image.new_from_file(input_path)
+ Vips::Image.new_from_file(input_path, **(animated ? { n: -1 } : {}))
rescue Vips::Error => error
raise InvalidImage, error.message
end
+ source = source.autorot unless animated
+
+ source_height =
+ animated && source.get_typeof("n-pages") != 0 ? source.get("page-height") : source.height
+
if !width.empty? && !height.empty?
width = Integer(width)
height = Integer(height)
@@ -352,8 +365,7 @@ class DiscourseVipsWorker
if max_pixels
raise ArgumentError, "invalid resize scale" if !max_pixels.positive?
- source = source.autorot
- Math.sqrt(max_pixels.fdiv(source.width * source.height))
+ Math.sqrt(max_pixels.fdiv(source.width * source_height))
else
Float(scale)
end
@@ -361,22 +373,26 @@ class DiscourseVipsWorker
if max_pixels
width = (source.width * scale).floor.clamp(1, max_pixels)
- height = (source.height * scale).floor.clamp(1, max_pixels)
+ height = (source_height * scale).floor.clamp(1, max_pixels)
else
- width = [([source.width, source.height].max * scale).round, 1].max
- height = width
+ width = height = [([source.width, source_height].max * scale).round, 1].max
end
end
image =
- Vips::Image.thumbnail(
- input_path,
- width,
- height: height,
- size: size.to_sym,
- crop: crop.to_sym,
- output_profile: "srgb",
- )
+ begin
+ Vips::Image.thumbnail(
+ animated ? "#{input_path}[n=-1]" : input_path,
+ width,
+ height: height,
+ size: size.to_sym,
+ crop: crop.to_sym,
+ output_profile: "srgb",
+ )
+ rescue Vips::Error => error
+ raise InvalidImage, error.message
+ end
+
image = image.gravity(gravity.to_sym, width, height) if !gravity.empty?
image = image.sharpen if sharpen == "true"
options = { strip: strip_metadata == "true" }
diff --git a/spec/fixtures/images/animated-timing.gif b/spec/fixtures/images/animated-timing.gif
new file mode 100644
index 00000000000..37600a6293b
Binary files /dev/null and b/spec/fixtures/images/animated-timing.gif differ
diff --git a/spec/fixtures/images/animated-timing.webp b/spec/fixtures/images/animated-timing.webp
new file mode 100644
index 00000000000..762717d4cd6
Binary files /dev/null and b/spec/fixtures/images/animated-timing.webp differ
diff --git a/spec/lib/discourse_vips_spec.rb b/spec/lib/discourse_vips_spec.rb
index aced04e84dc..c5ba85b4e5c 100644
--- a/spec/lib/discourse_vips_spec.rb
+++ b/spec/lib/discourse_vips_spec.rb
@@ -898,5 +898,192 @@ RSpec.describe DiscourseVips do
)
}.to raise_error(DiscourseVips::Error, "invalid resize scale")
end
+
+ it "creates a static thumbnail from an animated GIF by default" do
+ output_path = File.join(directory, "output.webp")
+
+ described_class.thumbnail(
+ input_path: file_from_fixtures("animated.gif").path,
+ output_path:,
+ width: 100,
+ height: 100,
+ output_format: "webp",
+ timeout: 20,
+ operation: :upload_thumbnail,
+ )
+
+ expect(FastImage.type(output_path)).to eq(:webp)
+ expect(FastImage.size(output_path)).to eq([100, 100])
+ expect(FastImage.animated?(output_path)).to eq(false)
+ end
+
+ context "when animated: true" do
+ let(:animated_gif_path) { file_from_fixtures("animated.gif").path }
+ let(:timed_gif_path) { file_from_fixtures("animated-timing.gif").path }
+
+ def animation_metadata(path)
+ script = <<~RUBY
+ require "json"
+ require "vips"
+
+ image = Vips::Image.new_from_file(ARGV.fetch(0), n: -1)
+ puts JSON.generate(
+ frame_count: image.get("n-pages"),
+ frame_delays_ms: image.get("delay"),
+ loop_count: image.get("loop"),
+ )
+ RUBY
+
+ JSON.parse(
+ Discourse::Utils.execute_command(RbConfig.ruby, "-e", script, path, timeout: 10),
+ symbolize_names: true,
+ )
+ end
+
+ it "resizes an animated GIF while preserving its frames and timing" do
+ output_path = File.join(directory, "output.gif")
+
+ described_class.thumbnail(
+ input_path: timed_gif_path,
+ output_path:,
+ width: 100,
+ height: 50,
+ size: :force,
+ animated: true,
+ timeout: 20,
+ operation: :upload_animated_avatar,
+ )
+
+ expect(FastImage.size(output_path)).to eq([100, 50])
+ expect(animation_metadata(output_path)).to eq(
+ frame_count: 4,
+ frame_delays_ms: [100, 250, 150, 500],
+ loop_count: 3,
+ )
+ end
+
+ it "converts an animated GIF to WebP while preserving its frames and timing" do
+ output_path = File.join(directory, "output.webp")
+
+ described_class.thumbnail(
+ input_path: timed_gif_path,
+ output_path:,
+ width: 100,
+ height: 50,
+ size: :force,
+ animated: true,
+ output_format: "webp",
+ timeout: 20,
+ operation: :upload_animated_avatar,
+ )
+
+ expect(FastImage.type(output_path)).to eq(:webp)
+ expect(FastImage.size(output_path)).to eq([100, 50])
+ expect(animation_metadata(output_path)).to eq(
+ frame_count: 4,
+ frame_delays_ms: [100, 250, 150, 500],
+ loop_count: 3,
+ )
+ end
+
+ it "resizes an animated WebP while preserving its animation" do
+ output_path = File.join(directory, "output.webp")
+
+ described_class.thumbnail(
+ input_path: file_from_fixtures("animated-timing.webp").path,
+ output_path:,
+ width: 100,
+ height: 50,
+ animated: true,
+ timeout: 20,
+ operation: :upload_animated_avatar,
+ )
+
+ expect(FastImage.size(output_path)).to eq([100, 50])
+ expect(animation_metadata(output_path)).to eq(
+ frame_count: 4,
+ frame_delays_ms: [100, 250, 150, 500],
+ loop_count: 3,
+ )
+ end
+
+ it "rejects gravity for animated thumbnails without changing the destination" do
+ output_path = File.join(directory, "output.webp")
+ FileUtils.cp(file_from_fixtures("static.webp").path, output_path)
+ original_content = File.binread(output_path)
+
+ expect {
+ described_class.thumbnail(
+ input_path: timed_gif_path,
+ output_path:,
+ width: 100,
+ height: 50,
+ animated: true,
+ gravity: :north,
+ timeout: 20,
+ operation: :upload_animated_avatar,
+ )
+ }.to raise_error(DiscourseVips::Error, "gravity is not supported for animated thumbnails")
+
+ expect(File.binread(output_path)).to eq(original_content)
+ end
+
+ it "resizes each animated frame to fit the requested pixel limit" do
+ output_path = File.join(directory, "output.webp")
+
+ described_class.thumbnail(
+ input_path: animated_gif_path,
+ output_path:,
+ max_pixels: 4000,
+ animated: true,
+ timeout: 20,
+ operation: :upload_animated_avatar,
+ )
+
+ expect(FastImage.size(output_path)).to eq([63, 63])
+ expect(FastImage.animated?(output_path)).to eq(true)
+ end
+
+ it "produces a smaller animated WebP at lower quality" do
+ lower_quality_path = File.join(directory, "lower-quality.webp")
+ higher_quality_path = File.join(directory, "higher-quality.webp")
+
+ [10, 95].zip([lower_quality_path, higher_quality_path])
+ .each do |quality, output_path|
+ described_class.thumbnail(
+ input_path: animated_gif_path,
+ output_path:,
+ width: 100,
+ height: 100,
+ animated: true,
+ output_format: "webp",
+ quality:,
+ timeout: 20,
+ operation: :upload_animated_avatar,
+ )
+ end
+
+ expect(FastImage.animated?(lower_quality_path)).to eq(true)
+ expect(FastImage.animated?(higher_quality_path)).to eq(true)
+ expect(File.size(lower_quality_path)).to be < File.size(higher_quality_path)
+ end
+
+ it "scales each animated frame by the requested factor" do
+ output_path = File.join(directory, "output.webp")
+
+ described_class.thumbnail(
+ input_path: animated_gif_path,
+ output_path:,
+ scale: 0.5,
+ animated: true,
+ output_format: "webp",
+ timeout: 20,
+ operation: :upload_animated_avatar,
+ )
+
+ expect(FastImage.size(output_path)).to eq([160, 160])
+ expect(FastImage.animated?(output_path)).to eq(true)
+ end
+ end
end
end