图片的base64编码

Posted mracale

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片的base64编码相关的知识,希望对你有一定的参考价值。

<?php
header(‘Content-type:text/html;charset=utf-8‘);
//读取图片文件,转换成base64编码格式
$image_file = ‘./111.jpg‘;
$image_info = getimagesize($image_file);
$base64_image_content = "data:{$image_info[‘mime‘]};base64," . chunk_split(base64_encode(file_get_contents($image_file)));
 
//保存base64字符串为图片
//匹配出图片的格式
if (preg_match(‘/^(data:s*image/(w+);base64,)/‘, $base64_image_content, $result)){
 $type = $result[2];
 $new_file = "./test.{$type}";
 if (file_put_contents($new_file, base64_decode(str_replace($result[1], ‘‘, $base64_image_content)))){
    echo ‘新文件保存成功:‘, $new_file;
 }
}
?>
 
<img src="<?php echo $base64_image_content;?>" />

  

以上是关于图片的base64编码的主要内容,如果未能解决你的问题,请参考以下文章

c# Base64编码和图片的互相转换代码

怎么得到图片的base64编码 - 技术问答

怎么得到图片的base64编码 - 技术问答

C#解码base64编码的二进制数据的代码

Base64图片编码优化

把图片转成base64编码有啥好处吗?