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

举报
Lansonli 发表于 2021/09/27 23:12:06 2021/09/27
【摘要】   /*** 图片链接转换为 base64 文件流* @param $imgUrl* @return string*/function img_url_to_base64($imgUrl){ $imageInfo = getimagesize($imgUrl); return 'data:' . $imag...

 


  
  1. /**
  2. * 图片链接转换为 base64 文件流
  3. * @param $imgUrl
  4. * @return string
  5. */
  6. function img_url_to_base64($imgUrl)
  7. {
  8. $imageInfo = getimagesize($imgUrl);
  9. return 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents($imgUrl)));
  10. }

 

 

展示:

  
  1. {
  2. "code": 0,
  3. "message": "操作成功",
  4. "data": {
  5. "img_base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcUAAAHFAQAAAACrut8pAAAAAmJLR0QAAd2KE6QAAAAJcEhZcwAA\r\nAEgAAABIAEbJaz4AAAAJdnBBZwAAAcUAAAHFAEXPcIYAAAOeSURBVHhe7ZxNbiIxEIU7QgrL3CBc\r\ngQNE4lrsmqNxlByhlywQHr/6cdOI/pExE7m63iYU9hdNntqvbCdMEzJ1aR7fWSon5+TknCLZNUkf\r\nj8NDXfuZTVuYvGzCrem24Yr6sjl/x+J0cLJZHTmmkmQrI+dZcievurLk75c6dPrh73Sm+dEuJ1dF\r\nTqgoSYtgu4jkJVWUjD4E/Otv6FQoIBScCU6uhpyUIVJNiUsQXfD3SxfvrENO2iInZYgkU44bmghT\r\n+n3fz4eTayInVZSUkSUkqyiJTIgOJVOoJUpAOLkmclIlyaRZsldBkhxqw3knpgwKJ1dETqocmaly\r\nJLqgvh50QbLrXk7aJperOpJWvjS+gNusGAPxON/hypY2gXcB0U9z0h6ZqepI/OhQf4F12egwO/Qd\r\nR3gTeOxvdp00R2aqShK7uz4TjuwQmXIn6o8cEMFJyCCZoQrJ+KNfZOUf2CHygR3SEcoEMTJlgpO2\r\nyCxVR17ZB25vB347OkR2RVM0IOCd2gU5aY7MVHUkOSSmBAQEm0IrnwNCYwCZQJJMcNIWmanqSHUI\r\nPgTcZsEUdMEAu4YjLWZ2FB1OmiMzVSXJMUAihwJ8IO+iKewQjbSYQYWTNskMVUjyhm6npiQhBvgV\r\njdzt+66DM5KTVsgsVUfCFMTArdHDTYtNoJhyN6IFbwKdNEdmqkrySa8LOM9wJsAUMjJFh5NWyQxV\r\nSNJi5/bGb6Ze148gBhAdZCSiw0mDZJaqJDkGuL1RQOxQSwzwTM0EsuvgpFEyQ9WRWPnJBwQECjQ+\r\n+EAxgMYXixgQLWZwS3TSHJmp6kgsdo4B9YFMocZHpvQtMX7hkYOTBslMVUf2DoVh44MPVCAgyKE4\r\nLQzSxElLZKb+B9nJh0jokB1eJMUhbXzaErVgbHvaC/nZPKaJk0bIheqELPL0LZR8rPANpKx8bnxQ\r\n2vftE9k9vb910hY5rrlnaFxPSF0EzfTnBTt8dHz3HlIdYh+QFskhITu9v3XSNDmm+WdoTE/JVorz\r\nDIkvbyCvbEqUOARRf9wL+SneOWmbHJdMG32GxvWcXPL/fuiSKk/CobjvoxigCbELAtsmEvs+J+2T\r\n41Jy7BkaVz75qHJk71Dg2yy1S489qT86aZtcrrpJOIQYuPHKV1P4OH8/4qRxcrleJaX4M5JOOuyD\r\nBAQV7FDLxx4n10Au1Ytk0t+QdJt1TH+GRJkQ1dEvKQZ2OWmbXK6XyEw5OScn5/QC+Q80ME27gJ1j\r\n9gAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyMC0wNi0xNlQxNDo1MjowOCswODowMJs4XlEAAAAldEVY\r\ndGRhdGU6bW9kaWZ5ADIwMjAtMDYtMTZUMTQ6NTI6MDgrMDg6MDDqZebtAAAAAElFTkSuQmCC\r\n"
  6. }
  7. }

 

文章来源: lansonli.blog.csdn.net,作者:Lansonli,版权归原作者所有,如需转载,请联系作者。

原文链接:lansonli.blog.csdn.net/article/details/108304724

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。