g1smd - 12:08 pm on Sep 12, 2011 (gmt 0)
<img src="<?php print $imagename; ?>" border="2" />
Make sure that all of the logic that sorts out the image name (and checks that the file exists) is placed BEFORE the point where the script sends the HTML DOCTYPE.
You will want to send a 404 HTTP header if there is no image to serve on the current page. It is too late to send such a header after any part of the HTML page has been sent.