Adding and Resizing Images with Grails

Here is a quick post on how to upload images within your Grails project to your file system(rather than your database). It seems simple enough, but I ran into a few snags as I was working on one of my projects. Just wanted to provided a working example for those that are entering the Grails territory for the first time. Happy Coding!

I am using the imageTools plugin which you can read more about here

NOTE: The imageTools plugin has been criticized for its low quality of output. ImageMagick may be a better fit for you project(s). My particular project didn’t call for high quality pictures. A quick google search for “imagemagick for grails” should get you started on your way.

I am using version 1.0.3 in the example below

to install, I ran the following command from my grails application’s root directory

grails install-plugin


class Picture {
byte[] imagefile
//Any other stuff you want to track


Controller Code for Saving an image

def save = {

def downloadedfile = request.getFile('imagefile')
def pictureInstance = new Picture(params)
def imageTool = new ImageTool()

if(downloadedfile &&{
String imagepath = grailsAttributes.getApplicationContext().getResource("images/").getFile().toString() + File.separatorChar + "${}.jpg"
downloadedfile.transferTo(new File(imagepath))


imageTool.writeResult(imagepath, "JPEG")
flash.message = "Picture ${} created"
else {

Code for displaying the image in both the ‘show’ and ‘list’ views

<td><img src="${createLinkTo(dir:'images', file:'.jpg' )}" /> </td>

5 thoughts on “Adding and Resizing Images with Grails

  1. Nice post Juan. I heard this post mentioned on the latest (February 1, 2009) Grails Podcast while driving to work this morning. That’s awesome.

  2. Thanks Roger, I am a little behind on the podcast. I am glad that this post is useful to others 😀

  3. thanks for this man .
    but i think putting the image to your image folder in the war is not a wise idea.. is there a way to do it in the machine file system and then read it ?

Leave a Reply

Your email address will not be published. Required fields are marked *