Resize Images with Active Storage in Rails
Tags: rails
Published: 2021-01-06 14:48:45

{% youtube pLCYFVd0tFo %}

We used to have to use gems like Carrierwave & Paperclip to upload files & manipulate images in Ruby on Rails. However we now have a fantastic library built into Rails called [Active Storage](https://edgeguides.rubyonrails.org/active_storage_overview.html#transforming-images) which handles that for us.

## Setting up Active Storage

The first thing you need to do is run the install script, which will create a migration to add the Active Storage tables to your database.

```bash
$ rails active_storage:install
$ rails db:migrate
```

## Uploading a file

All Active Storage upload need to be connected to a model. To tell rails "We'd like to have a method called `avatar` on this model, which is a file attachment" we use the method  `has_one_attached` method like this:

```ruby
class Post < ApplicationRecord
  has_one_attached :avatar

  validates :title, presence: true
end
```

From there, we can pass a file object to that field & it'll save to the database.

## Resizing an Image

The awesome thing about Active Storage, is we can create new versions of an image on the fly. This means Rails won't perform the resize until the user requests that image, and after the transformation is complete it will cache the results for future use.

The documentation is a little unclear as to what options as can passed into the `variant` method, but I found the arguments link to the methods found on the [ImageProcessing::MiniMagick Documentation](https://github.com/janko/image_processing/blob/master/doc/minimagick.md).

Here is a cheat sheet I put together for transforming images:

```erb
<!-- Change the format of the image -->
<%= image_tag @post.avatar.variant(format: :jpg) %>

<!-- Rotate the image 45° -->
<%= image_tag @post.avatar.variant(rotate: [45, background: '#0F0']) %>

<!-- Saver - compress the image -->
<%= image_tag @post.avatar.variant(saver: { quality: 10 }) %>

<!--
  resize_and_pad: Image will be resized to 500x500 while keeping the original aspect ratio.
  Any remaining space (e.g. if the image was originally 50x10, so will scale up to 500x100) will be filled in with the colour we pick in 'background'
  We use Gravity to define where the left over space will be, so "north" will anchor the original image at the top of the new resized image.
-->
<%= image_tag @post.avatar.variant(resize_and_pad: [500, 500, gravity: 'north', background: '#000']) %>

<!--
  Resizes the image to fill the specified dimensions while retaining the original aspect ratio.
  If necessary, will crop the image in the larger dimension.
-->
<%= image_tag @post.avatar.variant(resize_to_limit: [500, 500]) %>

<!--
  Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image.
-->
<%= image_tag @post.avatar.variant(resize_to_fill: [500, 500, gravity: 'north']) %>

<!--
  Resizes the image to fit within the specified dimensions while retaining the original aspect ratio.
  Will downsize the image if it's larger than the specified dimensions or upsize if it's smaller.
-->
<%= image_tag @post.avatar.variant(resize_to_fit: [500, 500]) %>
```

## References

- [active_storage/variant.rb](https://github.com/rails/rails/blob/master/activestorage/app/models/active_storage/variant.rb)
- [Episode Source Code](https://github.com/MikeRogers0-YouTube/ActiveStorage-Image-Resizing)