版本为:3.2.3
代码如下:
if(empty($_FILES['article_thumb']['tmp_name'])){
//echo "<script>alert('您没有选择文件');history.back(-1);</script>";exit;
}else{
//上传图片
$root_path = ROOT_PATH . 'www/home/data/upload/images';
if (!is_dir($root_path)) {
mkdir($root_path);
chmod($root_path, 0777);
}
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 15000000;// 设置附件上传大小 15M
$upload->exts = array('jpeg', 'jpg','gif','png');// 设置附件上传类型
$upload->rootPath = $root_path . '/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
$upload->subName = '';
$upload->saveName = 'uniqid';
$info = $upload->upload();
if (!$info) {
echo "<script>alert('".$upload->getError()."');history.back(-1);</script>";exit;
}
//处理缩略图
$image_path=$root_path.'/'.$info['article_thumb']['savename'];
$small_image_path=substr($image_path,0,-4).'_small.'.$info['article_thumb']['ext'];
$small_image_url='/data/upload/images/'.substr($info['article_thumb']['savename'],0,-4).'_small.'.$info['article_thumb']['ext'];
$large_image_path=substr($image_path,0,-4).'_large.'.$info['article_thumb']['ext'];
$large_image_url='/data/upload/images/'.substr($info['article_thumb']['savename'],0,-4).'_large.'.$info['article_thumb']['ext'];
$image = new \Think\Image();
//切小图
$image->open($image_path);
//设置缩略图最大宽度、高度
$small_image_max_width=40;
$small_image_max_height=40;
$image->thumb($small_image_max_width, $small_image_max_height)->save($small_image_path);
//切大图
$image->open($image_path);
$large_image_max_width=375;
$large_image_max_height=667;
$image->thumb($large_image_max_width, $large_image_max_height)->save($large_image_path);
//删除原图
unlink($image_path);
$arr['article_thumb']=$small_image_url;
$arr['article_image']=$large_image_url;
}