How to Upload a Picture Using Ruby
If you piece of work with any kind of images in your Ruby application, there is a skilful run a risk that you'll desire to change them in one way or another.
Like resizing them to save infinite & faster download for your users.
But how can yous practice this?
MiniMagick is a gem that tin help you.
It's an interface between the ImageMagick programme & your Ruby code.
It helps you use all sorts of transformations to your images to customize them to your needs!
First:
You lot need to install ImageMagick using your operating system package manager, or downloading the installer from the project's website.
Then with mini_magick
you tin use regular Ruby methods to make changes to your images.
Allow's see some examples!
Opening an Prototype
Yous can open an image in two ways:
-
MiniMagick::Image.open
=> Makes a copy of the image -
MiniMagick::Image.new
=> Changes the original image
The open
method takes both a file name & a URL, and then you tin pull images directly from the web.
I'one thousand 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 up( "https://images.unsplash.com/photograph-1516295615676-7ae4303c1c63" )
Now that yous have the image loaded you lot can become data nearly it, like its size, dimensions, format, etc.
Here's how:
prototype.dimensions # [3963, 5944] image.type # "JPEG" image.human_size # "20.7663MB"
This is a HUGE epitome, so let resize it!
As well, I would like to rotate it so information technology's horizontal instead of vertical.
Resize & Rotate
Y'all can resize an image like this:
paradigm.resize "500x500"
This gives you an exact dimension, but if you want to scale the image to a per centum while keeping the aspect ratio intact…
You can exercise this:
image.resize "25%"
This is a resize to 25% of the original size, not a resize BY 25%.
Now:
You lot need to utilize the changes by writing the file back to deejay.
Here's how:
image.write("/tmp/new_image.jpg")
If you opened the paradigm for direct modification (using new
, instead of open
) then you don't demand to utilize write
.
How to Ingather an Paradigm
You lot can crop parts of an epitome to remove things you don't desire & make information technology smaller.
The syntax is this:
<width> x <elevation> +<xoffset> +<yoffset>
For example, if you want to cut the lower half of an prototype:
image.crop "100%x50%+0+0"
For a vertical ingather of half the image:
image.crop "50%x100%+0+0"
You tin can play effectually with these numbers until you find the crop yous're looking for.
Creating a Rounded Prototype
If you're looking to make your image rounded, and then yous'll need to combine a set of methods.
Like this:
MiniMagick::Tool::Convert.new do |img| img.size '3900x5000' img << 'xc:transparent' img.make full "apple tree.jpg" img.draw "translate 2000, 2500 circumvolve 0,0 2000,0" img.trim img << 'circle.png' stop
This creates an empty canvas of the given size, which should be the size of the prototype that you want to round.
So:
This sail is filled with the image, a circle is drawn in the middle, and everything effectually the circle is removed.
Finally, the paradigm is saved as "circle.png".
Detect that interpret 2000, 2500
are the coordinates for the heart of the sheet.
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 earth with mini_magick
.
Example:
img.edge ten
You can set the color like this:
img.bordercolor("white")
Don't forget to write
your changes if you're using open up
instead of new
.
Paradigm Optimization
Images can be optimized beyond making them smaller.
For example, y'all can use the strip
method in mini_magick
to remove metadata.
Like this:
image.strip
If yous desire extra optimization y'all tin use a precious stone like image_optim
.
Like this:
require 'image_optim' image_optim.optimize_image!('orange.jpg')
Watch Video Tutorial
Summary
You have learned how to modify images (rotate, resize, crop) using the mini_magick
Ruby gem!
Thanks for reading.
Source: https://www.rubyguides.com/2018/12/minimagick-gem/
0 Response to "How to Upload a Picture Using Ruby"
Post a Comment