More GrailsKit work – resizing user profile images

Date May 26, 2008

Just a quick note on what I’ve been working on with GrailsKit.  I’m currently putting together a “user profile” page, which allows a user to edit their own profile.  Nothing too fancy, but I’m learning as I go how best to do this in Grails.  Coupla new points learned doing this (it’s not done yet – not much time to devote given all other commitments right now!)

First point – image resizing for thumbnails.  Dead simple in PHP – lots of pre-existing code examples, many classes, and all using the ‘built-in’ (need to be compiled in, of course) GD functions.  In Java/Grails, not so simple.  I feel like I always hit walls with Java – 18 months ago I was trying to do basic sound stuff with Java and had to come to the conclusion that there’s no decent cross-platform way of using a microphone with Java.  They (Sun) just seem to have given up on desktop stuff, but “the server’s where Java shines”, or so I keep getting told.  Well, most(?) apps today are web-based, and doing basic image manipulation shouldn’t be as hard as it is in Java.

This article (from late 2005!) outlines the problems of trying to use just native Java for image resizing.  It’s a huge memory hog, and is slow.  I also looked at the Grails ‘image tools‘ plugin but it gave horrid, unusable results.  They were the same as back in 2001 when I was trying to do 16 million color pics with GD but had it compiled to not deal with ‘true color’ images – everything was shifted down to 256 colors or less.  That’s what the imagetools plugin did for me.  Could I have set some parameters to change its behaviour?  Perhaps, but there was not documeted examples of what I should be able to switch to get it to work as I expected out of the box. So … I simply exec out to ImageMagick, as described in the first link above.  It’s not blazingly fast, but fast enough for me for now, and ‘just works’.

And with this comes my second ‘breakthrough’ – Services in Grails.  I originally had the thumbnailing/imagemagick exec code in a controller during my initial tests, then decided to make it in to an ImageService file.  This file has a method which will take a ‘multiPartFile’ file object (representing an uploaded file), thumbnail it based on parameters in the basic Config.groovy file, the write it to the /web-app/image directory.  Lots of work can still be done on this file – better method name, better docs, better error handling and so on.  But it works, and worked as I expected it to.

Big deal, right?  Well, not particularly, but because I made this a ‘Service’ file in the ‘/grails-app/services’ directory, it’s automatically available to add as a class property in my controllers.  So rather than specifically having to import the Service, it’s available automatically in any Controller.  Bit of black magic, no?  :)   I was a bit hesitant to use this approach at first, but I think that was mostly out of ‘fear of the unknown’ so to speak.  This seems as good a convention as any, at least for now.

Continuing to grow those Grails muscles, little by little.  This is probably second nature to many Grails devs already, but for anyone (else) new to this scene, perhaps this will have helped give you another idea of how to organize your code.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • TailRank
  • YahooMyWeb
  • del.icio.us
  • DZone
  • Facebook
  • Reddit
  • StumbleUpon
  • Digg
  • Simpy
  • Technorati

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">