markevans / dragonfly

A Ruby gem for on-the-fly processing - suitable for image uploading in Rails, Sinatra and much more!
http://markevans.github.io/dragonfly
MIT License
2.12k stars 244 forks source link

no way to select frames when using ImageMagick converters #243

Closed k3rni closed 10 years ago

k3rni commented 11 years ago

Frames (as in a GIF, MNG animation or a PS, PDF file with multiple pages) are selected using a subscript notation (see http://www.imagemagick.org/script/command-line-processing.php, under "Selecting Frames"). When not selected, IM either keeps the multiple-image format, or produces multiple separate files.

Dragonfly currently doesn't offer an option to choose which frame I want to work on.

Examples:

convert multipage.pdf -thumbnail 100x100 outputfile

creates outputfile as a multipage pdf, each page scaled down appropriately

convert multipage.pdf -thumbnail 100x100 outputfile.png

produces as many files named outputfile-%d.png as there were pages

convert multipage.pdf[0] -thumbnail 100x100 outputfile

creates a single-page pdf, scaled down

convert multipage.pdf[0] -thumbnail 100x100 outputfile.png

creates a single png file, with the desired thumbnail

The last option is what I'm after, such as: Dragonfly[:app].fetch(UID).convert('-thumbnail 100x100', :frame => 0).png or Dragonfly[:app].fetch(UID).thumb('100x100', :frame => 0).png

Or have I missed something in the docs?

k3rni commented 11 years ago

Also, for PDFs at least, page numbering doesn't have to start at 0. But that's not really important here.

markevans commented 11 years ago

I see. you're correct - it's not particularly supported yet because the imagemagick processors use a "convert" method under the covers which obscures the path argument and doesn't allow adding a number [n] I'll add this to the TODO list, or submit a pull request if you wish (though be warned I'm changing the processors fairly drastically in a branch soon be merged in)

aaronchi commented 11 years ago

was support ever added for this? Trying to resize only the first frame of an animated gif

markevans commented 10 years ago

this has been added in version 1 http://markevans.github.io/dragonfly/imagemagick/#processors

aaronchi commented 10 years ago

I saw. New version look great :)

The other thing I had added to dragonfly was a frame counter to analyzers to see if an image is animated. Not sure if you want to add this to the default analyzer but this is the code I added to count frames

# Counts frames to see if an image is animated
module Dragonfly
  module ImageMagick
    module Analysers
      class ImageAnimated

        def call(content)
          identify_command = content.env[:identify_command] || 'identify'
          details = content.shell_eval do |path|
            "#{identify_command} -format '%n' #{path}"
          end
          details.scan(/\d/).join('').to_i > 1
        end

      end
    end
  end
end
markevans commented 10 years ago

cool! good to see the new custom analyser stuff getting used. I won't add it by default if you don't mind, as I'm trying to keep the codebase small and let users do their own processors/analysers as much as possible. by the way there's also a method "identify" (not an analyser) and a configuration option "define" so if you don't need it to be an analyser, you can just do this now in the config:

define :image_animated? do
  identify("-format %n").to_i > 1
end
k3rni commented 10 years ago

:+1: nicely done.