0

Possible Duplicate:
get image from base64 string

I tried

header('Content-Type: image/png');
echo base64_decode($data);`

But it doesn't work.

Community
  • 1
  • 1
Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106
  • a base64 encoded png. It works fine when I put `$data` in a data URI – Drew LeSueur Jun 01 '10 at 18:50
  • For example, Firefox says something like "The image cannot be displayed because it contains errors" – Drew LeSueur Jun 01 '10 at 18:54
  • Don't forget different browsers have different size limit, the only way to be sure it works is if the base64 is 32k or less (I don't mean the .PNG image, I mean the base64 output). – TravisO Jun 01 '10 at 19:24
  • I assume the extra backtick(`) in your code after the base64_decode(); is a typo? – Geek Num 88 Jun 01 '10 at 19:25
  • if you redirect `echo base64_decode($data);` to a file (image.png) can you open it all right? what is its size (both in bytes and in pixels)? – leonbloy Jun 02 '10 at 12:15

3 Answers3

3

A png data url looks like this:

data:image/png;base64,[actual data]

You have to cut the beginning to be able to base64_decode it.
Also, if you remove the header call, you will be able to see any error message your code outputs.

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
0

PHP has this magical function named imagecreatefromstring() too... But there is no need to use it if you need to show image only.

Anpher
  • 4,567
  • 24
  • 24
0

Thank you everyone for trying to help. The problem was I was that I forgot to url encode the query parameter before I sent it to the PHP script.

Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106