rmagick rotate and resize to fit specified dimensions in paperclip - ruby-on-rails

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

Related

Rails paperclip generate thumbnail from other style

I'm following the jcrop rails tutorial, but I've hit a snag. What it comes down to is the fact that paperclip is generating the thumbnail from the original file, but I need it to be generated from another style. The original file doesn't have any space between the product shot and the edge of the document. Therefore I can't crop further out. To combat that, I made another style which has white pixel padding. That's what I want to generate the thumbnail from.
# croppable is the one with the padding...it's what shows up in the crop view.
# I want :thumb to be generated from THAT style, not :original.
# When generating from :original, the crop offset/size is screwed because the dimensions of :original don't match :cropped
# and I can't crop beyond the pixel dimensions of :original.
has_attached_file :photo, :styles => {
:thumb => { :geometry => "300x300#", :format => :jpg, :processors => [:cropper] },
:general => ["150x375", :jpg],
:show => ["x425", :jpg],
:croppable => ["1200x1200>", :jpg]
},
:url => "/assets/wines/:style/:wine_name",
:path => ":rails_root/public:url",
:default_url => ":wine_default",
:default_path => ":rails_root/public:wine_default",
: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 -unsharp 0.3x0.3+5+0'
},
:processors => [:thumbnail, :compression]
I came across a processor that someone else had made online and used it for myself. Details can be found at http://pjkh.com/articles/speeding-up-thumbnail-generation-with-paperclip/ for advanced usage information.
recursive_thumbnail.rb (include in lib/paperclip_processors)
module Paperclip
class RecursiveThumbnail < Thumbnail
def initialize file, options = {}, attachment = nil
super Paperclip.io_adapters.for(attachment.styles[options[:thumbnail] || :original]), options, attachment
end
end
end
and for your model:
:styles => {
:croppable => ["1200x1200>", :jpg],
:show => ["x425", :jpg],
:general => ["150x375", :jpg],
:thumb => {
:geometry => "150x150^",
:format => :jpg,
:processors => [:recursive_thumbnail] },
:thumbnail => :croppable
}
Maybe this would help you or someone else like me who Google led to this question.
https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation
Generating/regenerating your thumbnails
Note: regenerating only one of several defined styles as described in
some of the examples below will lead to broken paths/urls for all
other styles if you have :hash in your paperclip_defaults[:path] and
:updated_at in your paperclip_defaults[:hash_data] (and you have by
default). Don’t do this unless you really know what you’re doing.
You can (re)generate your thumbnails en masse with Paperclip’s rake
tasks. Using our example class above:
bundle exec rake paperclip:refresh:thumbnails CLASS=User

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 crop & fill with Paperclip (or RMagick)?

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"
}

Resources