Home > Blockchain >  PHP imagecreatefromstring() doesn't work when i enter a base64 decoded value
PHP imagecreatefromstring() doesn't work when i enter a base64 decoded value

Time:12-31

So, I'm using the following code:

$currentIMG = imagecreatefromstring(base64_decode($img));

Where $img is a base64 string. When I echo base64_decode($img) it shows me a string (as it should), but when I use imagecreatefromstring() it shows the following error:

Fatal error: Uncaught Error: Object of class GdImage could not be converted to string

I can guarantee that the base64 string is not broken because it works on online base64 to image converters, so I can't figure out what's the problem.

Thanks in advance!

Code:

function base64toImage($img) {
    $currentIMG = imagecreatefromstring(base64_decode($img));
    echo $currentIMG;       
}

CodePudding user response:

Instead of doing:

echo $currentIMG;

Which tries to print a GDImage as if it is a string, use something like:

Header('Content-type: image/png');
imagepng($currentIMG);
  • Related