PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

How to convert image to base64 using PHP

A useful article on how to convert and display image as base64 data using PHP.

August 29, 2022
Reading Time: 3 mins read
PHP101.Net - How to convert image to base64 using PHP

PHP101.Net - How to convert image to base64 using PHP

Share on FacebookShare on Twitter

Quick solution

The script below will use PHP to convert image to base64:

// Get the image content
$image = file_get_contents('image.png');

// Convert it to base64
$base64_image = base64_encode($image);

Optionally, to display the image using converted base64 data:

echo '<img src="data:image/png;base64,' . $base64_image . '">';

Below is more detailed information on the conversion process with notes and alternative methods for converting image to base64 data and display it.

Why convert image to base64?

The main purpose of converting image to base64 data is to be able to transmit and store image data from binary format to ASCII format. This way, we can display, or transfer the image data easier in text format.

Another minor benefit of using base64 image format is to be able to display the image on webpages using its base64 data in text format, without having to load the image file. Therefore, the number of HTTP request is recuded.

Although so, the actual data transmission using the image’s base64 data is actually more than the binary file itself. The converted base64 format’s amount of data will be always bigger than the original binary format by around 33%.

In conclusion, the benefit mostly only applies to small, tiny images (eg. icons) that would save the amount of requests to a webpage.

To convert image to base64 using PHP

The primary steps to convert an image to base64 is:

  • Get the image file content
  • Convert it to base64 data

There is various ways to get the image file content.

Option 1: Get image content using file_get_contents()

This is the basic method to extract file contents.

// Get image content from image file or URL
$imageData = file_get_contents('image.png');

Please note that, there would be web hosting disabled the file_get_contents() for security purposes. In that case, we have two more options.

Option 2: Get image content using stream handler

This is another safe method to get image content that won’t be blocked on most web hosting.

// Open image or URL in read more
$file = fopen('image.png', 'r');

// Put image data using stream handler
$imageData = stream_get_contents($file);

// Close the stream to release memory
fclose($file);

Display the image in base64 format

Usually, there are two ways we wanted to display the image in base64 format:

  • As <img> tag
  • Using PHP to output as a standalone image

Display image in base64 format as <img> tag

We can display the image in <img> tag by passing the base64 data as this format:

echo '<img src="data:{$image_mime_type};base64,{$base64_data}">';

Where

  • {$image_mime_type} is the mimetype of the image, eg. image/png for png format, image/jpeg for jpeg/jpg format.
  • {$base64_data} is the extracted base64 data from the image.

For example, with png image format:

echo '<img src="data:image/png;base64,iVBORw0KGgo.....">';

Use PHP to output as a standalone image

Using this method will have the php page to display the page as if it’s displaying an image file.

// Set header as the image mimetype we want
header("Content-type: image/png");

// Encode the image data to base64 and display the image
echo base64_encode($imageData);

Final thoughts

The guide on how to convert image to base64 using PHP is now over. We hope you could understand its benefit, drawback of converting and displaying image as base64, and make good use of it in your projects.

You might also like

  • How to copy files using PHP
  • How to convert from string to array using PHP

References

  • PHP.net Official Documentation – base64_encode() function
  • PHP.net Official Documentation – base64_decode() function
Tags: image base64image to base64php image to base64
Previous Post

How to copy files using PHP

Next Post

How to display image from file using PHP

Related Posts

PHP101.Net - How to remove empty array elements using PHP

How to remove empty array elements using PHP

September 18, 2022
PHP101.Net - How to display image from file using PHP

How to display image from file using PHP

September 8, 2022

Table of Contents

    • Quick solution
    • Why convert image to base64?
    • To convert image to base64 using PHP
    • Option 1: Get image content using file_get_contents()
    • Option 2: Get image content using stream handler
    • Display the image in base64 format
    • Display image in base64 format as <img> tag
    • Use PHP to output as a standalone image
    • Final thoughts
    • You might also like
    • References
Next Post
PHP101.Net - How to display image from file using PHP

How to display image from file using PHP

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to copy files using PHP

How to change Laragon PHP version on Windows

How to install ImageMagick on Windows

How to install PHP on Windows – Standalone method

How to Add Elements to Array in PHP Easily

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.