When saving an image to Amazon S3 etc. with Active Storage, upload the image after reducing it.
--Environment - Rails 6.0.3.3 - Active Storage、image_processing、libvips
Before saving, replace the tempfile
of the attached image (ʻActionDispatch :: Http :: UploadedFile`) with a compressed version.
class Event < ApplicationRecord
has_many_attached :images
end
<div class="form-group">
<%= f.label :images %>
<%= f.file_field :images, multiple: true, class: 'form-control-file' %>
</div>
@event.images.each do |image|
image.tempfile = ImageProcessing::MiniMagick.source(image.tempfile).resize_to_fit(1280, 1280).call
end
@event.save
Recommended Posts