How to convert base64 to image and save in folder php?

Today, We want to share with you convert base64 to image php.In this post we will show you base64 to image conversion through php script, hear for base64 image conversion php we will give you demo and example for implement.In this post, we will learn about Convert base64 to image file and write To folder in PHP with an example.

How to convert base64 to image and save it using PHP?

sometimes we need to store whole image into our DB then we store in the base64 image format and while getting that image to show on the page we first need to be decode that base64 image.

This case generally problem arises while working with php api for an application.

In this example you will learn to decode base64 encoded image and save that image into a File Folders.

PHP - Save base64 Encoded string - Convert base64 to Image
PHP – Save base64 Encoded string – Converts base64 to Image

Lets simple example you have encoded image data into your any mysql database as well as you get that data into a php variable $encoded_image.


in the above full source code quickly i define upload directly path as well sa give full folder permission to this File Folders because i am going to write image file into this File Folders.

And after that i am breaking the string into arrays to get image and image file type.

After getting the image part simply apply php function base64_decode that encoded image part as well as simple copy this data content into a file.

And after executing this php file you will an image will be available into the upload File Folders. That is the file that you were expecting.

index.php


Convert Base64 to image in PHP

To convert a Base64 encoded string to an image in PHP, you can use the base64_decode() function to decode the string into a binary data, and then use the imagecreatefromstring() function to create an image resource from the binary data.

Here’s an example code snippet:


In this example, we first remove the data:image/png;base64, prefix from the Base64 string using the str_replace() function. Then, we decode the string into binary data using the base64_decode() function. Next, we create an image resource from the binary data using the imagecreatefromstring() function. Finally, we save the image as PNG using the imagepng() function and free up memory using the imagedestroy() function.

I hope you get an idea about convert base64 to image php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment