php读取和保存base64编码的图片内容
2015-07-07 13:44
756 查看
<?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;?>" />
相关文章推荐
- PHP常用正则表达式汇总(转)
- 从框架看PHP的五种境界及各自的薪资待遇
- PHP 自动加载类
- PHP静态化之真静态化
- PHP导入导出Excel代码
- PHP删除数组中的特定元素的代码
- PHP中,将变量值作为变量名
- PHP的json_encode中文被转码的问题
- 在使用thinkphp框架时,在模板中尽量少使用php代码段,会导致不解析的后果
- PHP与ECMAScript_1_变量与常量
- PHP接收请求的方式
- CTParagraphStyle的属性
- PHP ArrayAccess(数组式访问)接口
- PHP字符串与数组常用函数
- PHP empty(),isset()与is_null()的实例测试(转载)
- php编译错误:configure: error: Please reinstall the iconv library.
- PHP COOKIE的几种设置方式
- 一张表搞清楚php is_null、empty、isset的区别
- 10条php编程小技巧
- php实现的任意进制互转类分享