Resizing image with ImageMagick and Ruby on Rails - 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

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.

How to resize the uploaded image to specific width in carrierwave gem in Rails

I am uploading an image to my rails app using carrierwave.
I wish to have the max-width fixed to a specific size and height according to the width.
When I am doing it like this
version :medium do
resize_to_limit(600,0)
end
Then the image is not coming up.
Also, What's the difference between resize_to_limit and resize_to_fit
Please guide.
If you are content with having only one version, then you can just add this to your uploader:
include CarrierWave::MiniMagick
process resize_to_fill: [600, 0]
Here is the documentation for 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.
Here's for 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.
And here for 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.
You need to recreate new version of image uploader like :-
version :medium do
process :resize_to_fill => [600,0]
end
Then recreate them :-
User.all.each do |user|
user.avatar.recreate_versions!
end

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

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/

Resources