简体   繁体   English

如何丢弃原始图像文件并使用Paperclip和Rails 3保留调整大小?

[英]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.

相关问题 Rails和Paperclip ...不保存原始图像,只保存样式? - Rails and Paperclip… don't save the original image, just the styles? 如何告诉回形针不保存原始文件? - How do I tell paperclip to not save the original file? 如何将文件系统中的图像添加到Rails中的Paperclip模型? - How do I add an image from my file system to a model with Paperclip in Rails? Rails回形针-上传原始图像和缩略图 - Rails paperclip - Upload original image and thumbnail Rails回形针图像到AXSLX文件 - Rails Paperclip image to AXSLX file 如何改善内部的Rails / Paperclip图像压缩或ImageMagick / Rmagick? - How do I improve Rails / Paperclip image compression inside or ImageMagick / Rmagick? Rails:如何从不相关的控制器上将Paperclip图像附件上传到数据库+强大的参数要求 - Rails: How do I upload Paperclip image attachment to a DB from an unrelated controller + Strong params requirement 使用Rails和Paperclip进行裁剪时,如何获取图像的顶部区域? - How do I get the top region of an image when cropping with Rails and Paperclip? 如何使用回形针将嵌入式图像附加到Rails 4.2邮件程序? - How do I attach an inline image to a rails 4.2 mailer using paperclip? 删除附加到其上的对象后,如何保存回形针图像? - how can I keep a paperclip image after deleting an object it is attached to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM