Resizing images on the fly using PHP Own stuff

I'm currently working on the new Twaweza website (not online as I write this), and needed a library to resize images on the fly with PHP, without having to store the resized images.

Being a typical lazy coder, I looked around for an existing library and found a useful start in Simon Jarvis' library for resizing images on the fly. It almost served my needs, besides the ability to resize images to fit inside a user defined box.
So I added the function resizeToMax.

The updated file can be found in my dropbox. Obviously, change the extension to php to use it.

Update (March 2010): I also included a resizeToMin function which resizes an image so that it fits over the passed box size. This is the same as what Flickr uses for their square thumbnails.