The random file part I've already coded, but the code you've posted is interesting as I've been interested in writing a script for another purpose that returns not only the image, but a clickable image that can go to a link.
The hard part about this project is I don't know how to do an overlay of a graphic over the resulting table of graphics, which I've already got working. I've read enough to know it can be done with CSS and two divs, one overlaying on the other, but I don't know how to do that.
I'm confused...you can't return a clickable image using the php script.