How to convert image to Base 64 Data URI in PHP

One of the most important thing you can do to improve the load time of your website is reducing HTTP requests of your website.  The more number of HTTP requests, more time your website will take in loading. This is why it is recommended to combine JS and CSS files into one file. You can also embed image direct in the page so that it does not request for a separate file. You can do this by converting an image into base64 data URI.

Base 64 Data URI allows you to convert an image into data string and hence you can embedded this data string directly into the browser. It will not hit another HTTP request for the image and help in reducing number of requests to make your website faster.

You can see the demonstration of the tool to convert image into Base 64 Data URI. If you also want to do something like this in PHP, follow the code below.

You only need to fetch the content of the image and then convert the data in Base 64.

You can use the sample code added above in your existing code. I am not going to explain the full image upload system.

Demo

advertisement

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest updates, web design resources and tools.

You have Successfully Subscribed!

About The Author

  • ADD YOUR COMMENT

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Receive fresh web design resources and SEO tips in your email by our weekly newsletter

You have Successfully Subscribed!