开发者

Attachment_fu: how can I selectively prevent thumbnail creation?

开发者 https://www.devze.com 2022-12-16 03:45 出处:网络
I\'m happily using attachment_fu to handle file uploads and thumbnail creation.However, for some (but not all!) cases I would like to suppress the creation of thumbnails.

I'm happily using attachment_fu to handle file uploads and thumbnail creation. However, for some (but not all!) cases I would like to suppress the creation of thumbnails.

How would I hack attachment_fu to do this?

Specifics: I have a traditional attachment_fu model

class Pic < ActiveRecord::Base
  has_attachment :content_type => :image, :storage => :s3, :resize_to => '200x600>',
                 :thumbnails => { :thumb48 => [48,48], 
                                  :thumb32 => [32,32], 
                                  :thumb22 => [22,22] }
  validates_as_attachment
end

The user specifies the file to be uploaded in a form using a file input, and submits the form to an action where the pic is created using

@pic = Pic.new(params[:pic])

In certain cases I'd like to be able to do something like

@pic = Pic.new(params[:pic], {:generate_thumbnails => false})

and prevent the t开发者_开发百科humbnails from being generated.


interesting question.

have you thought about just not displaying the thumbnails for certain pics? or is storage a concern?

another option would be to create two models - one called Pic which doesn't define any thumbnails and then one called PicWithThumbs, which extends the Pic class, and does define thumbnails.

Then in your controller you could do an 'if' statement that checked the params for a value called 'create_thumbs' (boolean) - if :create_thumbs is true, then create an instance of PicWithThumb, else create a Pic

I know, it smells a little, and I'm a bit of a noob, so feel free to shoot me down. I'm interested in seeing what the best solution is though...


I'm not sure about attachment fu, but in paperclip you can stop thumbnail generation by returning false in before_post_process, in attachment fu perhaps you could do a similar thing in the process_attachment callback?

0

精彩评论

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

关注公众号