ThinkPHP上传图片压缩尺寸

function layuiUpload($path = '') {
	$upload = new \Think\Upload();
	$upload->maxSize = 2097152;
	$upload->exts = ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx'];
	$savePath = $path ? './upload/' . $path . '/' : './upload/';
	$upload->rootPath = $savePath;
	$upload->autoSub = false;
	$res = $upload->upload();
	if (\stristr($res['file']['type'], 'image')) { // 如果上传的是图片进行压缩
		$image = new \Think\Image();
		$image->open($savePath . $res['file']['savename']);
		$image->thumb(800, 800)->save($savePath . $res['file']['savename']);
	}
	$this->ajaxReturn([
		'code' => 0,
		'msg' => '',
		'data' => [
			'fullSrc' => 'http://' . \currentDomain() . \str_replace('./', '/', $savePath) . $res['file']['savename'],
			'src' => \str_replace('./', '/', $savePath) . $res['file']['savename'],
			'fileName' => $res['file']['savename'],
		],
	]);
}

posted on 2022-09-08 10:10  小馬過河﹎  阅读(58)  评论(0)    收藏  举报

导航