In this tutorial, i will show you how to convert base64 string to image and write it into folder in PHP. i will write simple code for save base64 encoded image to file using php and you can save it png, jpg as you want.
Many times you need to require to convert base64 string to image and save it to folder. If you are work with an API creation then it is very simple to work with base64 string and you need to convert base64 encoded string into image.
Here in this example i write function generateImage() from base64 string. you can simply follow bellow example:
public function generateImage($img)
$folderPath = "images/";
$image_parts = explode(";base64,", $img);
$image_type_aux = explode("image/", $image_parts);
$image_type = $image_type_aux;
$image_base64 = base64_decode($image_parts);
$file = $folderPath . uniqid() . '.png';
I hope it can help you....
Do you like below Tutorials ?
- Laravel 5.7 Image Upload with Validation Example
- Laravel 5.7 get ip address
- Email Verification in laravel 5.7 Example
- Calculate age from date of birth in Laravel
- Get date on change event of Bootstrap Datepicker
- PHP Laravel 7.x and 6.x Union query Example