0

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 :)

3
  • @u_mulder Hes not using short tags so your response shouldn't use them either otherwise it might cause undue confusion. Also he needs data:image/png;base64,<?php echo base64_decode($user->getPicture()); ?> of course the mime type need to be relevant tot he image.. ieimage/jpeg or whatever it actually is. Commented Jan 2, 2015 at 18:25
  • The URI should look like src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." - you are missing the data:image...(etc.) part it seems? Commented Jan 2, 2015 at 18:25
  • @prodigitalson : I already put data:image/png;base64,<?php echo base64_decode($user->getPicture()); ?>, but my image did not displaying Commented Jan 2, 2015 at 18:35

1 Answer 1

5

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 ?>" />
Sign up to request clarification or add additional context in comments.

7 Comments

I already put data:image/png;base64,<?php echo base64_decode($user->getPicture()); ?>, but my image did not displaying
yes, but you're DECODING, which means you're stuffing the raw binary data into the data uri.
sorry, what do you mean? I feel confused here
your already base64-encoded your image to insert it into the db. so you don't have to do ANYTHING... just take the text you selected from the db and put it into the data uri. base64_decode() will return your image to raw binary garbage, which CAN'T be used in a data uri
I already try data:image/png/base64;<?php echo $user->getPicture();?>, but the picture still do not showing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.