本文实例讲述了php实现将base64格式图片保存在指定目录的方法。分享给大家供大家参考,具体如下:
<?php
header('Content-type:text/html;charset=utf-8');
$base64_image_content = $_POST['imgBase64'];
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
$new_file = "upload/active/img/".date('Ymd',time())."/";
if(!file_exists($new_file))
{
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($new_file, 0700);
}
$new_file = $new_file.time().".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
echo '新文件保存成功:', $new_file;
}else{
echo '新文件保存失败';
}
}
?>
[b]PS:这里再为大家推荐几款在线图片工具供大家参考使用[/b]
[b]图片转换为Base64编码在线工具:[/b]
[url=http://tools.jb51.net/transcoding/img2base64]http://tools.jb51.net/transcoding/img2base64[/url]
[b]在线Email邮箱图标制作工具:[/b]
[url=http://tools.jb51.net/email/emaillogo]http://tools.jb51.net/email/emaillogo[/url]
[b]在线PS图像处理工具:[/b]
[url=http://tools.jb51.net/aideddesign/webps]http://tools.jb51.net/aideddesign/webps[/url]
[b]在线图片格式转换(jpg/bmp/gif/png)工具:[/b]
[url=http://tools.jb51.net/aideddesign/picext]http://tools.jb51.net/aideddesign/picext[/url]
[b]ICO图标在线生成工具:[/b]
[url=http://tools.jb51.net/aideddesign/ico_img]http://tools.jb51.net/aideddesign/ico_img[/url]
更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/455.htm]PHP图形与图片操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/357.htm]PHP运算与运算符用法总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/495.htm]PHP网络编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》
希望本文所述对大家PHP程序设计有所帮助。