A
A
Andrey K2012-11-16 10:06:53
Ruby on Rails
Andrey K, 2012-11-16 10:06:53

ImageMagick: looping when cropping an image?

Hello! I ran into a problem when cropping an image - the execution of the imagemagick command is looping.
For those familiar with Rails and paperclip, the crop preprocessor:

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command
        crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ') # super returns an array like this: ["-resize", "100x", "-crop", "100x100+0+0", "+repage"]
      else
        super
      end
    end

    def crop_command
      target = @attachment.instance
      if target.cropping?
        ["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}", "-format", "PNG"]
      end
    end
  end
end

I can't understand why...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-08-24
@svetozar

Cyclic because:
after_update :reprocess_image, :if => :cropping?
It turns out saving the picture in after_update calls after_update again.
To avoid looping, you can use before_commit (so the image update is called twice, but at least not infinity)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question