在 Paperclip 中调整原始图像的大小 [英] Resize original image in Paperclip

查看:58
本文介绍了在 Paperclip 中调整原始图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Paperclip 将原始图像存储在原始"文件夹中.有没有办法调整原始图像的大小?为了节省光盘空间,我想把原稿做得更小.

Paperclip stores original images in "original" folder. Is there a way to resize the original images? I want to make the originals smaller in order to save the disc space.

因此,例如,如果访问者上传了一张 2592x1936 的照片,我想将其存储为 1024x1024,就像我们在 :styles 中设置 :thumb 图像的尺寸一样

So, for example, if visitor uploads a photo with 2592x1936 I want to store it as 1024x1024, the same way we set the dimensions for :thumb images in :styles

更新(已解决)

我发现了如何在上传时自动调整原始图像的大小.一个只需要在样式中添加:original:

I found out how to resize original images automatically on upload. One just needs to add :original to styles:

class MyModel < ActiveRecord::Base
    has_attached_file :photo, 
        :styles => { :original => "1024x1024>", :thumb => "150x150>" }
end

推荐答案

我不确定回形针是否会自行调整大小.您可能需要查看 Rmagick 才能完成此操作.我会尝试让 RMagick 运行(或 minimagick),然后使用 before_save 回调来执行 您编写的 :resize 方法告诉 RMagic 调整图像大小.您的方法可能如下所示:

I'm not sure paperclip does resizing by itself. You might have to look at Rmagick to get this done. I would try to get RMagick going (or minimagick) and then use a before_save callback to execute a :resize method that you write that tells RMagic to resize the image. Your method might look like:

class Image < ActiveRecord::Base
  belongs_to :profile
  before_save :resize

  def resize
    self.image = self.image.resize "1024x1024"
  end
end

class Image < ActiveRecord::Base
  belongs_to :profile
  before_save do
    self.image = self.image.resize "1024x1024"
  end
end

这篇关于在 Paperclip 中调整原始图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆