header('Content-type:text/html;charset=utf-8');
//读取图片文件,转换成base64编码格式
$image_file = './picture.jpg';
$image_info = getimagesize($image_file);
$base64_image_content = "data:{$image_info['mime']};base64," .
chunk_split(base64_encode(file_get_contents($image_file)));
// string chunk_split ( string $body [, int $chunklen = 76 [, string $end = "\r\n" ]] ) 将字符串分割成小块
// 使用此函数将字符串分割成小块非常有用。例如将 base64_encode() 的输出转换成符合 RFC 2045 语义的字符串。它会在每 chunklen 个字符后边插入 end
//保存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;
}
}
echo "<pre>";
print_r($result);
echo "</pre>";
echo "<pre>";
print_r($result);
echo "</pre>";
php读取和保存base64编码的图片内容
最新推荐文章于 2024-02-28 17:19:42 发布