RoR - Paperclip - How to set minimal width of an attachement - ruby-on-rails

my layout's requirement is to keep all thumbnails at 80px height, not higher, not smaller. In my model I set the style to :thumb=> "500x80>", so basically almost every picture which is not too wide gets its perfect miniature with 80px height. Sometimes, however, my pictures are narrow and high, so the thumb can have unclickable dimensions of like 5x80. So I dont want to crop pictures as long as thumbnails are not getting crazy narrow, but I think I can make a little sacrifice and crop them if thumb's width is getting smaller than 25px.
So my questions is - is it possible in paperclip to set minimal proportions of a picture by which the style will be "500x80>" and beyond that it will turn to sth like "25x80#"?

I found a nice solution somewhere in the internet a couple of weeks ago. I forgot where, sorry. But it looks like this:
has_attached_file :img, :styles => {:thumb => [Proc.new { |instance| instance.resize }, :jpg]}
def resize
#geo_original = Paperclip::Geometry.from_file(img.to_file(:original))
ratio = #geo_original.width/#geo_original.height
if ratio < 0.4 or ratio > 1.375
# Image very high or very wide
"110x80#"
else
# Average dimensions
"110x80>"
end
end

I'm not sure how you could accomplish this using just paperclip - feels like there should be someway to do it doesn't it?
Paperclip is just using imagemagick in the background (http://www.imagemagick.org/Usage/resize/#shrink) you could cron a job that uses image magic to grow those pesky narrow images on a nightly basis.
It's a hack, but best idea I can offer.
Good luck.

Related

Rails 4 Resizing and Cropping Images similar to Facebook

I currently use yanex rich content api to get pages summaries in my rails 4 app. What is the best way to format the images similar to how Facebook does it? Facebook sometimes shows an image with the size of 377px by 177px which they crop and scale the original so it can maintain the aspect ratio. They also show images to fit a 90px by 90px div which just scale (no crop) the original image.
you can see examples of the two different sizes here: https://www.facebook.com/pages/Coffee-News-Now/209732789217831?ref=hl
Should I use imagemagick and if so what is the logic to determine which image size to show (larger image: cropped/scaled or smaller image: scaled)?
Try looking at paperclip gem.
You can automate thumbnail generation.
has_attached_file :avatar, :styles => { :thumb => ["32x32#", :png] }

Carrierwave - Processed image too big in size

I got a Carrierwave uploader and process images like this:
version :thumbnail do
process :resize_to_model
process :quality => 90
end
def resize_to_model
thumbs_size = model.thumbnail_size
resize_to_fill thumbs_size[:width], thumbs_size[:height]
end
However, after processing an image which was 1024x724px and is 214x151px afterwards the file size only went down from 2,1mb to 1,8mb. I think 1,8mb really is a lot for that size. Can I do something about that? Even with 90% quality the image should be like maybe 100kb or not?
Before someone asks, the rest works perfect. No errors, the size in px is right and everything else is also fine.
Edit: I forgot to mention I Use rmagick(resize_to_fill). Is that a reason maybe?
The difference between 100% and 90% quality is so small and the storage space savings is negligible. If you are truly just using this version as a thumbnail you should look at using a much lower quality, say 60% or 40%.
If you are concerned about making sure the quality is still "good enough" then you could also look at different compression techniques. The process used to provide #2x images for Retina displays can be used in this instance. A great resource is available in the Filament Group's article Compressive Images.
The tl;dr version is basically, use the original (or near original) size of the image but drastically reduce the image quality (to 0-20%). Then, when using the reduced quality image be sure to provide width and height attributes in the <img> element to resize it down to the thumbnail size. Because the image will be scaled down you will not see the reduction in quality of the "thumbnail" image.

Resizing an image to minimum dimensions in Paperclip

I'm using Paperclip to resize an imported image, and I want the image to be a minimum of 465x465, so I've set the following style in my model:
has_attached_file :image, :styles => { :cropped => ["465x465^", :jpg]}
As I understood the ImageMagick documentation, and various other questions and answers on Stack Overflow, the caret(^) should mean that if the image width or height is smaller than those minimum dimensions, it's resized to meet them and also maintain aspect ratio.
The trouble I'm having is that I'm importing images that exceed both the width and height, in which case I'd expect ImageMagick to leave it alone, but it's resizing the smaller dimension (width if it's a portrait image, height if it's landscape) to 465px - does anyone know why that might be?
Taking a look at the documentation of imagemagick, it looks like you're looking at the < flag :
http://www.imagemagick.org/Usage/resize/#enlarge
I didn't try it, but it should work

Paperclip resize and crop to rectangle

So I am expecting a series of photos of different sizes and aspect ratios. I want to be able to shrink/stretch the photo to fit as much as it can in a 200x100 rectangle and then crop the rest that does not fit. I want the crop to happen around the center as well. Is this possible? I am so confused with the imagemagick documentation.
Thanks!
Paperclip's # option will do exactly what you want: fit the image maximally within the specified dimensions then crop the excess with gravity at the center.
Example:
has_attached_file :photo,
:styles => {
:original => "200x100#"
}
Note: If you want to keep the original intact and generate an additional cropped thumb, just change the :original key to something else, like :thumb.
Reference: http://rdoc.info/github/thoughtbot/paperclip/Paperclip/ClassMethods
are any of these usefull?
Resize an image with Paperclip
Simple cropping with Paperclip
http://mfischer.com/wordpress/2009/02/02/multiple-image-upload-and-crop-with-rails/comment-page-1/

How do you setup Paperclip to default to clipping rather than resizing images

I know this should be simple however I'm having a bit of a brain block.
I'm using paperclip for uploading profile images however it's currently resizing and distorting them.
class User < ActiveRecord::Base
has_attached_file :profile_image,
styles: { square_50: "50x50" }
...
end
What I actually want it to do is to clip the images into the correct ratio then resize them to the desired size.
i.e. If my containing box is 200x200px and a 400x500px profile image is uploaded then I want it to have 50px cropped off the top and bottom (to make it 400x400px) and for it then to be resized to 200px x 200px.
What is the correct way to achieve this?
Try :square => '200x200#' -- that should give you the square image you're looking for.

Resources