That could work... only if the parent script (the one calling the image script) could know which router are involved.
Right now, only the image script knows that.
I could know this information in the main script, but only if I queried the database many times more than I actually do.
Duplicating queries is not much of an option, as it will charge the server too much.
Here is what I thought of:
Main.php
$tmpfname = tempnam ("/", "FOO");
// the image script will write in the temporary file like this:
echo '<img src="image_circuit.php?circuit_id='.$id.'&tmpfname='.$tmpfname.'">';
include($tmpfname);
unlink($tmpfname);
image_circuit.php
$handle = fopen($tmpfname, "w");
// do the db queries, and make the image...
fwrite($handle, "writing to tempfile");
fclose($handle);
Only problem: the GET request made for getting the img (and that in fact runs the image script) is run concurrently to the main script, and the main script can finish before the image script is executed: then the temp file will have been destroyed before it has been used.