Search code examples
ruby-on-railsrails-activestorage

Rails api active_storage


Is there a way to create variants of images upon upload and store the variants rather than the entire image.

for example. a user uploads a picture that is 1000x1000, when I receive that upload in rails I want to create 3 variants, a 100x100 a 300x300 and a 600x600 and store those in S3 then be able to retrieve them by doing something like post.image.thumbnail

It seems silly that everytime an image is request I must retrieve the full sized image from s3 and process it on the fly to serve it to the user.


Solution

  • I think you are looking for minimagick

    add this to your gemfile

    gem 'mini_magick'
    

    and then you can use minimagick methods to transform the image

    <%= image_tag user.avatar.variant(resize: "100x100") %>
    

    here is the documentation for this