I use Shrine in a Ruby on Rails application to create the process of resizing and uploading images to storage.
My current code is:
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
I implemented it while reading the official documentation, but this is not desirable in two ways.
- Requires trigger in model code. Can it be completed with only
image_uploader.rb
? - Access to images generated with this code requires a "resized" prefix(e.g.
@user.image(:resized).url
), and the original image will also remain in storage. I want to process the original image itself.
Is there a way to upload while solving these two issues?