前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php读取和保存base64编码的图片内容

php读取和保存base64编码的图片内容

作者头像
lilugirl
发布2019-05-26 21:09:46
2.5K0
发布2019-05-26 21:09:46
举报
文章被收录于专栏:前端导学前端导学
代码语言:javascript
复制
<?php
header('Content-type:text/html;charset=utf-8');
//读取图片文件,转换成base64编码格式
$image_file = './4296762_165319032930_2.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;?>" />
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档