How do you crop a specific area with paperclip in Rails (3)? - ruby-on-rails

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?

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

This could also answer your question:
http://railscasts.com/episodes/182-cropping-images

Related

Using paperclip + papercrop with specific style

How can I set the crop of an image only in a specific style?
This are my styles:
{
:thumb => attachment.instance.thumb_style,
:small => attachment.instance.small_style,
:normal => attachment.instance.normal_style
}
So when I crop the image, the only style that will change is the :thumb.
Found a solution.
I was trying to use reprocess! inside the model with an after_asset_post_process but it was creating an infinite loop, so what I did was:
img = Paperclip::Attachment.new("asset", instance, {:path => ':rails_root/public/system/assets/:class/:attachment/:id/:style/:basename.:extension', :styles => {:normal => "620x"} })
img.reprocess!
After I saved the asset in the controller.

Rails, Paperclip: Downsizing image minimum amount such that one dimension matches geometry

I'm using Paperclip in a Rails project to resize an important image and have tried the following:
has_attached_file :attachment, :styles => { :medium => ['640x480>', :jpg] }
has_attached_file :attachment, :styles => { :medium => ['640x480^#', :jpg] }
I'd like to downsize an image the minimum amount necessary so that the height=640 or width=480. For instance a source image which is 1920x1080 would be resized to 853x480, and a source image which is 1080x1920 would be resized to 640x1137. None of the geometry arguments listed in the ImageMagick documentation seem to have the desired effect.
Is there a geometry argument that would accomplish the resizing I desire? If not, how might this best be accomplished?
As posted in the comments the fill flag (^) does exactly what Mark needs. That flag is used to resize the image based on the smallest fitting dimension. That is, the image is resized to completely fill (and even overflow) the pixel area given. The code should be changed to this:
has_attached_file :attachment, :styles => { :medium => ['640x480^', :jpg] }
Documentation about this flag can be found here: http://www.imagemagick.org/Usage/resize/#fill.

where does paperclip store relationship between an interpolator using a hash and the file

I am using paperclip and would like to process a set of files offline and ideally move them to a content server. But I am not seeing where paperclip is storing this information to be able to get at it. The hash used isn't meant to obfuscate so I'm not that worried about it but would like to handle different images with same name as we've had problems with this from current digital cameras.
I have the following interpolation:
Paperclip.interpolates :val do |attachment, style|
Digest::MD5.hexdigest("me-" + attachment.size.to_s + "-other")
end
has_attached_file :asset,
:url => "/images/:global_path/:val-:style.:extension",
:path => UPLOAD_PATH + "/:global_path/:val-:style.:extension",
:styles => { :medium => "200x200>", :thumb => "75x75>", :bigthumb => "125x125"
}
Producing a url like: /images/170/1c8ce44646e4cbe816ebe901482783e9-thumb.jpg?1337831837
I'd like to be able to rsync the images directory but can't find how to get at the hash value. Where would I find this?
thx

Rails Paperclip, Multiple of Different Type (PDF, Image, Doc...)

There are a lot of good resources out there that show how to create a Rails application with multiple image uploads. Additionally, there are a lot of good resources showing how to use paperclip to upload different file types (PDF, image, .Doc).
I'd like to create an application where a User has an image asset for their profile picture, and also has the ability to upload PDFs or .Doc files to their account to be tied to the User. Has anyone had experience with this to confirm it's possible through the Paperclip gem? Any tutorials or resources to point me in the right direction would be appreciated.
I was also having exactly same problem like you. No need to do extra coding for this, just add following
has_attached_file :attachment,
:styles => lambda{ |a|
["image/jpeg", "image/png", "image/jpg", "image/gif"].include?( a.content_type ) ? {
:thumb=> "100x100#",
:small => "150x150>",
:medium => "300x300>",
:large => "500x500>" }: {}
}
Let me know if you need further explanations.
Just in case anyone is looking at this recently, we have just tried the accepted answer, and the content types did not work properly. We got it working like this:
has_attached_file :attachment, styles: lambda { |a| a.instance.attachment_content_type =~ %r(image) ? {:small => "x200>", :medium => "x300>", :large => "x400>"} : {} }
The difference is in the "column name" for the object -- typically "___content_type", instead of just "content_type"
You sure can upload images and other filled with paperclip, may be worth going to www.railscasts.com as there are quite a few related posts to your question.

zooming image in ruby

Can anyone tell me the way to zoom the image in Ruby on rails please ?
There's railscast #182 on image Cropping.
If you are using Paperclip, you may specify different sizes for thumbnails, like this:
class Client < ActiveRecord::Base
LOGO_STYLES = {
:original => ['1024x768>', :jpg],
:big => ['512x384#', :jpg],
:medium => ['256x192#', :jpg],
:small => ['128x96#', :jpg]
}
has_attached_file :logo, :styles => Client::LOGO_STYLES
attr_protected :logo_file_name, :logo_content_type, :logo_size
Finally, if you already have the image with the size you want, and you only want to change how it looks on the client, then your problem isn't ruby - it is javascript. Google for "javascript zoom" instead of "rails zoom" :)
You should be clearer.
If what you want to do is allow the user to zoom on an image then you should take a look at javascript.
For example jqzoom, which does a zoom on an image when your mouse is on it.
If what you want is to resize the image to a specific size then it's a ruby/rails problem.
And you can use paperclip to do so.
In your model, you'd add the following :
has_attached_file :my_image, :styles => { :thumb => ["32x32#", :png] }
Where the "32x32" will be the size of the image after it's been resized.
Then you follow the paperclip's README to see the migration and informations to add to the view.
And when you'll validate your form, the image will be automatically uploaded and resized.
If you don't wish to upload the files before to resize them, you can use Paperclip::Thumbnail directly.
And resize your image with the following :
Paperclip::Thumbnail.new('/path/to/your/file', { :geometry => '32x32#' }).make
And you'll have your thumbnail.

Resources