3月222017
php base64保存为图片
发布:2017-3-22 17:36 Wednesday 分类:Php
阅读:1812次 评论:2条
因为上传的图片内容都是base64数据,base64图片的好处就是减少一个http请求,但是缺点就是网页无法缓存图片。那么base64数据怎么保存为图片呢?请往下看。
php base64保存为图片代码:
$type = $result[2];
$base64_body = substr(strstr($base64_url,','),1);
$img = base64_decode($base64_body);
// 保存图片-临时文件
$name = md5(time());
$temp = '/';
$file_name_ok = $temp.$name.".".$type;
php base64保存为图片代码:
<?php
$base64_url = 'base64数据源';
preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_url, $result);$type = $result[2];
$base64_body = substr(strstr($base64_url,','),1);
$img = base64_decode($base64_body);
// 保存图片-临时文件
$name = md5(time());
$temp = '/';
$file_name_ok = $temp.$name.".".$type;
file_put_contents($file_name_ok, $img);
?>
本文固定链接: http://alzhai.com/post-1026.html