商品的缩略图
商品的缩略图
复制一份之前品牌模块的model模型文件,更名为
GoodsModel.class.php
<?php namespace Admin\Model; use Think\Model; class GoodsModel extends Model { protected $_validate = array( array('goods_name','require','品牌名称不得为空!',1), array('market_price','require','市场价格不得为空!',1), array('shop_price','require','本店价格不得为空!',1), array('goods_name','','品牌名称不得重复!',1,unique,3), ); public function _before_insert(&$data,$option){ if($_FILES['original']['tmp_name']){ $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 // $upload->autoSub = false; $upload->savePath = './Public/Uploads/Goods/'; // 设置附件上传目录 $upload->rootPath = './'; $info = $upload->uploadOne($_FILES['original']); $original=$info['savepath'].$info['savename']; $max_thumb=$info['savepath'].'max_'.$info['savename']; $mid_thumb=$info['savepath'].'mid_'.$info['savename']; $sm_thumb=$info['savepath'].'sm_'.$info['savename']; $image = new \Think\Image(); $image->open($original); $image->thumb(362, 362)->save($max_thumb); $image->thumb(222, 222)->save($mid_thumb); $image->thumb(67, 67)->save($sm_thumb); $data['original']=$original; $data['max_thumb']=$max_thumb; $data['mid_thumb']=$mid_thumb; $data['sm_thumb']=$sm_thumb; } } }
控制器 GoodsController.class.php
public function add(){ $goods=D('goods'); if(IS_POST){ if($goods->create()){ if ($goods->add()) { $this->success('添加品牌成功',U('index')); }else{ $this->error('添加品牌失败'); } }else{ $this->error($goods->getError()); } return; } $cateres=D('cate')->catetree(); $this->assign('cateres',$cateres); $brandres=D('brand')->select(); $this->assign('brandres',$brandres); $this->display(); }