Rails 4 Resizing and Cropping Images similar to Facebook - ruby-on-rails

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

Related

Paperclip gem not setting image width and height rails

I am using Paperclip gem to save images in an application built on rails. But unfortunately, the dimensions, i.e, height and width of the image are not set.
What can be the possible issue?
You have to set the styles you want using imagemagick's geometry, a common setting for example is to crop the biggest image inside the original, so the image isn't stretched:
has_attached_file :avatar, styles: {thumb: "100x100#"}
That will find the largest square inside the image and resize it to 100x100. If the original image is a rectangle it will crop some parts to make it squrae.

Resizing image with ImageMagick and Ruby on Rails

I have been trying to use Imagemagick to resize images uploaded by a user as a square.
Currently, I am using the ! like so - 640x640!
This works fine if the image i feed it is a resolution of 640x640 or bigger - it resizes and makes it into a square as expected.
The problem is that if either the height or width of the image is smaller than 640, then it wont square it out. For instance if the image is 480x600, it wont do anything to the image. Similarly if the image is 680x456 then it will resize it to 640x456
How can i make it so that it will always square the image to a maximum size of 640x640? If the image is greater than 640x640, then i want it to resize to 640x640. If the image is smaller than 640x640, i.e. 480x600, i want it to resize to 480x480
I'm doing it in rails, within the paperclip attachment definition, like this:
has_attached_file :avatar, :styles => { :medium => "640x640!", :thumb => "150x150!" }, :default_url => "/images/:style/missing.png"
First, Require the library
require 'rubygems'
require 'mini_magick'
Second, You have to get the image first
image = MiniMagick::Image.open("PathOfTheImage")
Next, resize it
image.resize "640x640!"
finally, save the image
image.write "output.png"
and use the output image afterwards.
Making it always square may loose images's aspect ratio. Here are couple of ways to resize the image.
resize_to_limit
Resize the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it is larger than the specified dimensions. The resulting image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
resize_to_fit
Resize the image to fit within the specified dimensions while retaining the original aspect ratio. The image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
resize_to_fill
Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image. If necessary, crop the image in the larger dimension.
This one is Image Magick Way
http://www.imagemagick.org/discourse-server/viewtopic.php?t=26196#p115047

How to resize and crop in ImageMagick and Paperclip?

The output image size should be 800x200 or smaller. How to achieve the following?
If the source image is smaller in both dimensions, do not modify.
If the source image is larger in both dimensions, then first resize to the larger side keeping aspect ratio. Then crop. So, 1000x500 is resized to 800x400, then cropped (around the center) to 800x200.
If the source image is larger in one dimension, then crop to 800x200.
Working in Rails with Paperclip.
Not sure if this handles #1 in the question, but it does #2 and #3.
has_attached_file :picture,
:styles => { :medium => "800x200#" }
Thanks to colli8marko for this!
Simple cropping with Paperclip

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