MiniMagick Gem: How to Transform Images Using Ruby

If you work with any kind of images in your Ruby application, there is a good chance that you’ll want to change them in one way or another.

Like resizing them to save space & faster download for your users.

But how can you do this?

MiniMagick is a gem that can help you.

It’s an interface between the ImageMagick program & your Ruby code.

It helps you apply all sorts of transformations to your images to customize them to your needs!

First:

You need to install ImageMagick using your operating system package manager, or downloading the installer from the project’s website.

Then with mini_magick you can use regular Ruby methods to make changes to your images.

Let’s see some examples!

Opening an Image

You can open an image in two ways:

  1. MiniMagick::Image.open => Makes a copy of the image
  2. MiniMagick::Image.new   => Changes the original image

The open method takes both a file name & a URL, so you can pull images directly from the web.

I’m going to use this image from Unsplash, a free photo sharing site.

Minimagick Example Image

You can load the image like this:

require 'mini_magick'

image = MiniMagick::Image.open(
  "https://images.unsplash.com/photo-1516295615676-7ae4303c1c63"
)

Now that you have the image loaded you can get information about it, like its size, dimensions, format, etc.

Here’s how:

image.dimensions
# [3963, 5944]

image.type
# "JPEG"

image.human_size
# "20.7663MB"

This is a HUGE image, so let resize it!

Also, I would like to rotate it so it’s horizontal instead of vertical.

Resize & Rotate

You can resize an image like this:

image.resize "500x500"

This gives you an exact dimension, but if you want to scale the image to a percentage while keeping the aspect ratio intact…

You can do this:

image.resize "25%"

This is a resize to 25% of the original size, not a resize BY 25%.

Now:

You need to apply the changes by writing the file back to disk.

Here’s how:

image.write("/tmp/new_image.jpg")

If you opened the image for direct modification (using new, instead of open) then you don’t need to use write.

How to Crop an Image

You can crop parts of an image to remove things you don’t want & make it smaller.

The syntax is this:

<width> x <height> +<xoffset> +<yoffset>

For example, if you want to cut the lower half of an image:

image.crop "100%x50%+0+0"

For a vertical crop of half the image:

image.crop "50%x100%+0+0"

You can play around with these numbers until you find the crop you’re looking for.

Creating a Rounded Image

If you’re looking to make your image rounded, then you’ll need to combine a set of methods.

Like this:

MiniMagick::Tool::Convert.new do |img|
  img.size '3900x5000'
  img <<   'xc:transparent'

  img.fill "apple.jpg"

  img.draw "translate 2000, 2500 circle 0,0 2000,0"
  img.trim

  img << 'circle.png'
end

This creates an empty canvas of the given size, which should be the size of the image that you want to round.

Then:

This canvas is filled with the image, a circle is drawn in the middle, and everything around the circle is removed.

Finally, the image is saved as “circle.png”.

Notice that translate 2000, 2500 are the coordinates for the middle of the canvas.

While the 2000 in circle 0,0 2000,0 is the radius of the circle.

How to Add a Border With MiniMagick

Adding a border is the easiest thing in the world with mini_magick.

Example:

img.border 10

You can set the color like this:

img.bordercolor("white")

Don’t forget to write your changes if you’re using open instead of new.

Image Optimization

Images can be optimized beyond making them smaller.

For example, you can use the strip method in mini_magick to remove metadata.

Like this:

image.strip

If you want extra optimization you can use a gem like image_optim.

Like this:

require 'image_optim'

image_optim.optimize_image!('orange.jpg')

Watch Video Tutorial

[responsive_video type=’youtube’ hide_related=’0′ hide_logo=’0′ hide_controls=’0′ hide_title=’0′ hide_fullscreen=’0′ autoplay=’0′]https://www.youtube.com/watch?v=ZowvjxNHbIg[/responsive_video]

Summary

You have learned how to change images (rotate, resize, crop) using the mini_magick Ruby gem!

Thanks for reading.