How to crop & fill with Paperclip (or RMagick)? - ruby-on-rails

I upload a photo, it is a rectangle. How Can I get it resized and filled to a square ?
I mean when the photo is horizontal positioned it should have above and under it, two white fields (for keeping the shape of a square) and when it is vertically, it should have two white fields on the sides of the photo.
When I used PHP, a have used this http://www.verot.net/php_class_upload_samples.htm
Have a look at the
100x150, keeping ratio, filling top and bottom
example
I'm using Paperclip with RoR. How is the best way to do that ?

Here's what I used on a rails 3 app w/ paperclip. I used the following ImageMagick options to make it centered: background, compose, gravity and extent. I'm using the mini_magick processor.
has_attached_file :image,
:styles => { :large => ["855x570>", :jpg], :medium => ["432x288>", :jpg], :small => ["276x184>", :jpg], :tiny => ["195x130>", :jpg] },
:processor => "mini_magick",
:convert_options => {
:medium => "-background white -compose Copy -gravity center -extent 432x288",
:small => "-background white -compose Copy -gravity center -extent 276x184",
:tiny => "-background white -compose Copy -gravity center -extent 195x130"
}

Related

rmagick rotate and resize to fit specified dimensions in paperclip

All I essentially want to do, is to crop an image using jcrop, then with those dimensions, rotate the image -30deg and save the thumbnail. However rmagick creates additional pixels due to the fact that there's no scaling involved after the rotation. I'd like to know how to achieve that.
# 150 x 150 is the final cropped image I want for my thumb(nails)
# :croppable is a file I use that takes the original and adds white padding in a 1200x1200
# file size so I can actually crop with white space available (you can't crop outside the
# original dimensions of the file)
has_attached_file :photo, :styles => {
:thumb => { :geometry => "150x150#", :format => :jpg, :processors => [:cropper] },
:general => ["150x375", :jpg],
:show => ["x425", :jpg],
:croppable => ["1200x1200>", :jpg]
},
:url => "/assets/w/:style/:w",
:path => ":rails_root/public:url",
:default_url => ":w_default",
:default_path => ":rails_root/public:w",
:default_style => :show,
:convert_options => {
:thumb => '-gravity center -rotate -30',
:croppable => '-gravity center -extent 1200x1200',
:general => '-gravity center -extent 150x375 -quality 95',
:all => '-quality 100 -antialias -flatten -background white -interlace Plane -unsharp 0.3x0.3+5+0'
},
:processors => [:thumbnail, :compression]
What I eventually want to do is to rotate the preview image via css transform, so that the preview actually shows what the thumbnail will look like after cropping. At this point I'm not sure how to get what I want when it comes to paperclip saving the thumbnail and rotating it.
No answer found. I ended up switching to Carrierwave and encountering another set of problems which I've finally arrived at a solution for. Too bad Carrierwave doesn't log the processing of images, but that's a small price to pay.
Carrierwave RMagick not removing transparency in convert to jpg
Carrierwave +repage option not working

Resize missing.png depending upon style in paperclip

I'm using Paperclip to upload a image
here my paperclip configuration
has_attached_file :avatar,
:path => ":rails_root/public/users/:id/avatar/:style/avatar.jpg",
:url => "/users/:id/avatar/:style/avatar.jpg",
:default_url => "/missing/users/:style/missing.png",
:styles => {"47x47" => "47x47", "228x228" => "228x228","185x176"=>"185x176","pitch_planner"=>"262x129!"},
:convert_options => {"47x47" => "-background black -gravity center -extent 47x47",
"228x228" => "-background black -gravity center -extent 228x228","185x176" => "-background black -gravity center -extent 185x176"}
Now what if I want is to generate a resize image of missing.png depending upon the "style" How to achieve this in paperclip
One way to do it resize the image manually and store it inside folder pitch_planner or what ever styles you want to resize for
can it be done in programmatically through paperclip
Not with paperclip, but you could overwrite the method that looks for the default image, and use image magick to create it if not already present.
img = Magick::Image::read(default_image).first
img.resize_to_fit(75, 75)
img.write 'path'

How to resize and center image

I would like to set not square image to square and use for this square center of image. How to done it with paperclip?
This will take an image, crop in the center of the image at 500x500, then throw everything else away, then resize that new image back down to 100x100. It's used for generating square thumbnails, but you can adjust this for your needs.
has_attached_file :image,
:styles => { :thumb => "" },
:convert_options => {
:thumb => "-gravity Center -crop 500x500+0+0 +repage -resize 100x100^",
:default_style => :thumb

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?
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 ...

how to write this where paperclipe understands

I can do this command to resize an image to fit a specific size on the command line with Imagemagick. How do I tell paperclip can do the same when I upload an image:
convert Bisiye2.jpg -thumbnail '150x150^' -gravity center -extent 150x150 Bisiye2_tofit.jpg
has_attached_file :image, :styles => { :thumb => "150x150>" },
:convert_options => {:thumb => "-gravity center -extent 150x150"}

Resources