Cropping with Carrierwave

Update: An earlier version of this post had a bug in it.

Recently on a project, we needed to be able to crop images uploaded with Carrierwave. Carrierwave supports cropping functionality through the resize_to_fill method, which will crop from the center of the image appropriately, depending on the orientation of the original image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require 'carrierwave/processing/mime_types'

class AvatarUploader < BaseUploader
  include CarrierWave::MiniMagick
  include CarrierWave::MimeTypes

  process :set_content_type

  version :cropped do
    resize_to_fill: [100, 100]
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end
end
ruby carrierwave