How do I tell paperclip to not save the original file?

风格不统一 提交于 2019-11-26 20:49:43

问题


How do I tell Paperclip not to save the original file when it is uploaded? Or even better, to store a scaled version of the file as the original?


回答1:


I believe that you can simply define a style for :original to have paperclip replace the original with that size.

:styles => { :original => '300x168>', :cropped_thumb => {:geometry => "115x70#", :jcrop => true}, ...}



回答2:


Cris G's solution may be nice at most simple cases but it has limitations. consider that: style :original Paperclip process first of all others, so after that updated :original image (much smaller now) will be the source for following processing. Hence you are forced to keep :original style as best-resolutioned. The situation comes worse as you need to crop images with processor: that is the situation where you really need for real original quality. )

So I would recommend you somewhat raw (need to find out how to get every attachments of the model) solution:

after_save :reprocess_attach

private

def reprocess_attach
    if self.<atch_name>.present? && Pathname.new(self.<atch_name>.path).exist?
        self.<atch_name>.save
        File.unlink(self.<atch_name>.path)
    end
end

it doesn't care about what processing was behind the stage. It just kills original file )




回答3:


Paperclip always saves an original by default, but I 'believe', that if you just remove it from your migration then it will not try and save it.

I save a scaled original on my model so that users can edit their image later. My model looks like this :

:styles => { :cropped_thumb => {:geometry => "115x70#", :jcrop => true}, :resized_thumb => {:geometry => "115x70>"}, :deal => {:geometry => "64x56#"},  
:cropped_large => {:geometry => "#{PHOTO_IMAGE_WIDTH}x#{PHOTO_IMAGE_HEIGHT}#", :jcrop => true},
:resized_large => {:geometry => "#{PHOTO_IMAGE_WIDTH}x#{PHOTO_IMAGE_HEIGHT}>"},

:orig => '300x168>',  #this is the scaled original that I call later


:cropped_orig => {:geometry => '300x168#', :jcrop => true},
:resized_orig => {:geometry => '300x168>'} },
:processors => [:jcropper]


来源:https://stackoverflow.com/questions/3219787/how-do-i-tell-paperclip-to-not-save-the-original-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!