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

PHP代码
  1. header('Content-type:text/html;charset=utf-8');  
  2. //读取图片文件,转换成base64编码格式  
  3. $image_file = './4296762_165319032930_2.jpg';  
  4. $image_info = getimagesize($image_file);  
  5. $base64_image_content = "data:{$image_info['mime']};base64," . chunk_split(base64_encode(file_get_contents($image_file)));  
  6.    
  7. //保存base64字符串为图片  
  8. //匹配出图片的格式  
  9. if (preg_match('/^(data:\s*image\/(\w+);base64,)/'$base64_image_content$result)){  
  10.   $type = $result[2];  
  11.   $new_file = "./test.{$type}";  
  12.   if (file_put_contents($new_filebase64_decode(str_replace($result[1], ''$base64_image_content)))){  
  13.     echo '新文件保存成功:'$new_file;  
  14.   }  
  15.    
  16. }  

<img src="<?php echo $base64_image_content;?>" />



评论: 0 | 引用: 0 | 查看次数: 1683
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑