Premature end of script headers
this means that the script did not send any header information at all.
Odd.. I'll look into that a litte later, as today is winding down....
Anyway, have a look at the PHP manual, because I think you are trying to do the impossible.
You start the page by telling the browser that this is an image, but lateron
you print HTML code:
echo "<img src=\"$im\">";
Also, you should print the image before you destroy it.
I am sure you already did this, but try one of the samples from the manual and see if those work.