在Paperclip中调整原始图像的大小

Evg*_*nii 17 paperclip ruby-on-rails-3

Paperclip将原始图像存储在"原始"文件夹中.有没有办法调整原始图像的大小?我想缩小原件以节省光盘空间.

因此,例如,如果访问者使用2592x1936上传照片,我想将其存储为1024x1024,就像我们设置尺寸的方式一样:拇指图像:样式

更新(已解决)

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

class MyModel < ActiveRecord::Base
    has_attached_file :photo, 
        :styles => { :original => "1024x1024>", :thumb => "150x150>" }
end
Run Code Online (Sandbox Code Playgroud)

Cal*_*ods 2

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

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

  def resize
    self.image = self.image.resize "1024x1024"
  end
end
Run Code Online (Sandbox Code Playgroud)

或者

class Image < ActiveRecord::Base
  belongs_to :profile
  before_save do
    self.image = self.image.resize "1024x1024"
  end
end
Run Code Online (Sandbox Code Playgroud)