The following code uses PHP to convert an image to a base64 string format:

Example

<?php
//$file: image address
//Filetype: JPEG,PNG,GIF
$file = "encode.jpg";
if($fp = fopen($file,"rb", 0))
{
    $gambar = fread($fp,filesize($file));
    fclose($fp);

     
    $base64 = chunk_split(base64_encode($gambar));
    // Output
    $encode = '<img src="data:image/jpg/png/gif;base64,' . $base64 .'" >';
    echo $encode;
}    
?>