Carrierwave - Resizing Images to Fixed Width

Carrierwave - Resizing images to fixed width

Just put this in your uploader file:

class ImageUploader < CarrierWave::Uploader::Base

version :resized do
# returns an image with a maximum width of 100px
# while maintaining the aspect ratio
# 10000 is used to tell CW that the height is free
# and so that it will hit the 100 px width first
process :resize_to_fit => [100, 10000]
end

end

Documentation and example here: http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit

Keep in mind, resize_to_fit will scale up images if they are smaller than 100px. If you don't want it to do that, then replace that with resize_to_limit.

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

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.

resizing images using minimagick and carrier wave

Use resize_to_fit or resize_to_limit instead of resize_to_fill

process :resize_to_limit => [450, 253]

or

process :resize_to_fit => [450, 253]

Carrierwave resize an image before saving

My problem was the line config.enable_processing = true
in my carrierwave.rb

I removed it and now the images are saved 150x150 as I wanted.

The only line that I needed to add in uploader.rb was :
process resize_to_fill: [150, 150]

Resize images with carrierwave if it exceeds height/width

Change :resize in the image uploader to process :resize_to_fit => [584, 329]

Carrierwave: Scale image if the size is larger than (conditionally create versions)

I defined method in which if image exceed given width then manipulate it to your size the 32 pixels in this case. Put this code in your ImageUploader:

  version :thumb do 
process :resize_to_width => [32, nil]
end

def resize_to_width(width, height)
manipulate! do |img|
if img[:width] >= width
img.resize "#{width}x#{img[:height]}"
end
img = yield(img) if block_given?
img
end
end

Exact dimensions through imageMagic and Carrierwave

Summary

Carrierwave passes a block with resize_to_fit and resize_to_limit to the manipulate! method to crop the image.

By changing this block, you can change the cropping/resizing. Carrierwave calls on instance Magick::Image the method change_geometry at this line.

The change_geometry method supports resizing a method by specifying constraints. For example, you can specify that the image should be resized such that the aspect ratio should be retained but the resulting image should be no larger than 640 pixels wide and 480 pixels tall.

The argument may be either a geometry string or a Geometry object. Change_geometry yields to the block, passing new width and height values based on the argument with respect to self. The return value is the return value of the block.

You need to pass a block to manipulate! similar to the below example:

def resize_no_crop(width, height)
width = dimension_from width
height = dimension_from height
manipulate! do |img|
# change the below geometry object to achieve your effect
# geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
new_img = img.change_geometry(geometry) do |new_width, new_height|
img.resize(new_width, new_height)
end
destroy_image(img)
new_img = yield(new_img) if block_given?
new_img
end
end

Set the geometry object as per your desires

geometry = Magick::Geometry.new(width, height, 0, 0, !)

as I read in the docs, the last parameters ! has the following effect

! Use this flag when you want to force the new image to have exactly the size specified by the the width and height attributes.

ImageMagick command line tool and Rmagick api docs provide more information.

Explanation

Carrierwave uses miniMagick to perform the resizing process.

The ImageProcessing gem uses MiniMagick, a mini replacement for RMagick that uses ImageMagick command-line tools, to build a "convert" command that performs the processing.

This is how the resize_to_limit method works, it may help you building your own method using the Rmagick and ImageMagick command line tools.

module MiniMagick
extend ActiveSupport::Concern

included do
require "image_processing/mini_magick"
end

module ClassMethods
def convert(format)
process :convert => format
end

def resize_to_limit(width, height)
process :resize_to_limit => [width, height]
end
end
end

The method manipulates the images using the ImageMagick command line tool and Rmagick. You can visit the above websites to read their documentation.

As you can read below resize_to_limit manipulates the image by creating a new instance of Magick::Geometry and passing a width,height, x,y and flag

def resize_to_limit(width, height)
width = dimension_from width
height = dimension_from height
manipulate! do |img|
# Read The Explanation below
end
end

Carrierwave created their own manipulate! method which will iterate with the yield statement and crop/resize the image

  image.each_with_index do |frame, index|
frame = yield(*[frame, index, options].take(block.arity)) if block_given?
frames << frame if frame
end

The method each_with_index will iterate between manipulate! do ... end with the yield statement.

*[frame, index, options].take(block.arity), .take(block.arity) will pass only frame as it is called with only one parameter.

The frame variable will equal the new_image returned.

geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
new_img = img.change_geometry(geometry) do |new_width, new_height|
img.resize(new_width, new_height)
end
destroy_image(img)
new_img = yield(new_img) if block_given?
new_img

You can read more about Image::Magick class and on rmagick.github.io to better understand how the process of frame selection works.



Related Topics



Leave a reply



Submit