php 远程图片地址 转 base64

/**
 * 获取网络图片的Base64编码
 * $img_url 网络图片地址
 * $hasPre  是否有前缀
 * @return string
 */
public function imgToBase64($img_url,$hasPre = true)
{
    $img_base64 = '';
    $imageInfo = getimagesize($img_url);
    if (!$imageInfo) {
        return false;
    }
    $img_base64 = "" . chunk_split(base64_encode(file_get_contents($img_url)));
    if ($hasPre) {
        $img_base64 = 'data:' . $imageInfo['mime'] . ';base64,'.$img_base64;
    }
    return $img_base64;
}
原文地址:https://www.cnblogs.com/jiqing9006/p/14501561.html