How To: Resize and Crop images in grails

with No Comments

Hello everybody, today I want to describe how to crop/resize an images in Grails. There are 2 best ways how to do it:

  1. Using plugin burning-image(use JAI or ImageMagic)
  2. Using ImageMagick 

1. Burning-image

You can find this plugin here.

That use it you just need to inject burningImageService bean to your service:

def burningImageService

ok, good)
Currently I’ll show you a code for resize an image:

where

  • file – it’s your image represented as java.io.File or as path to your file as string.
  • result dir – path where you will find a new file
  • x and y it’s new width and height

And here it is code for crop:

where: x,y,x1,y1 – coordinates of croping
That’s all with burning-image, you can find more functionality here

2. Image Magic

It required installed image magick. You can find it here

Example will be for for Unix systems.

ProcBuilder – class for run cmd commands from java app.

In this example we resize and crop image.
so first we call imagemagic by convert,
after we mark that we will resize image -resize with params "${width}x${height}"width and height separated by ‘x’
later we will crop image(crop result after resize).
"-crop", "${x1}+${y1}+${x2}+${y2}" x1,y1,x2,y2 – dots of area separated by ‘+’

Thats all, thank you for attention, ask question in comment if you have:)

Leave a Reply