What is the best way to show images saved in Blob store in databases?
Concrete Example: I'm saving my images in MySQL on a Blob colum (let's call it 'image'). I'm using CakePHP (doesn't really matters)
That's is the avatar image of the users. I need to show it to them.
Now, what's the best way of doing that?
currently, i'm using a separate action "show_image/{user_id}" that reads the image from the database, set heade开发者_JAVA百科rs and print the content. So, if i want to display the image i just use:
<img src="show_image/3" />
show_image is something like:
function show_image($user_id){
$user = //read user from database
$image = file_get_contents($user['image']);
header('Content-type: ' . $user['image_type']);
header('Content-Disposition: attachment; filename="'.$user['image_name'].'"');
echo $image;
}
Now, maybe there are other ways (base64 maybe) or something that works better than this approach.
Thanks!
Instead of saving the image as a binary object, you should probably just upload the file to a location and store the location string in the DB. This will reduce the load on your DB as well as being generally much easier.
Ok, i've done the usual way as the question say.
Just added the header cache thing pointed by @pst.
精彩评论