Skip to content

Identify animated formats using ImageMagick #880

Closed
wants to merge 2 commits into from
View
11 lib/paperclip/thumbnail.rb
@@ -98,7 +98,16 @@ def transformation_command
# Return true if the format is animated
def animated?
- @animated && ANIMATED_FORMATS.include?(@current_format[1..-1]) && (ANIMATED_FORMATS.include?(@format.to_s) || @format.blank?)
+ @animated && (ANIMATED_FORMATS.include?(@format.to_s) || @format.blank?) && identified_as_animated?
+ end
+
+ # Return true if ImageMagick's +identify+ returns an animated format
+ def identified_as_animated?
+ ANIMATED_FORMATS.include? identify("-format %m :file", :file => "#{@file.path}[0]").to_s.downcase.strip
+ rescue Cocaine::ExitStatusError => e
+ raise Paperclip::Error, "There was an error running `identify` for #{@basename}" if @whiny
+ rescue Cocaine::CommandNotFoundError => e
+ raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
end
end
end
View
BIN test/fixtures/animated
Binary file not shown.
View
BIN test/fixtures/animated.unknown
Binary file not shown.
View
45 test/thumbnail_test.rb
@@ -234,6 +234,17 @@ class ThumbnailTest < Test::Unit::TestCase
end
end
+ context "being thumbnailed with default animated option (true)" do
+ should "call identify to check for animated images when sent #make" do
+ thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x50#")
+ thumb.expects(:identify).at_least_once.with do |*arg|
+ arg[0] == '-format %m :file' &&
+ arg[1][:file] == "#{File.expand_path(thumb.file.path)}[0]"
+ end
+ thumb.make
+ end
+ end
+
context "passing a custom file geometry parser" do
teardown do
self.class.send(:remove_const, :GeoParser)
@@ -380,6 +391,40 @@ def to_s
end
end
+ context "with unidentified source format" do
+ setup do
+ @unidentified_file = File.new(fixture_file("animated.unknown"), 'rb')
+ @thumb = Paperclip::Thumbnail.new(@file, :geometry => "60x60")
+ end
+
+ should "create the 12 frames thumbnail when sent #make" do
+ dst = @thumb.make
+ cmd = %Q[identify -format "%wx%h" "#{dst.path}"]
+ assert_equal "60x60"*12, `#{cmd}`.chomp
+ end
+
+ should "use the -coalesce option" do
+ assert_equal @thumb.transformation_command.first, "-coalesce"
+ end
+ end
+
+ context "with no source format" do
+ setup do
+ @unidentified_file = File.new(fixture_file("animated"), 'rb')
+ @thumb = Paperclip::Thumbnail.new(@file, :geometry => "70x70")
+ end
+
+ should "create the 12 frames thumbnail when sent #make" do
+ dst = @thumb.make
+ cmd = %Q[identify -format "%wx%h" "#{dst.path}"]
+ assert_equal "70x70"*12, `#{cmd}`.chomp
+ end
+
+ should "use the -coalesce option" do
+ assert_equal @thumb.transformation_command.first, "-coalesce"
+ end
+ end
+
context "with animated option set to false" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, :geometry => "50x50", :animated => false)
Something went wrong with that request. Please try again.