How to create GD Image from base64 encoded jpeg?

33,841

How can I create a GD resource from that again so that I actually can save that image as a file?

Are you sure you need to do that extra step? How about:

file_put_contents('MyFile.jpg', base64_decode($_POST['MyFormField']));
Share:
33,841

Related videos on Youtube

The Surrican
Author by

The Surrican

Updated on July 09, 2022

Comments

  • The Surrican
    The Surrican almost 2 years

    I have an upload Api that as a response object delivers (along with other stuff inside a Json object) a base64 encoded jpeg image.

    I create the encoded image as so:

    $im; // gd image resource
    ob_start();
    imagejpeg($im);
    $data = base64_encode(ob_get_clean());
    

    The data then is put inside a form field using javascript and submitted.

    How can I create a GD resource from that again so that I actually can save that image as a file?

    Everything in PHP.

  • The Surrican
    The Surrican over 13 years
    yeah, looks the best way ;) my mind was somehow blocked :D +1 but gonna accept the other answer because it was a directa answer to my q
  • Vilx-
    Vilx- over 13 years
    @Joe Hopfgartner - Harrumph! Well, a Happy New Year to you anyway, whenever that is for you. :D
  • Dennis
    Dennis over 9 years
    sometimes this step doesn't cut it. TCPDF some software I use chocked on this but worked with GD