thinkphp結合layui上傳圖片


簡單示例:

<script type="text/javascript">
layui.use(['form', 'layedit','element', 'laydate','upload'], function(){
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;
    var laydate = layui.laysdate;
    // 上傳
    var upload = layui.upload;
    var uploadInst1 = upload.render({
    elem: '#uploadImgBut1', //綁定元素
    url: "{:U('Admin/Upload/layuiupload')}",//上傳接口
    done: function(res){
        if(res['state'] ==1){
            layer.msg(res['message']);
            $("#uploadImg1").attr('src',res['path']).show();
            $("input#uploadImgSrc1").val(res['path']); 
        };
    }
    }); 
});
</script>

后台代碼:

public function layuiupload(){
        $upload = new \Think\Upload();// 實例化上傳類
        $imgSize = intval(CP('IMGSIZE'));
        $imgType = CP('IMGTYPE');
        $imgSize = !empty($imgSize) ? $imgSize : 3145728;
        $imgType = !empty($imgType) ? explode(',',$imgType) : array('jpg','gif','png','jpeg');
        $upload->maxSize   =     $imgSize;// 設置附件上傳大小
        $upload->exts      =     $imgType;// 設置附件上傳類型
        $upload->rootPath  =     "./uploads/Picture/"; // 設置附件上傳根目錄
        $upload->savePath  =     ''; // 設置附件上傳(子)目錄
        $data = array();
        $data['state'] = 1;
        $data['message'] = '上傳成功';
        $data['path'] = '';
        // 上傳文件 
        $info = $upload->upload();
        if(!$info){
            $data['state'] = 0;
            $data['message'] ='上傳失敗';
        };
        $path = "uploads/Picture/".$info['file']['savepath'].$info['file']['savename'];
        $data['path'] = $path;
        echo json_encode($data);die;
    }

 上傳圖片,壓縮和裁剪

public function layuiupload(){
    $upload = new \Think\Upload();// 實例化上傳類
    $image = new \Think\Image(); 
    $imgSize = intval(CP('IMGSIZE'));
    $imgType = CP('IMGTYPE');
    $imgSize = !empty($imgSize) ? $imgSize : 3145728;
    $imgType = !empty($imgType) ? explode(',',$imgType) : array('mp4','jpg','gif','png','jpeg');
    $upload->thumb     =     true;
    $upload->maxSize   =     $imgSize;// 設置附件上傳大小
    $upload->exts      =     $imgType;// 設置附件上傳類型
    $upload->rootPath  =     "./Uploads/Picture/"; // 設置附件上傳根目錄
    $upload->savePath  =     ''; // 設置附件上傳(子)目錄
    $data = array();
    $data['state'] = 1;
    $data['message'] = '上傳成功';
    $data['path'] = '';
    // 上傳文件 
    $info = $upload->upload();
    if(!$info){
        $data['state'] = 0;
        $data['message'] ='上傳失敗';
    };
    $path = "/Uploads/Picture/".$info['file']['savepath'].$info['file']['savename'];
    $image->open(".".$path);
    $image->thumb(230, 160,\Think\Image::IMAGE_THUMB_FIXED)->save(".".$path);
    $data['path'] = $path;
    echo json_encode($data);die;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM