ActiveStorage resize image

Edit
equivalent
Public
Rails
ActiveStorage

Rails 7.1 Active Storage Variants - resize images options explained

 resize_to_limit
 resize_to_fit
 resize_to_fill
 resize_and_pad
 crop
 rotate 

https://guides.rubyonrails.org/active...

26 February 2024
Rails 7.1
Ruby 3.2


ActiveStorage resize image VIPS

<!-- 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]) %>