Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jotto/5256221 to your computer and use it in GitHub Desktop.
Save jotto/5256221 to your computer and use it in GitHub Desktop.
https://github.com/thoughtbot/paperclip/issues/1085 this is cheap hack to get the convert_options to happen before transformation so the image size stays as defined even if there are convert options
raise "Expecting Paperclip version 3.3.1 since we are monkey patching it in application.rb" unless Paperclip::VERSION == "3.3.1"
module Paperclip
class Thumbnail < Processor
def make
src = @file
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
dst.binmode
begin
parameters = []
parameters << source_file_options
parameters << ":source"
if transformation_command.any?{|cmd|cmd.match(/resize/)}
parameters << convert_options
parameters << transformation_command
else
parameters << transformation_command
parameters << convert_options
end
parameters << ":dest"
parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
success = convert(parameters, :source => "#{File.expand_path(src.path)}#{'[0]' unless animated?}", :dest => File.expand_path(dst.path))
rescue Cocaine::ExitStatusError => e
raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
rescue Cocaine::CommandNotFoundError => e
raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
end
dst
end
end
end
@rterbush
Copy link

@jotto Thanks for your help with this. I offer back to you the following enhancements. I'm using this with Spree, so currently stuck to Paperclip 2.8.0, so changes are reflected.

I also have need to add some flags after the -resize flag, so I have included some changes in transformation_command to further hack your monkey patch. Thanks again.

raise "Expecting Paperclip version 3.3.1 since we are monkey patching it in application.rb" unless Paperclip::VERSION == "2.8.0"

module Paperclip 
  class Thumbnail < Processor

    # Returns the command ImageMagick's +convert+ needs to transform the image
    # into the thumbnail.
    def transformation_command
      scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
      trans = []
      trans << "-coalesce" if animated?
      trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
      trans << "-crop" << %["#{crop}"] << "+repage" if crop
      trans << '-layers "optimize"' if animated?
      trans << "-extent" << %["#{scale}"] unless scale.nil? || scale.empty?
      trans << '-unsharp 0x0.5 -enhance'
      trans
    end

    def make
      src = @file
      dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
      dst.binmode

      begin
        parameters = []
        parameters << source_file_options
        parameters << ":source"
        if transformation_command.any?{|cmd|cmd.match(/resize/)}
          parameters << convert_options
          parameters << transformation_command
        else  
          parameters << transformation_command
          parameters << convert_options
        end
        parameters << ":dest"

        parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

        success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}#{'[0]' unless animated?}", :dest => File.expand_path(dst.path))
      rescue Cocaine::ExitStatusError => e
        raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
      rescue Cocaine::CommandNotFoundError => e
        raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
      end

      dst
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment