Skip to content

Instantly share code, notes, and snippets.

@patsanch
Created May 6, 2011 07:48
Show Gist options
  • Save patsanch/958575 to your computer and use it in GitHub Desktop.
Save patsanch/958575 to your computer and use it in GitHub Desktop.
Paperclip Cropper
# http://pastie.org/995357
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
puts "CROP: #{crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')}"
crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')
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}"]
end
end
end
end
@andrewhavens
Copy link

Found this approach a bit cleaner: http://viget.com/extend/manual-cropping-with-paperclip

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

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