php实现将base64格式图片保存在指定目录的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了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 '新文件保存失败';
}
}
?>

PS:这里再为大家推荐几款在线图片工具供大家参考使用

图片转换为Base64编码在线工具:
http://tools.jb51.net/transcoding/img2base64

在线Email邮箱图标制作工具:
http://tools.jb51.net/email/emaillogo

在线PS图像处理工具:
http://tools.jb51.net/aideddesign/webps

在线图片格式转换(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext

ICO图标在线生成工具:
http://tools.jb51.net/aideddesign/ico_img

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

linux下的php-fpm参数配置介绍与参数优化说明

php-fpm.conf重要参数详解 pid = run/php-fpm.pid #pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启 error_log =...

php数组一对一替换实现代码

复制代码 代码如下: <?php header("Content-type: text/html; charset=utf-8"); function multiple_repla...

ubuntu下编译安装xcache for php5.3 的具体操作步骤

wget http://xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gzsudo tar -xzvf  xc...

php实现XSS安全过滤的方法

本文实例讲述了php实现XSS安全过滤的方法。分享给大家供大家参考。具体如下: function remove_xss($val) { // remove all non-pri...

PHP下操作Linux消息队列完成进程间通信的方法

关于Linux系统进程通信的概念及实现可查看:http://www.ibm.com/developerworks/cn/linux/l-ipc/   关于Linux系统消息队列的概念及实...