Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the top region of an image when cropping with Rails and Paperclip?

So I have a Rails 3 app using Paperclip to crop images.

I have this code in my model for Photo:

 has_attached_file :thumbnail, PAPERCLIP_OPTIONS.merge(
    :styles => {:cropped => '300x250#'})

The resulting image that's generated creates a 300x250 image, however the crop seems to always start a good 50px or so below the top of the image (not a good thing for social networking when it cuts off the top of peoples heads).

I did some research and I'm thinking I need to supply a :convert_options key that coincides with the :cropped style. However, I don't know exactly what options to set (-gravity, -region, etc.)

Anybody have any thoughts. I know there are Imagemagick pros; I'm not one, lol.

Thanks!

Update: I found this link.. http://forrst.com/posts/Customized_Cropping_with_Paperclip-7g6

Is this still valid or does somebody have a better easier way?

like image 264
Ben Scheib Avatar asked Nov 21 '11 20:11

Ben Scheib


1 Answers

Here's my favorite way to do it:

:styles => { :large => "", :medium => "", :thumb => ""},
    :convert_options => { 
        :large => "-gravity north -thumbnail 300x300^ -extent 300x300" ,
        :medium => "-gravity north -thumbnail 200x200^ -extent 200x200",
        :thumb => "-gravity north -thumbnail 100x100^ -extent 100x100"
    }

Note that instead of # you use ^ + extent.

Gravity parameters are like on a map: north, northeast, east ...

like image 133
Alex Marchant Avatar answered Nov 13 '22 01:11

Alex Marchant