Convert an image to base64 encoded string in PHP

Today we’ll show you how to convert an image to base64 encoded string in PHP. In this article we will create a sample demo for you to get base64 string from image data.

php base64 encoded image from url, php decode base64 image and display, php base64 encode uploaded image, php base64 encode image, convert image to base64 php online, convert base64 to image codeigniter, base64 encode and decode in php, converting image to string in php, use pathinfo, file_get_contents & base64_encode functions to convert image data to base64 string.

Example to convert an image to base64 encoded string

In this example we will use a couple of inbuilt PHP’s functions like pathinfo(), file_get_contents() and base64_encode() to convert image data to base64 image string and display it in image tag.

index.php

If you check the value of the $base64_str variable then it’s look like below.

Run code and check the output in the browser.

Output - Convert an image to base64 encoded string in PHP - Clue Mediator
Output – Convert an image to base64 encoded string in PHP – Clue Mediator

In the upcoming article, we will see how to get an image from base64 code and save it in the given path of the directory.

Thank you for reading. Happy Coding!

Demo & Source Code

Github Repository
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *