I've never tried this, but my first guess would be to use ImageCreateFromString() instead, as to prevent too much writing on disk. I'm not sure if it would accept JPEG images, or database data would have to be altered in some way.
Also, you should reconsider "tmpimg.jpg". What if two users call this in the same time? The name should be unique. Check tempnam() in manual.
Wouldn't it be easier to keep those images in directory instead of database and then just passing them through with either <img src="/images/$name.jpg"> or with <img src="/images/image.php?f=$id">?
image.php:
// get image name for $id in variable $name
header("Content-type...");
readfile("path/$name.jpg");