As I see it, you link to a cgi instead of the jpeg, and when triggered, that code sends to stdout the exact same thing that you would have done to write the temporary, after sending the header (file type and blank line is enough, I think).
—John
Comment on Re: avoid temporary files when building a webmailer