Thinkphp3.2.3上传图片并生成缩率图(包含add,edit)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38576764/article/details/79972450

添加

$upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =      'article/'; // 设置附件上传(子)目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
          foreach($info as $file){
              echo '图片保存为:'.$file['savepath'].$file['savename'];
          }
          $imgurl =$file['savepath'].$file['savename'];
          $thumburl=$file['savepath'].'small_'.$file['savename'];
          $openurl='./Uploads/'.$file['savepath'].$file['savename'];
          $thumb='./Uploads/'.$file['savepath'].'small_'.$file['savename'];
          $image = new \Think\Image();
          $image->open($openurl);
          $image->thumb(150, 150)->save($thumb);
        }
        if (false === $article->create()) {
            $this->error($article->getError());
        }
        $article->img=$imgurl;
        $article->thumb=$thumburl;
        $cid=I('post.cid');
        $article->cname=get_name('article_cate',$cid);
        $article->create_time=time();
        $list=$article->add();

编辑

      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize   =     3145728 ;// 设置附件上传大小
      $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
      $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
      $upload->savePath  =      'article/'; // 设置附件上传(子)目录
      // 上传文件
      $info   =   $upload->upload();
      if(!$info) {// 上传错误提示错误信息
        $id=I('post.id');
        $imgurl=get_imgurl('article',$id);
        $thumburl=get_thumburl('article',$id);
      }else{// 上传成功 获取上传文件信息
          foreach($info as $file){
              echo $file['savepath'].$file['savename'];
          }
          $imgurl =$file['savepath'].$file['savename'];
          $thumburl=$file['savepath'].'small_'.$file['savename'];
          $openurl='./Uploads/'.$file['savepath'].$file['savename'];
          $thumb='./Uploads/'.$file['savepath'].'small_'.$file['savename'];
          $image = new \Think\Image();
          $image->open($openurl);
          $image->thumb(300,300)->save($thumb);
      }
      if (false === $article->create()) {
          $this->error($article->getError());
      }
      //获取分类名称和id
      $cid=I('post.cid');
      $cname=get_name('article_cate',$cid);
      $article->img=$imgurl;
      $article->thumb=$thumburl;
      $article->cname=$cname;
      $article->is_imp=I('post.is_imp');
      $article->update_time=time();
      $list=$article->save();
阅读更多

没有更多推荐了,返回首页