Sorry, I just realised that your main issue is that you need the script to have a .jpg extension, yet still be parsed by PHP.
I think that you need to tell PHP to parse .jpg files in the httpd.conf file. Although this is probably pretty nasty.
I don't know, but maybe you could put a special case in the httpd.conf like ".rand.jpg" so that any files endidng with .rand.jpg will be parsed by PHP, but .jpg won't.