欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ThinkPHP5.0 图片上传生成缩略图实例代码说明

程序员文章站 2022-05-28 11:45:32
很多朋友遇到这样一个问题,图片上传生成缩略图,很多人在本机(win)测试成功,上传到linux 服务器后错误。 我也遇到同样的问题。网上一查,有无数的人说是服务器临时文件...

很多朋友遇到这样一个问题,图片上传生成缩略图,很多人在本机(win)测试成功,上传到linux 服务器后错误。

我也遇到同样的问题。网上一查,有无数的人说是服务器临时文件目录权限问题。

几经思考后,发现并非如此。

其根本的原因是,保存到变量的信息是之前的,之后又move移动到了自己指定的目录下,同时临时文件已经不存在。所以再生成缩略图的时候,需要open的,文件地址应该是自己定义的目录+文件名。然而很多实例文档中,还是使用的move 之前的信息。

又加之在win服务器下,move后,指定目录已生成了文件,同时临时文件未被删除。所以能用move之前的信息生成缩略图。

希望不多的言语能帮助遇到同样问题的你。

下面在通过实例代码给大家介绍thinkphp5.0 图片上传生成缩略图的方法。

代码如下所示:

<?php
namespace app\common\controller;
use app\common\model\goods;
class tools
{
 public static function upload_goods_img($wherename="", $width="", $height="")
 {
 // 打开图片的相对路径
 $imgpath = config('img_path');
 // 绝对路径
 $imgrootpath = config('imgrootpath');
 $storeid = '自定义';
 $merchantid = '自定义';
 $old_filename = $storeid . $merchantid . time();
 $filename = $storeid . $merchantid . time() . mt_rand(1000, 9999);
 $type = goods::upload($wherename, $old_filename);
 if($type) 
 {
  $savepath = $imgrootpath . '/' . $wherename . '/' . $filename . '.' . $type;
  $thumbfile = $filename . '.' . $type;
  $thumbname = $imgpath . '/' . $wherename . '/' . $thumbfile;
  $image = \think\image::open($imgpath . '/'. $wherename .'/' . $old_filename . '.' . $type);
  $image->thumb($width, $height, \think\image::thumb_fixed)->save($thumbname);
  $data = [
  'access_url' => $imgrootpath . '/' . $wherename . '/' . $filename . '.' . $type,
  'filename' => $thumbfile,
  ];
  return $data;
 }
 } 
}

调用:

class goods
{
 public function upload_sku()
 {
 $wherename = 'goods/sku';
 $width = 750;
 $height = 750;
 $data = tools::upload_goods_img($wherename,$width, $height);
 return returnjson(1, '上传成功', $data);;
 }
}

ps:下面在看一段代码tp5中上传图片方法,并生成相应缩略图的方法

//接收上传文件的name
$file = $this->_req->file("upload_head_image");
//将上传的文件移动到public/uploads/user
$info = $file->validate(['size'=>5242880,'ext'=>'jpg,jpeg,png'])->move(root_path . 'public' . ds . 'uploads' . ds . 'user');
if($info){
 $pic = new \app\home\model\user();
 $pic_url = $pic->thumbimage($file,$info);
 $user['portrait'] = 'uploads/user/'.$pic_url;
 //print_r($pic_url);exit();
 }

///model中代码如下
 /**
 * [生成用户头像缩略图,180、50]
 * @param [type] $file [获取上传文件$_file]
 * @param [type] $pic [上传文件的路径]
 * @return [type] [返回处理后的文件路径]
 */
 public function thumbimage($file,$pic){
 $image = \think\image::open($file);
 $getsavename = str_replace('\\','/',$pic->getsavename());
$portrait_thumbnail_180= 'uploads/user/'.str_replace($pic->getfilename(),'180_'.$pic->getfilename(),$getsavename);
$image->thumb(180,180,\think\image::thumb_center)->save(root_path . 'public' . ds . $portrait_thumbnail_180,null,100,true);
 $portrait_thumbnail_80 = 'uploads/user/'.str_replace($pic->getfilename(),'80_'.$pic->getfilename(),$getsavename);
 $image->thumb(80,80,\think\image::thumb_center)->save(root_path . 'public' . ds . $portrait_thumbnail_80,null,100,true);
 $portrait_thumbnail_50 = 'uploads/user/'.str_replace($pic->getfilename(),'50_'.$pic->getfilename(),$getsavename);
 $image->thumb(50,50,\think\image::thumb_center)->save(root_path . 'public' . ds . $portrait_thumbnail_50,null,100,true);
if ($image) {
  return $getsavename;
 }
 }

总结

以上所述是小编给大家介绍的thinkphp5.0 图片上传生成缩略图实例代码说明,希望对大家有所帮助