Product thumbnail
Thumbnail image of the product
Copy the model file of the previous brand module and rename it
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','市場價(jià)格不得為空!',1), array('shop_price','require','本店價(jià)格不得為空!',1), array('goods_name','','品牌名稱不得重復(fù)!',1,unique,3), ); public function _before_insert(&$data,$option){ if($_FILES['original']['tmp_name']){ $upload = new \Think\Upload();// 實(shí)例化上傳類 $upload->maxSize = 3145728 ;// 設(shè)置附件上傳大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設(shè)置附件上傳類型 // $upload->autoSub = false; $upload->savePath = './Public/Uploads/Goods/'; // 設(shè)置附件上傳目錄 $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; } } }
Controller 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(); }