PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 22, 2022

[FIXED] PHP imagecreatefromstring() doesn't work when i enter a base64 decoded value

 January 22, 2022     base64, php     No comments   

Issue

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;       
}

Solution

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


Answered By - Raxi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing