Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you crop a specific area with paperclip in Rails (3)?

I have paperclip in Rails (3) working with simple cropping, for example the blow code makes a simple crop of the thumbnail:

has_attached_file :image, :styles => { :thumb => "90x90#" }, :default_style => :thumb

However I was wondering how do you crop a very specific area of an image; lets say you have an x and y coordinate to start from and then a width and height of the crop.

How do you go about passing a complex style like this in?

like image 727
MintDeparture Avatar asked Jan 02 '11 13:01

MintDeparture


1 Answers

Check {size}{offset} combination here:

http://www.imagemagick.org/script/command-line-processing.php#geometry

Example where numbers are width, height, x, y:

90x90+40+30

Paperclip parses the style options string and it is limited to resizing and cropping. Complex ImageMagick options work if they are being passed as :convert_options, because they are added to convert command without modification.

has_attached_file :image,
  :styles => { :thumb => "" },
  :convert_options => { :thumb => "-crop 90x90+40+30" },
  :default_style => :thumb

Links to thumbnail processor source code and wiki page:

  • https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/thumbnail.rb
  • https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation
like image 160
Heikki Avatar answered Sep 21 '22 22:09

Heikki