Due to the nature of the client-server communication in HTTP there is no "simple" way to do that.
The best practice would be to store the image in a temporary directory and then to either store a timestamp with the image and run a job that deletes all images older than 30 minutes (or whatever timeout you want) or link the image to the user-session and check if the session still exists - if the session dies for whatever reason, delete the image.
You can set session-handlers with
bool session_set_save_handler ( callback $open , callback $close , callback $read , callback $write , callback $destroy , callback $gc )
Use the destroy and gc handlers to delete the files.
You will have to call the script to define the session save handlers at least in every script that works with sessions. You could put the code in an include file and include it whenever needed, or use the auto_prepend_file directive in php.ini
If you are using jQuery you can bind to the beforeunload event:
$(window).bind("beforeunload", function() {
//send a request to delete the file
}
You could of course also use Javascript to send keepalive-requests to your server (using AJAX for example) and delete the image when those keepalives stop. That would probably be the most difficult and unreliable method and I would not recommend it.