Monk
Monk

Reputation: 99

PHP : How to display image from blob

I have a little problem here. I try to convert an image into string base64, after that I want to save the string into blob in MySQL. So, the blob can be displayed on the mobile apps.

this is my code :

$data = file_get_contents($_FILES["picture"]["tmp_name"]);
$image = base64_encode($data);

I already successfully save the blob into MySQL, but I can not displayed the image in website.

<td> <img src="<?php echo base64_decode($user->getPicture()); ?>"></td>

because the result is : ������� and many more

Am I wrong ? Please correct me :)

Upvotes: 0

Views: 3695

Answers (1)

Marc B
Marc B

Reputation: 360572

The src attribute of an image MUST point at a url. You cannot dump the raw binary contents of an image in there and expect it to work. The browser will take that raw binary data and try to hit the page's originating server and request that data as if it was a file url. i.e. you have this on a page loaded from http://example.com/foo/bar/baz.php:

<img src="blahblahblahblah" />

which will result in the browser requesting

http://example.com/foo/bar/blahblahblahblah

If you want to embed your image in the page, then you have to use a Data URI:

<img src="data:image/jpeg;base64,<?php echo $base64_encoded_image ?>" />

Upvotes: 5

Related Questions