base64文件上传后台处理

jscanvas前端画图

 <canvas id="myCanvas" width="500" height="300"></canvas>

unction DrawPic() {
    // Get the canvas element and its 2d context
    var Cnv = document.getElementById('myCanvas');
    var Cntx = Cnv.getContext('2d');
    
    // Create gradient
    var Grd = Cntx.createRadialGradient(150, 150, 20, 140, 200, 330);
    Grd.addColorStop(0, "#c96513");
    Grd.addColorStop(1, "#861d33");

    // Fill with gradient
    Cntx.fillStyle = Grd;
    Cntx.fillRect(0, 0, 500, 300);

    // Write some text
    for (i=1; i<10 ; i++)
    {
        Cntx.fillStyle = "white";
        Cntx.font = "36px Microsoft YaHei";
        Cntx.globalAlpha = (i-1) / 9;
        Cntx.fillText("jQuery之家-htmleaf.com", i * 3 , i * 30);
    }
}

function UploadPic() {
    
    // generate the image data
    var Pic = document.getElementById("myCanvas").toDataURL("image/png");

    // console.log(Pic);

    // Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "");
    // console.log(Pic);

    // Sending the image data to Server
    // $.ajax({
    //     type: 'POST',
    //     url: 'php.php',
    
    //     data: {imageData: 'Pic'  },
     
    //     dataType: 'json',
    //     success: function (msg) {
    //         alert("Done, Picture Uploaded."); 
    //     }
    // });
 //    console.log(typeof Pic);
 // var test=Pic;
    $.post("url",{pic:Pic,uid:'1000000'},function(data){
        console.log(data);

    });


注意的代码
var Pic = document.getElementById("myCanvas").toDataURL("image/png");

    // console.log(Pic);

     Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "");
//正则去除前面的base64,但是没用到

    var Pic = document.getElementById("myCanvas").toDataURL("image/png");

Pic是一个字符串 类型

函数代码

//文件上传
function base64_upload($base64) {

    $base64_image = str_replace(' ', '+', $base64);
    //post的数据里面,加号会被替换为空格,需要重新替换回来,如果不是post的数据,则注释掉这一行
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
        //匹配成功图片类型
        if($result[2] == 'jpeg'){
            $image_name = md5(uniqid()).'.jpg';
         
        }else{
            $image_name = md5(uniqid()).'.'.$result[2];
        }

        $dirpath=mk_dir();
        $image_file =$dirpath."/".$image_name;
       //服务器文件存储路径
        if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){
            return $image_file;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

//创建文件目录
function mk_dir(){ 
    $dir = date('Y-m-d', time()); 
    $path="./Uploads/".$dir;
    if(is_dir($path)){  
        return $path;  
    }else{  
        mkdir($path,0777,true);  
        return $path;  
    }  
}  

头像逻辑


  //base64文件上传和修改函数
 function userpic(){

    $pic=I('pic');
    $uid=I('uid');
    if(!$pic&&!$uid){
      $data=array(
                'code'=>500,
                'msg'=>'参数错误'
                );
    $this->ajaxReturn ( $data );      
    }
    else{
       $url=base64_upload($pic);
    $row=M('yxtuser')->where(array('uid'=>$uid))->find();
      //修改
       if($row&&$url){
        $data=array(

          'uid'=>$uid,
          'uimg'=>$url,
          );   
          unlink($row['uimg']);
        if(M('yxtuser')->where(array('id'=>$row['id']))->save($data)){

          $info=array(

            'data'=>$data,
            'status'=>array(
                'code'=>200,
                'msg'=>'头像修改成功'
                )
            );  

            $this->ajaxReturn ( $info );  
        }
       }

     //新增逻辑
       if($url&&$uid){
          $data=array(
            'uid'=>$uid,
            'uimg'=>$url,
            );
           if($row=M('yxtuser')->add($data)){
               $info=array(

                'data'=>$data,
                'status'=>array(
                    'code'=>200,
                    'msg'=>'头像上传成功'
                    )
                );  
                $this->ajaxReturn ( $info );  
           }

       }     

    }//else end
   
  }//use

转载于:https://my.oschina.net/oneboi/blog/734538

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值