Magic Image

부분 유료
분류별 chr15m | 업데이트됨 2달 전 | Media

7.8 / 10

지연 시간


서비스 수준


모든 자습서로 돌아가기 (3)

Common image manipulation tasks with Magic Image API

Here are some examples of common image manipulation tasks you can use ImageMagick for, using this API.

To run these examples on the Magic Image API you need two fields. The first field is the image data which is a multipart encoded form field. The second field is the args field which contains the ImageMagick arguments for the command you want to run. The output filename is set using the last component of the request URL.

We’ll use the following reference image by Dave Pollot so you can compare.

Star Wars painting reference

Convert PNG to JPG

The output file type is set with the filename you append to the request URL. So if you want to convert from PNG to JPG simply use a filename that ends with .jpg (e.g. output.jpg) and pass the special args value -convert.

url = ""
args = "-convert"

Convert JPG to PNG

If you want to output a PNG file instead of a JPG simply set the filename to one that ends with .png (e.g. output.png) like this:

url = ""
args = "-convert"

Convert GIF to JPG

If you want to convert an animated GIF you need to specify an extra argument -flatten. This is because GIFs have multiple frames, so you want to combine them into a single image:

url = ""
args = "-flatten"

Change image quality

Reducing the image quality is one way to shrink the weight of an image so it uses less bandwidth. Use -quality in the args value to change the quality of the image that is output. This example brings the JPEG quality down to 10% (the image is small but has visible artifacts):

url = ""
args = "-quality 10"

JPEG quality 10%

Resize an image

You can resize an image using the -resize argument.

Here we resize to a maximum of 64 pixels. This applies to both width and height, whichever is larger will be shrunk to 64 pixels.

args = "-resize 64"

Resize to max 300 pixels

You can also specify both a horizontal and vertical limit and the resize will fit within both. This command will resize to 64 pixels because the input image is a square.

args = "-resize 128x64"

You can also force the image to a particular size, stretching it to fit and distorting the image:

args = "-resize 128x64!"

Resize with stretching

You can fit to the smaller size and crop any overlap with the caret ^ character:

args = "-resize 128x64^"

This handy command will center the image in a 100x100 square, scale it so the shortest side is 100 pixels, and then crop it to 100x100:

args = "-resize 100x100^ -gravity center -extent 100x100"

Blur an image

You can blur an image using the -blur command. Specify the number of pixels to blur by:

args = "-blur 10x10"

Blurred image

Colorize an image

You can colorize an image using the -colorize command. Use the -fill argument to specify the color and then specify the percentage to color by.

Let’s make the image 25% redder:

args = "-fill #ff0000 -colorize 25"

Colorized image

Flip or mirror an image

To flip an image vertically use -flip:

args = "-flip"

Flip vertically

To flip an image horizontall use -flop:

args = "-flop"

Mirror horizontally

Rotate an image

To rotate an image use -rotate. Let’s rotate the image by 45 degrees clockwise:

args = "-rotate 45"

Rotate the image by 45 degrees

Combining multiple commands

You can combine multiple image manipulation operations into one command. You can chain them together simply by adding them one after another in the args value. For example to mirror an image horizontally and then colorize it you can use the following args value:

args = "-flop -colorize 0,100,0"

등급: 5 - 투표: 1