Skip to content

Commit

Permalink
Flatten animated gifs before converting format.
Browse files Browse the repository at this point in the history
Based on @Andrekra solution.

Fixes #794
  • Loading branch information
Thomas von Deyen committed Sep 3, 2015
1 parent ac88d80 commit a05855c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
11 changes: 7 additions & 4 deletions app/controllers/alchemy/pictures_controller.rb
Expand Up @@ -49,13 +49,16 @@ def send_image(image, format)
request.session_options[:skip] = true
ALLOWED_IMAGE_TYPES.each do |type|
format.send(type) do
options = []
if type == 'jpeg'
quality = params[:quality] || Config.get(:output_image_jpg_quality)
image = image.encode(type, "-quality #{quality}")
else
image = image.encode(type)
options << "-quality #{quality}"
end
render text: image.data
# Flatten animated gifs, only if converting to a different format.
if type != "gif" && image.ext == 'gif'
options << "-flatten"
end
render text: image.encode(type, options.join(' ')).data
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/controllers/pictures_controller_spec.rb
Expand Up @@ -235,6 +235,29 @@ module Alchemy
end
end
end

context 'requesting an animated gif with different format' do
let(:image) do
fixture_file_upload(
File.expand_path('../../fixtures/animated.gif', __FILE__),
'image/gif'
)
end

let(:picture) { build_stubbed(:picture, image_file: image) }

before do
expect(image).to receive(:ext) { 'gif' }
expect(picture).to receive(:image_file) { image }
expect(Alchemy::Picture).to receive(:find) { picture }
end

it 'flattens the gif before converting the format.' do
expect(image).to receive(:encode).with('png', '-flatten') { double(data: '') }
alchemy_get :show, id: picture.id, name: picture.urlname,
format: 'png', sh: picture.security_token
end
end
end

describe '#thumbnail' do
Expand Down
Binary file added spec/fixtures/animated.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a05855c

Please sign in to comment.