PHP – Save base64 Encoded string – Convert base64 to Image

PHP – Save base64 Encoded string – Convert base64 to Image

In this Post We Will Explain About is PHP – Save base64 Encoded string – Convert base64 to Image With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Convert base64 to image file in PHP Example

In this post we will show you Best way to implement Save Base64 Encoded image to file using PHP, hear for Save a PNG image from a base64 data string with PHPwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

When we are handling any PHP API for web-apps, they mostly all the images path provides the image in Base64 encoded source code, so we need to simple we server store or save base64 encoded image to images folder to server as a image or file.

Read Also:  Auto Post on Facebook page with php using Graph Api

Step 1 : Base64 POST Image

If we are getting the simple image source_data with image type following latest code as below:

$source_data = $_POST['image'];
$source_data = 'data:image/png;base64,AFFAFFFEEe8LKS346fj42Pj4...';

Step 2 : Base64 images String Replace

We need to simple parse base64 function part from this image source_data following latest code below:

$source_data = str_replace('source_data:image/png;base64,', '', $source_data);
$source_data = str_replace(' ', '+', $source_data);

Step 3 : base64_decode function

And then, decode the image source_data using base64_decode function following latest code below:

Read Also:  Free PHP, HTML, CSS, JavaScript editor - Codelobster IDE

$source_data = base64_decode($source_data);

Step 4 : file_put_contents

And then, we can put this image source_data to your desired file using file_put_contents function like below:

$data_file = 'admin/images/'. uniqid() . '.png';
$file_success = file_put_contents($data_file, $source_data);

Step 5 : Save base64 images

If we want to simple image rotate the image and save base64 images

$source_data = base64_decode($source_data); //simple base64 any format decoded image source_data
$source_img = imagecreatefromstring($source_data);
$rotated_img = imagerotate($source_img, 90, 0); //simple images rotate with angle 90 degree here
$data_file = 'admin/images/'. uniqid() . '.png';
$data_savefile = imagejpeg($rotated_img, $data_file, 10);
imagedestroy($source_img);

Example

I hope you have Got What is Save a Base64 Encoded Canvas image to a png file using PHP And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.