php 读取图片文件为base64

前端img的src如果是base64,那么需要mime头的

后端php读取图片并加上mime头处理如下

function readPhotoToBase64($image_file)
{
    //相对或绝对路径
    $image_info = getimagesize($image_file);
    $base64_content = "data:{$image_info['mime']};base64," . chunk_split(base64_encode(file_get_contents($image_file)));
    return $base64_content;
}
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注