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!
You need to install ImageMagick using your operating system package manager, or downloading the installer from the project’s website.
mini_magick you can use regular Ruby methods to make changes to your images.
Let’s see some examples!
You can open an image in two ways:
MiniMagick::Image.open=> Makes a copy of the image
MiniMagick::Image.new=> Changes the original image
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.
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.
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.
You can resize an image like this:
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:
This is a resize to 25% of the original size, not a resize BY 25%.
You need to apply the changes by writing the file back to disk.
If you opened the image for direct modification (using
new, instead of
open) then you don’t need to use
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:
For a vertical crop of half the image:
You can play around with these numbers until you find the crop you’re looking for.
If you’re looking to make your image rounded then you’ll need to combine a set of methods.
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.
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".
translate 2000, 2500 are the coordinates for the middle of the canvas.
circle 0,0 2000,0 is the radius of the circle.
Adding a border is the easiest thing in the world with
You can set the color like this:
Don't forget to
write your changes if you're using
open instead of
Images can be optimized beyond making them smaller.
For example, you can use the
strip method in
mini_magick to remove metadata.
If you want extra optimization you can use a gem like
require 'image_optim' image_optim.optimize_image!('orange.jpg')
You have learned how to change images (rotate, resize, crop) using the
mini_magick Ruby gem!
Thanks for reading.