天天看点

HTML5 调用手机相册和摄像头的方法并上传微信下测试通过

<input type="file" capture="camera" accept="image/*" id="cameraInput" name="cameraInput">

$("#cameraInput").on(\'change\',function(){

var file = this.files[0]
//alert(img);
var reader = new FileReader();
reader.readAsDataURL(file);

reader.onload = function(evt) {
$("#imgprv").attr("src",this.result);
$("#imgprv").attr("data-b64",encodeURIComponent(this.result));

}

});      

PHP接收端

function SetHeader(){
        $uid= $_SESSION[\'uid\'];
        $b64f = urldecode(I("b64f"));
        
        $f       = explode(\',\', $b64f);
        
        if ( preg_match(\'/\s*data:image\/(\w+);/\',$b64f , $result ) ){
                    file_put_contents( "1.txt", $result[1] );
                    $fName = time().".".$result[1];
                    $file =  C(\'PICTURE_UPLOAD.rootPath\')."newf"."/".$fName;
                    file_put_contents($file, base64_decode( $f[1] ) );
                    //写入数据库
                    M("member")->where("uid=$uid")->save(array("avatar"=>$file));
            
                    
        }
        exit( json_encode(array(\'status\'=>1,\'msg\'=>\'上传成功\',\'data\'=>$file) ) );
        
    }