Skip to content

Commit

Permalink
Add support for animated gifs
Browse files Browse the repository at this point in the history
ruby-vips and mini_magick need to be told to ignore
the page in order to resize animated gifs.
  • Loading branch information
tvdeyen committed Aug 2, 2023
1 parent 9e46ec4 commit b2d7d44
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 14 deletions.
32 changes: 32 additions & 0 deletions app/services/alchemy/dragonfly_to_image_processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class << self
def call(options = {})
opts = crop_options(options).presence || resize_options(options)
opts.merge!(format_options(options))
opts.merge!(flatten_options(options))
opts.merge!(quality_options(options))
opts
end
Expand Down Expand Up @@ -56,6 +57,33 @@ def format_options(options)
{format: format}
end

def flatten_options(options)
case options[:flatten]
when true
{loader: flattened_loader_options}
when false, nil
{loader: not_flattened_loader_options}
end
end

def flattened_loader_options
case variant_processor
when :vips
{n: 1}
when :mini_magick
{page: 0}
end
end

def not_flattened_loader_options
case variant_processor
when :vips
{n: -1}
when :mini_magick
{page: nil}
end
end

def image_magick_string(options)
if options[:crop] == true
"#{options[:size]}#"
Expand Down Expand Up @@ -95,6 +123,10 @@ def default_output_format

Alchemy::Config.get(:image_output_format)
end

def variant_processor
Rails.application.config.active_storage.variant_processor
end
end
end
end
3 changes: 2 additions & 1 deletion spec/models/alchemy/picture/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
expect(picture.image_file).to receive(:variant).with(
{
resize_to_limit: [10, 10, {sharpen: false}],
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
subject
Expand Down
143 changes: 130 additions & 13 deletions spec/services/alchemy/dragonfly_to_image_processing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
{
crop: [0, 0, 2000, 1000],
resize_to_limit: [200, 100, {sharpen: false}],
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -41,7 +42,8 @@
{
crop: [0, 0, 2000, 1000],
resize_to_limit: [200, 100, {}],
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -56,7 +58,8 @@
is_expected.to eq(
{
resize_to_limit: [100, 100, {sharpen: false}],
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -69,7 +72,8 @@
is_expected.to eq(
{
resize_to_fit: [100, 100, {sharpen: false}],
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -82,7 +86,8 @@
is_expected.to eq(
{
resize_to_fill: [100, 100, {sharpen: false}],
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -95,7 +100,8 @@
is_expected.to eq(
{
resize_to_limit: [100, 100, {sharpen: false}],
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -107,7 +113,8 @@
is_expected.to eq(
{
resize_to_fill: [100, 100, {sharpen: false}],
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -121,7 +128,8 @@
is_expected.to eq(
{
resize_to_limit: [100, 100, {}],
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -131,14 +139,20 @@
let(:options) { {} }

it "just contains default quality option" do
is_expected.to eq({saver: {quality: 85}})
is_expected.to eq({
saver: {quality: 85},
loader: {n: -1}
})
end

context "if quality is given" do
let(:options) { {quality: 15} }

it "contains given quality option" do
is_expected.to eq({saver: {quality: 15}})
is_expected.to eq({
saver: {quality: 15},
loader: {n: -1}
})
end
end
end
Expand All @@ -150,7 +164,8 @@
is_expected.to eq(
{
format: "webp",
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -167,7 +182,8 @@
it "does not contain the format option" do
is_expected.to eq(
{
saver: {quality: 85}
saver: {quality: 85},
loader: {n: -1}
}
)
end
Expand All @@ -182,11 +198,112 @@
is_expected.to eq(
{
saver: {quality: 85},
format: "webp"
format: "webp",
loader: {n: -1}
}
)
end
end
end
end

describe "flatten option" do
shared_context "vips variant processor" do
before do
expect(Rails.application.config.active_storage).to receive(:variant_processor) do
:vips
end
end
end

shared_context "mini_magick variant processor" do
before do
expect(Rails.application.config.active_storage).to receive(:variant_processor) do
:mini_magick
end
end
end

context "with flatten not set" do
let(:options) { {format: "gif"} }

context "with vips variant processor" do
include_context "vips variant processor"

it "does not flatten image" do
is_expected.to include({
loader: {n: -1}
})
end
end

context "with mini_magick variant processor" do
include_context "mini_magick variant processor"

it "does not flatten image" do
is_expected.to include({
loader: {page: nil}
})
end
end
end

context "with flatten set to false" do
let(:options) do
{
format: "gif",
flatten: false
}
end

context "with vips variant processor" do
include_context "vips variant processor"

it "does not flatten image" do
is_expected.to include({
loader: {n: -1}
})
end
end

context "with mini_magick variant processor" do
include_context "mini_magick variant processor"

it "does not flatten image" do
is_expected.to include({
loader: {page: nil}
})
end
end
end

context "flatten set to true" do
let(:options) do
{
format: "gif",
flatten: true
}
end

context "with vips variant processor" do
include_context "vips variant processor"

it "flattens image" do
is_expected.to include({
loader: {n: 1}
})
end
end

context "with mini_magick variant processor" do
include_context "mini_magick variant processor"

it "flattens image" do
is_expected.to include({
loader: {page: 0}
})
end
end
end
end
end

0 comments on commit b2d7d44

Please sign in to comment.