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

Tuesday, October 11, 2022

[FIXED] How to create GD Image from base64 encoded jpeg?

 October 11, 2022     base64, gd, image, jpeg, php     No comments   

Issue

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.


Solution

You can use imagecreatefromstring() function:

$data = base64_decode($_POST['image_as_base64']);

$formImage = imagecreatefromstring($data);

"String" does not mean a "real" string. In that case it means bytes/blob data.



Answered By - user432219
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • 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