Rails, Paperclip: Downsizing image minimum amount such that one dimension matches geometry - ruby-on-rails

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.

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.

How to get image to show using s3 and paperclip?

I am using paperclip and AWS for my Rails app to upload image. You can find it here: http://lit-stream-6263.herokuapp.com/
When I try to upload images, I don't get an error but for some reason the image doesn't show. When I go into the S3 bucket though, I'm able to see the image that gets uploaded...it's just not rendering in the html page. Any advice on how to fix this?
Update
From 9nonnatus, I'm seeing the picture if I change the URL. However in my rails view I have
<%= image_tag product.avatar.url(:medium) %>
to display the image. This is what I see in the documentation as well. How do I adjust this to fit the url you mention above?
class Product < ActiveRecord::Base
attr_accessible :blog_link, :blog_name, :description, :image_link, :name, :num_likes, :product_link, :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end
Looking at the source of the page you can copy the image link and try to access it in your browser. It gives an error telling you that your hyperlinks are incorrectly formatted. Instead of making the img src format something like:
http://s3.amazonaws.com/rockywolfugc/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
you have to use this format:
http://rockywolfugc.s3-us-west-2.amazonaws.com/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
In other words, remove /rockywolfugc from after .com and replace s3 with rockywolfugc.s3-us-west-2
Hope that helps.

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.

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

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