[英]How do I throw away the original image file and just keep the resizes with Paperclip and Rails 3?
I am using Paperclip to, among other things, allow a registered user to upload an avatar for use on thier profile. 除其他外,我正在使用Paperclip允许注册用户上传头像以在其个人资料上使用。 I want to store a big and small version of the image which will be rmagicked to standard sizes.
我想存储图像的大小版本,将其放大为标准尺寸。 Now, what i want to do, is store these two standard sizes (:normal and :tiny, for example.) but I do not want to store the :original.
现在,我想要做的是存储这两个标准尺寸(例如:normal和:tiny),但我不想存储:original。
This will be nice for several reasons as I will never display or use any version than the two standard (re)sizes. 由于以下几个原因,这会很不错,因为我永远不会显示或使用任何比两个标准(重新)尺寸大的版本。
I can't think of an way to do that with Paperclip directly, but you could remove the original manually after creating the record. 我无法想到使用Paperclip直接执行此操作的方法,但您可以在创建记录后手动删除原始文件。 An example could look like this:
示例可能如下所示:
class Photo
has_attached_file :photo
after_create :destroy_original
protected
def destroy_original
# photo.url will look something like /system/photos/1/original.png
File.unlink("#{Rails.root}/public#{self.photo.url}")
end
end
What is your reasoning for wanting to delete the files? 你想删除文件的理由是什么? File storage is so cheap now, that it's not really a valid reason anymore.
文件存储现在很便宜,它不再是一个有效的理由。
I would advise against deleting the original files. 我建议不要删除原始文件。 If you ever decide you want to resize the files using Paperclip's rake tasks, you will need the originals.
如果您决定要使用Paperclip的rake任务调整文件大小,则需要原始文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.