If you are looking for good/saner alternatives to ImageMagick and GD, maybe consider looking at Imager. It provides a smaller featureset than ImageMagick, but with a saner and more stable API. I won't post any code, as the documentation is quite extensive.