1.通过点击事件获取当前是哪个图片.
2.全局变量获取当前的点击上传图片的位置存储起来.
3.安卓访问js方法把base64图片字符串传过来,这边接收传到后台.(这里曾经出现个问题就是,js接受的base64是没有问题的,但是php接收的时候他会转义里面有好多空格,导致base64不能使用,$.ajax里面添加datatype:"json"类型就可以了)
4.php后台接收
function actionUploadimgbase(){
$str=str_replace(array('data:image/gif;base64,','data:image/png;base64,','data:image/jpg;base64,'),'',$_POST['url']);
if($str==''){
$msg['status']=0;
$msg['info']='参数为空';
echo json_encode($msg);exit;
}
if($str){
$imageName = date("ymdHis",time())."_".rand(1111,9999)."_".rand(1111,9999).'.png';
if(!file_exists("./uploads/images/".date("Ymd"))){
mkdirs("./uploads/images/".date("Ymd"));
}
$path = "./uploads/images/".date("Ymd");
$url = "/uploads/images/".date("Ymd");
$imageSrc= $path."/". $imageName; //图片名字
$res=file_put_contents($imageSrc, base64_decode($str));
$sid=0;
$data['path']=$imageSrc;
$data['url']=$url."/". $imageName;
$data['md5']=md5_file($data['path']);
$data['sha1']=sha1($data['path']);
$data['tailorpath']=$data['path'];
$data['tailorurl']=$data['url'];
$data['status']=0;
$data['create_time']=time();
$data['sid']=$sid;
$picobj=M('sys_picture');
$pic=$picobj->find(array('md5'=>$data["md5"],'sha1'=>$data["sha1"] ),' id asc ','id');
if($pic){
$newid=$pic['id'];
}else{
$newid=$picobj->create($data);
}
$array=array('id'=>$newid,'url'=>$data['url']);
$msg['status']=1;
$msg['info']='上传成功';
$msg['result']=$array;
echo json_encode($msg);exit;
}else{
$msg['status']=0;
$msg['info']='参数错误';
echo json_encode($msg);exit;
}
}