开发者

Passing a parameter to the uploader / accessing a model's attribute from within the uploader / letting the user pick the thumbnail size

开发者 https://www.devze.com 2023-04-12 04:02 出处:网络
I would like to crop an image to the size the user has selected from a list (e.g. 100x100px, 200x200px,...)

I would like to crop an image to the size the user has selected from a list (e.g. 100x100px, 200x200px,...) How would I pass that attribute to the uploader or get the model's attribute from within the uploader?

Accessing the model's attribute from within the uploader as following does not work:

version :thumb do
    thumbnail_size = model.thumbnail_size
    ...
    ...
end

I get following error:

undefined loca开发者_如何学Cl variable or method `model' for #

Thank you! Florian


In order to be able to access the model's attribute I had to add a manipulation helper.

class MyUploader < CarrierWave::Uploader::Base
  ...

  version :thumb do
    process :custom_thumbnail
    process :convert => 'jpg'
    ...
  end

  def custom_thumbnail
      width =  model.get_image_width     
      height = model.get_image_height

      manipulate! do |img|
        img.convert "#{width}x#{height}"
        img
      end
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消