thinkphp 图片上传生成缩略图

来源:互联网 发布:网络网络维护 编辑:程序博客网 时间:2024/06/06 13:17
<?phpnamespace Admin\Controller;use Think\Controller;class GoodsController extends Controller {public $gm;public function __construct(){parent::__construct();//继承父类的构造方法$this->gm=D('goods');}    public function goodsadd(){      if(IS_POST){        if(!$this->gm->create($_POST)){        echo $this->gm->getError();        exit;        }                $upload = new \Think\Upload();// 实例化上传类                $upload->maxSize = 3145728 ;// 设置附件上传大小                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型                $upload->rootPath = './Upload/'; // 设置附件上传根目录                $upload->savePath = ''; // 设置附件上传(子)目录                // 上传文件                $info = $upload->upload();                if(!$info) {// 上传错误提示错误信息                $this->error($upload->getError());                }else{// 上传成功               $img_path1='./Upload/'.$info['goods_img']['savepath'];               $img_path2=$info['goods_img']['savename'];//保存路径                            $image = new \Think\Image();                $image->open($img_path1.$img_path2);//打开原图                // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg                $img_xiao='./Upload/thumb/'.$img_path2;//缩略图生成的路径                $image->thumb(150, 150)->save($img_xiao);//缩略图按照150的比例保存                $this->gm->thumb_img=$img_xiao;//数据库中thumb_img字段的值来自于$img_xiao               $this->gm->goods_img=$img_path1.$img_path2;//数据库中goods_img字段的值来自于$img_path;                }       $this->gm->add();      }        $this->display();    }}