There's really 2 things you need to do:
1. Get the image dimensions so you know if a resize is needed.
2. Resize the image.
If you want, have a look in album.pl, at the getPhotoDimensions subroutine, which handles #1, and genThumb contains the code I used to resize the image for #2. Hopefully that's a good start!