我在Ruby on Rails应用程序中使用Shrine来创建调整图像大小并将图像上传到存储的过程。
我当前的代码是:
image_uploader.rb
require "image_processing/mini_magick"
class ImageUploader < Shrine
plugin :derivatives
Attacher.derivatives_processor do |original|
magick = ImageProcessing::MiniMagick.source(original)
{
resized: magick.resize_to_limit!(120,120)
}
end
end
user.rb
class User < ApplicationRecord
include ImageUploader::Attachment(:image)
before_save :image_resize
def image_resize
self.image_derivatives!
end
end
我在阅读official documentation时实现了它,但这在两种方式中是不可取的。
- 需要模型代码中的触发器。只能用
image_uploader.rb
完成吗? - 要访问使用此代码生成的图像,需要使用“调整大小”前缀(例如
@user.image(:resized).url
),并且原始图像也将保留在存储中。我要处理原始图像本身。
在解决这两个问题的同时可以上传吗?