php将图片链接转换为base64编码文件流

2021-10-09 16:38:40 浏览数 (2)

代码语言:javascript复制
/**
* 图片链接转换为 base64 文件流
* @param $imgUrl
* @return string
*/
function img_url_to_base64($imgUrl)
{
     $imageInfo = getimagesize($imgUrl);
      return 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents($imgUrl)));
}
代码语言:javascript复制
展示:
代码语言:javascript复制
{
    "code": 0,
    "message": "操作成功",
    "data": {
        "img_base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcUAAAHFAQAAAACrut8pAAAAAmJLR0QAAd2KE6QAAAAJcEhZcwAArnAEgAAABIAEbJaz4AAAAJdnBBZwAAAcUAAAHFAEXPcIYAAAOeSURBVHhe7ZxNbiIxEIU7QgrL3CBcrngQNE4lrsmqNxlByhlywQHr/6cdOI/pExE7m63iYU9hdNntqvbCdMEzJ1aR7fWSon5 TknCLZNUkfrnj8NDXfuZTVuYvGzCrem24Yr6sjl/x J0cLJZHTmmkmQrI dZcievurLk75c6dPrh73Sm dEuJ1dFrnTqgoSYtgu4jkJVWUjD4E/Otv6FQoIBScCU6uhpyUIVJNiUsQXfD3SxfvrENO2iInZYgkU44bmghTrn n3fz4eTayInVZSUkSUkqyiJTIgOJVOoJUpAOLkmclIlyaRZsldBkhxqw3knpgwKJ1dETqocmalyrnJLqgvh50QbLrXk7aJperOpJWvjS gNusGAPxON/hypY2gXcB0U9z0h6ZqepI/OhQf4F12egwO/QdrnR3gTeOxvdp00R2aqShK7uz4TjuwQmXIn6o8cEMFJyCCZoQrJ KNfZOUf2CHygR3SEcoEMTJlgpO2rnyCxVR17ZB25vB347OkR2RVM0IOCd2gU5aY7MVHUkOSSmBAQEm0IrnwNCYwCZQJJMcNIWmanqSHUIrnPgTcZsEUdMEAu4YjLWZ2FB1OmiMzVSXJMUAihwJ8IO iKewQjbSYQYWTNskMVUjyhm6npiQhBvgVrnjdzt 66DM5KTVsgsVUfCFMTArdHDTYtNoJhyN6IFbwKd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