Evo_x
Evo_x

Reputation: 3017

Carrierwave - Dynamic height/width and cropping

So I got my Carrierwave Uploader which is pretty normal:

class ThumbFileUploader < CarrierWave::Uploader::Base

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

  process :resize_to_limit => [2000, 480]
end

In my model I got:

article.remote_thumbnail_url = article.picture_url

What I want to do is

  1. Tell carrierwave the dimensions to use for scaling from within my model
  2. Crop the image if it doesn't fit the exact dimensions after scaling

What's the best practice to accomplish that?

Upvotes: 0

Views: 791

Answers (1)

Mark Stratmann
Mark Stratmann

Reputation: 1612

I have had exactly the same problem.

You basically need to have your model store the original image dimensions when the file is first uploaded. You can then use these with jCrop to control the cropping process.

My carrier wave uploaded as this function

  def get_geometry
    if (@file)
      img = ::Magick::Image::read(@file.file).first
      @geometry = [ img.columns, img.rows ]
    end
  end

You have to do rather a lot in the Model, the uploader and in your views.

A full write up is on my web site at the link below.

http://mark.stratmann.me/content_items/image-cropping-with-rails-3-2-carrierwave-bootstrap-modals-jcrop

Upvotes: 1

Related Questions