php base64图片上传
2016-04-14 15:52
603 查看
<img src="{$list.img}" id="input_img" class="max-width:200px;"> <input type="hidden" name="img" id="input_imgName" value=""> <input type="hidden" id="oldImg" value="{$list.img}"> <input type="file" id="file_img"/>
<script>
function readFile(){
var file = this.files[0];
if(!/image\/\w+/.test(file.type)){
alert("只能上传图片");
return false;
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e){
var postData = {
'result':this.result,
'oldImg':$('#oldImg').val()
}
$.post("{:U('Banner/saveBase64Img')}",postData,function(data){
console.log(data);
$('#input_img').attr('src','/'+data.data).css({'max-width':'200px'});
$('#input_imgName').val('/'+data.data);
img_area.style.maxWidth = "200px";
});
}
}
$(function(){
if ( typeof(FileReader) === 'undefined' ){
result = "请使用现代浏览器操作";
alert(result);
$('#input_img').attr('disabled','disabled');
} else {
$('#input_img').addEventListener( 'change',readFile,false );
}
});
public function saveBase64Img(){ $oldImg = I('oldImg'); if(empty($oldImg)){ if(file_exists($file)){ if (unlink($file)){ return true; } } } $base64_image_content = I('result'); header('Content-type:text/html;charset=utf-8'); //匹配出图片的格式 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ $type = $result[2]; $new_file = "./Public/Uploads/".time().".{$type}"; if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){ $this->ajaxReturn(array('code'=>2000,'msg'=>'upload img ok','data'=>$new_file)); } } }
相关文章推荐
- SMTP结构及原理
- php yaf框架扩展实践五——数据层
- jrtplib简介
- PHP时间日期操作增减(date strtotime) 加一天 加一月
- PHP字符串常用方法
- php中filter函数用来验证、过滤
- PHP 函数之 call_user_func & call_user_func_array
- windows下PHP开发环境搭建
- PHP isset()与empty()的使用区别
- php进程间通讯常量定义
- Ubuntu安装PHP时候出错--xml2-config not found
- PHP 运行方式(PHP SAPI介绍)
- 虚拟机Ubuntu Server(宿主Win7)ftp服务的安装与启动
- windows下使用命令行运行PHP
- FTP客户端(利用sun.net.ftp.FtpClient实现)
- PHP大批量插入数据库的3种方法和速度对比
- [转链接]PHP运算符优先级的一个例外
- php测试程序运行时间和占用内存情况
- ThinkPHP 3.1.2 视图-1
- ThinkPHP 3.1.2 视图-1