carrierwave: multiple version strategy

1.2k Views Asked by At

I want to create multiple versions like :large, :medium, :small. No Problem so far. But how are these methods invoked?

A simple test:

version :large do
  process :resize_to_fit => [1024, 1024]
end
version :large2, :from_version=>:large do
  process :resize_to_fit => [1024, 1024]
end

results into two different sized files. I would assume the versions to be identic. So I guess :large2 is re-reading the file (large) from disk into RMagick?

How can I do it "right"? (I would use the same RMagick-Object to create the versions) I want to try to keep on the carriewave-way and not to do too much by myself.

Thanks and regards for any hint, Phil.

1

There are 1 best solutions below

3
On BEST ANSWER

An example to create 3 typical image file sizes with Carrierwave using MiniMagick in the uploader.rb file

# Create different versions of your uploaded files. 
  version :full do
     process :resize_to_fill => [1024, 1024] 
  end

  version :large_thumb, :from_version => :full do
     process :resize_to_fill => [300, 300] 
  end

  version :thumb, :from_version => :large_thumb do
     process :resize_to_fill => [100, 100] 
  end    

MiniMagick efficiency considerations when creating different sized images

MiniMagick docs suggest for processing efficiency to start with the largest resolution size and work down, cascading the re-sized files instead of starting with the large one each time as a base file converted (https://github.com/carrierwaveuploader/carrierwave).

Create versions from existing versions

For performance reasons, it is often useful to create versions from existing ones instead of using the original file. If your uploader generates several versions where the next is smaller than the last, it will take less time to generate from a smaller, already processed image.

You do this with the :from_version directive shown in the example above.

MiniMagick resize_to_fill versus resize_to_fit, aspect ratios, scaling

MiniMagick gives you various image manipulation options. I find resize_to_fill works for my purposes better than resize_to_fit, but your mileage may vary. There are considerations when choosing an image re-sizing option, especially in the case where the aspect ration of the original image does not match that of the resize target image.

Here's the ImageMagick spec with all of the image magic that MiniMagick uses: http://www.imagemagick.org/Usage/resize/#resize

MiniMagick gemspec https://github.com/minimagick/minimagick