php将图片的base64串 转成 图片文件

24次阅读
没有评论

共计 570 个字符,预计需要花费 2 分钟才能阅读完成。

要将图片的 base64 串转成图片文件,可以使用以下代码:

<?php
// 获取 base64 串
$base64_string = '...'; // 替换成实际的 base64 串
// 获取文件扩展名
$matches = [];
preg_match('/data:image\/(.*?);base64/', $base64_string, $matches);
$file_extension = $matches[1];
// 解码 base64 串
$image_data = base64_decode(preg_replace('/data:image\/(.*?);base64,/', '', $base64_string));
// 生成文件名
$file_name = 'image.' . $file_extension;
// 保存文件
file_put_contents($file_name, $image_data);
echo ' 图片文件已保存。';

$base64_string 替换成实际的图片 base64 串,然后运行代码即可将 base64 串转成图片文件。图片文件将保存在当前目录下,文件名为image. 扩展名,如image.jpg

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-13发表,共计570字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)