Convert an Image to base64 Encoding String in PHP

In this tutorial, we will introdue how to convert an image to base64 encoding string in php.

1. Open and read image data

We can use file_get_contents() function to read an image data.

<?php
$img = file_get_contents('cocyer.png');
?>

2. Convert an image to base64 encoding string

We can use base64_encode() function to convert.

<?php
$data = base64_encode($img); 
  
// Display the output 
echo $data; 
?>