上传图片并生成缩略图

上传图片并生成缩略图(使用的是Thinkphp3.2.3的框架):

add.html    一个简易的上传图片的页面


BrandController.class.php(后台处理控制器)

public function add() {

      $brand = D('Brand');

      if(IS_POST) {

        $data = I('post.');

         if($brand->create($data)) {

                // 使用前置操作(将对数据的操作放置在模型层中)

                if($brand->add()) {

                    $this->success('添加图片成功',U('brand/lists'));

               }else {

                       $this->error('添加图片失败');

                    }

          }else {

             $this->error($brand->getError());

 }

          return;

}

     $this->display();

}



BrandModel.class.php(模型层)

public function _before_insert(&$data,$options) {
   if($_FILES['brand_logo']['tmp_name']) {
// 图片上传
$upload = new \Think\Upload();
   $upload->maxSize   =     3145728 ;// 设置附件上传大小
$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->savePath  =      './Uploads/brand/'; // 设置附件上传(子)目录
            $upload->rootPath  =      './'; // 设置附件上传根目录

            // 执行上传操作
            $info = $upload->upload();
            if($info) {
// 图片路径
  $file_path = $info['brand_logo']['savepath'].$info['brand_logo']['savename'];
}

            // 生成缩略图
            $img = new \Think\Image();
            $img->open($file_path);
            $img->thumb(100,30)->save($file_path);

$data['brand_logo'] = $file_path;
}
   }



















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值