Image::Magick is a Perl interface into libmagick. You can get it from CPAN (or click the link to the left). It will work with any image format that libmagick supports (GIFs are a little funny these days. I don't remember if they'll work or not).
The code example will (should) work if you change the '\' to '\\', since you need to escape slashes. Or, you could use single quotes instead. Doing something with the returned values is up to you. If you're going to discard them, you could just leave the variable off.
--Chris
Updated: I was thinking that ImageMagick used GD, but it doesn't. It's a standalone library for image manipulation. GD is for doing plots and graphs. Sorry about that.