ThinkPHP5.0 图片上传生成缩略图常见问题与解决
许多开发者在实践图片上传并自动生成缩略图功能时,常遭遇环境差异导致的故障:在 Windows 本地环境中运行无误,但部署至 Linux 服务器后却频频出错。
笔者也曾深陷此类困境,查阅网络资料后发现,大量观点将其归咎于服务器临时文件目录的权限配置不当。
经过深入分析与排查,结论并非如此简单。
核心症结在于:保存于变量中的文件信息往往滞后,当文件被 move 方法转移至指定目录后,原有的临时文件即被系统清除。此时若再生成缩略图,open 操作必须指向自定义目录下的新文件路径,而非 move 之前的旧信息,但诸多示例文档仍沿用错误的路径引用。
此外,Windows 环境下 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 图片上传生成缩略图实例代码说明,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对本站网站的支持!