ThinkPHP6 缩略图上传
2022-07-28 14:14:58
122
{{single.collect_count}}

在MVC开发中,视图层表单中往往会有文件上传(上传图片)这一说,一般我们上传的图片需要先处理成压缩图,因为一般会是将缩略图的图片地址存入数据库,并不是原图(因为我们页面展示图片,是以缩略图的形式展示的,当点击缩略图的时候才会显示原图)所以就要考虑到原图和缩略图分开存储 

 图片上传、处理等等需要引入的拓展或者类在这里就不讲了,直接上代码; 

thinnkphp6手册中没有关于图形处理的讲解,如果想要了解可以去thinkPHP5 的手册中去了解

public function uploadFile(Request $request){//接收表单上传的文件$files = request()->file();//对上传文件进行验证try {validate(['logo'=>'fileExt:jpg,png,jpeg'])->check($files);//将原图上传保存$logo = Filesystem::disk('public')->putFile('topic',request()->file('logo'));}catch (ValidateException $e){return $e->getError();}//组装路径(文件名)$thumb = 'storage/img/';$date = date('Ymd');//判断文件是否存在,不存在就创建file_exists($thumb.$date)?'':mkdir($thumb.$date,0777,true);$time = md5((string)time()).'.'.request()->file('logo')->extension();//组装文件路径$file=$thumbPath = $thumb.$date.'/'.$time;//对图片进行压缩try {//找到原图对原图进行压缩处理$image = \think\Image::open('storage/'.$logo);$image->thumb(100,100)->save($thumbPath);}catch (\Exception $exception){return $exception->getMessage();}}

至此,原图以及缩略图的分开存储已完成;

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}