h5 canvas 압축 이미지

2248 단어
//js 
        $(".file-upload").on('change',function() {
            var file = $(this)[0].files[0];
            var url = webkitURL.createObjectURL(file);
            var $img = new Image();
            $img.src = url;
            $img.onload = function() {
                // 
                var width = $img.width,
                    height = $img.height,
                    scale = width / height;
                width = parseInt(800);
                height = parseInt(width / scale);

                // canvas
                var $canvas = document.createElement("canvas");
                $canvas.width=width;
                $canvas.height=height;
                var ctx = $canvas.getContext('2d');
                ctx.drawImage($img, 0, 0, width, height);
                var dataUrl = $canvas.toDataURL('image/png',0.5);
                var img = dataUrl.replace("data:image/png;base64,", "")
                // 
                $.post('uploadbase64img',{imgData : img},function(data){
               
                },'json');
            }
        });

// 
        $data = $this->getData(['imgData'=>'string']);
        $img = base64_decode($data['imgData']);
        $file_root = "./uploads/";
    
        if($data['assortment']){
            $file_root.=$data['assortment'].'/';
        }
        $dir = $file_root.date('Y').'/'.date('m').'/'.date('d').'/';// 
        if(!is_dir($dir)){
            mkdir($dir,0777,true);
        }
        $key=uniqid();
        $filename=date('YmdHis').'_'.$key.'.png';
        $result = file_put_contents($dir.$filename, $img);// 
        if($result>0){
            $res[0]['name']=$filename;
            $res[0]['path']=$dir.$filename;
            $res[0]['size']=$result;
            $res[0]['extension']='png';
            $res[0]['key']=$key;
            $this->apiSuccess($res);
        }else{
            // 
            $this->apiError(['msg'=>' ']);
        }
        exit;

좋은 웹페이지 즐겨찾기