Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic image versions with carrierwave

I have a list of sizes in the database and I need to resize my images to, and I use carrierwave to upload them.

Is there some way to use carrierwave to handle the resizing? right now I'm using a rake task to do it, although I'll probably switch to using girl_friday actors so that I can more easily trigger it.

Edit

In the end I ended up without using proper carrierwave versions, but used a carrierwave callback to add the resizing jobs to the background processor (In this case it's girl_friday)

class ImageUploader < CarrierWave::Uploader::Base
  after :store, :resize_by_db
  def resize_by_db(args)
    widths = Resolutions.all.map &:width
    widths.each do |width|
      RESIZE_QUEUE  << {:source => path, :width => width}
    end
  end
end
like image 533
Phrodo_00 Avatar asked Feb 14 '13 15:02

Phrodo_00


2 Answers

You need to call class method 'version' from instance method.

process :selected_version

def selected_version
  aspect_ratio = model.aspect_ratio
  name = aspect_ratio.name

  self.class.version name do
    process :crop
    process :resize_to_fit => [aspect_ratio.width, aspect_ratio.height]
  end
end
like image 169
Alexei.B Avatar answered Oct 12 '22 11:10

Alexei.B


I'm not sure if this will work but I think you can dynamically add versions to your uploader model.

I tried this method on my uploader and was able to define a new version on the uploader just by calling the method.

def self.defind_version(version_title, width, height)
  version version_title do
    process :resize_to_limit => [width, height]
  end
end

So then you can work that method call into a create hook of the db table that makes the list of versions.

Just an idea, I would test heavily before going to production.

like image 20
Polygon Pusher Avatar answered Oct 12 '22 10:10

Polygon Pusher