以下代码使用 PHP 将图片转换为 base64 字符串格式:

<?php
//$file:图片地址
//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)); 
    // 输出
    $encode = '<img src="data:image/jpg/png/gif;base64,' . $base64 .'" >'; 
    echo $encode; 
}     
?>