zoukankan      html  css  js  c++  java
  • 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 ThinkUpload();// 实例化上传类
            $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 ThinkUpload();// 实例化上传类
        $image = new ThinkImage(); 
        $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,ThinkImage::IMAGE_THUMB_FIXED)->save(".".$path);
        $data['path'] = $path;
        echo json_encode($data);die;
    }
  • 相关阅读:
    #include "stdafx.h" 错误?
    扩频技术
    求数组中只出现一次的数字(算法)
    1.3一摞烙饼的排序
    嵌套类
    企业级邮件服务软件推荐
    关于Linq To Sql中Detach方法和一个公共基类
    asp.net(c#) 将dbf转换为xls或wps,并将数据的列名改成中文;并判断本机是否安装office2003,2007和wps2007,2010
    一句代码解决IE8兼容问题(兼容性视图)
    asp.net(C#)套用模板操作Excel
  • 原文地址:https://www.cnblogs.com/e0yu/p/9353064.html
Copyright © 2011-2022 走看看