thinkphp5上传图片及生成缩略图公共方法(分享)

2025-05-29 0 21

直接上代码,可以写在公共文件common和继承的基础类中,方便调用

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39
/*

* $name为表单上传的name值

* $filePath为为保存在入口文件夹public下面uploads/下面的文件夹名称,没有的话会自动创建

* $width指定缩略宽度

* $height指定缩略高度

* 自动生成的缩略图保存在$filePath文件夹下面的thumb文件夹里,自动创建

* @return array 一个是图片路径,一个是缩略图路径,如下:

* array(2) {

["img"] => string(57) "uploads/img/20171211\\3d4ca4098a8fb0f90e5f53fd7cd71535.jpg"

["thumb_img"] => string(63) "uploads/img/thumb/20171211/3d4ca4098a8fb0f90e5f53fd7cd71535.jpg"

}

*/

protected function uploadFile($name,$filePath,$width,$height)

{

$file = request()->file($name);

if($file){

$filePaths = ROOT_PATH . 'public' . DS . 'uploads' . DS .$filePath;

if(!file_exists($filePaths)){

mkdir($filePaths,0777,true);

}

$info = $file->move($filePaths);

if($info){

$imgpath = 'uploads/'.$filePath.'/'.$info->getSaveName();

$image = \\think\\Image::open($imgpath);

$date_path = 'uploads/'.$filePath.'/thumb/'.date('Ymd');

if(!file_exists($date_path)){

mkdir($date_path,0777,true);

}

$thumb_path = $date_path.'/'.$info->getFilename();

$image->thumb($width, $height)->save($thumb_path);

$data['img'] = $imgpath;

$data['thumb_img'] = $thumb_path;

return $data;

}else{

// 上传失败获取错误信息

return $file->getError();

}

}

}

以上这篇thinkphp5上传图片及生成缩略图公共方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/myarche/article/details/78770037

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 thinkphp5上传图片及生成缩略图公共方法(分享) https://www.kuaiidc.com/93555.html

相关文章

发表评论
暂无评论