The problem is that your script requires too much memory to rotate an image that large. Exactly how much memory is "too much memory" will depend on the server configuration. You can set the memory limit in the PHP.INI file. 32MB is pretty much memory. I think the default is 8MB.
I'm not certain, but I don't think you can predict just how much memory imagerotate() will require to rotate a given image. It's certainly not a simple function of image dimensions. You could try to ball park some rough upper limit to rotatable image dimensions based on the 'pixel area' of the image (i.e., the width_in_pixels * height_in_pixels of the image). You would try progressively smaller images until it succeeds and then use that as your upper bound.
As for avoiding a fatal error, I'm not sure that will be possible. Running out of memory is a fairly nasty situation. If you're using php 5 or higher, try wrapping the imagerotate() call in a try/catch block.
try {
imagerotate($blah, $blah);
} catch (Exception $e) {
echo 'oops...it did not work ';
}