Reputation: 11
I'd like to store some photos in MySql as blobs and would like to be able to retrieve the binary and recreate images from it to display back to the user.
Everything I've search uses some combination of file_get_contents
, Base 64 encoding and the GD library. But every post I come across has a different requirement than what I'm trying to do and so the code examples aren't that helpful.
Can someone tell me what function calls I need to make or give me an order of operations for what needs to be done. Say I have the following code:
$someBlob = getImageBlobByImageId(1203);
Say this blob represents an image named "foo.jpg". How do I go from $someBlob
to foo.jpg
so I could put it in HTML like
<img src="<?php echo $fooImage; ?>"/>
Any hints or nudges in the right direction are greatly appreciated ;-)
Upvotes: 1
Views: 2602
Reputation: 732
Your HTML needs to point to a PHP script that retrieves the blob and sends it back on request as an image.
For example, in your HTML:
<img src="getImage.php?id=1203" />
And then, for getImage.php:
<?php
$id = $_REQUEST['id'];
header("Content-type: image/jpg");
echo getImageBlobByImageId($id);
exit();
?>
Be sure not to have the script output any content other than the header line and the blob data, or you won't get what you want.
Upvotes: 3