I think for portraits it does not matter if you hold the
db handle or not, they are not generally large enough to be
a real problem. Also
you said "I put $tmp_file in the SRC attribute of IMG tag",
which will not work since that will send a
"GET $tmp_file" which will ask for a non-existent url on
your server.
Have you tried checking whether the tmp file is corrupted
or not?