思路:判断如果上传了新图上就生成缩略图并删除原图,
实际操作:直接把添加时上传图片的代码复制到修改里
修改商品模型:
protected function _before_update(&$data, $option)
{
// 要修改的商品ID
$id=$option['where']['id'];
/**************** 处理LOGO *******************/
// 判断有没有选择图片
if($_FILES['logo']['error'] == 0)
{
$upload = new ThinkUpload();// 实例化上传类
$upload->maxSize = 1024 * 1024 ; // 1M
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
$upload->savePath = 'Goods/'; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if(!$info)
{
// 获取失败原因把错误信息保存到 模型的error属性中,然后在控制器里会调用$model->getError()获取到错误信息并由控制器打印
$this->error = $upload->getError();
return FALSE;
}
else
{
/**************** 生成缩略图 *****************/
// 先拼成原图上的路径
$logo = $info['logo']['savepath'] . $info['logo']['savename'];
// 拼出缩略图的路径和名称
$mbiglogo = $info['logo']['savepath'] .'mbig_'. $info['logo']['savename'];
$biglogo = $info['logo']['savepath'] .'big_'. $info['logo']['savename'];
$midlogo = $info['logo']['savepath'] .'mid_'. $info['logo']['savename'];
$smlogo = $info['logo']['savepath'] .'sm_'. $info['logo']['savename'];
$image = new ThinkImage();
// 打开要生成缩略图的图片
$image->open('./Public/Uploads/'.$logo);
// 生成缩略图
$image->thumb(700, 700)->save('./Public/Uploads/'.$mbiglogo);
$image->thumb(350, 350)->save('./Public/Uploads/'.$biglogo);
$image->thumb(130, 130)->save('./Public/Uploads/'.$midlogo);
$image->thumb(50, 50)->save('./Public/Uploads/'.$smlogo);
/**************** 把路径放到表单中 *****************/
$data['logo'] = $logo;
$data['mbig_logo'] = $mbiglogo;
$data['big_logo'] = $biglogo;
$data['mid_logo'] = $midlogo;
$data['sm_logo'] = $smlogo;
/*************** 删除原来的图片 *******************/
// 先查询出原来图片的路径
$oldLogo = $this->field('logo,mbig_logo,big_logo,mid_logo,sm_logo')->find($id);
deleteImage($oldLogo);
}
}
// 我们自己来过滤这个字段
$data['goods_desc'] = removeXSS($_POST['goods_desc']);
}