use think\image;
需要think-image支持,
安装:composer require topthink/think-image
tp6官方的上传返回的图片格式中有\这个斜杠,linux系统好像不识别它。
验证的rule格式,目前只写了图片类型的,其它的文件类型要自己增加。
要允许的参数比较多的话 使用逗号连接
$rule = [
'rule' => [
'mimeType' => 'jpg,png,JPG,JPEG,PNG,jpeg',
'size' => '2',
'image' => '1000,1000',
'suffix' => 'jpg,png'
],
'msg' => [
'mimeType' => '图片格式错误',
'size' => '图片大小超过4MB',
'image' => '图片尺寸不符合规范',
'suffix' => '图片后缀错误'
]
];
handleUploadFile($file, $rule = null, $type)
这个函数传入三个属性,
$file 是单个文件上传,获取的文件对象
$rule 是下面图片中的
file('image');
*/
namespace thinkFile;
use think\image;
class thinkFile
{
/**
* 检查上传文件的合法性
*
* @param $file
* @param $rule
* @return array
*/
public function handleCheck($file, $rule)
{
$rule_d = $rule['rule'];
foreach ($rule_d as $key => $val) {
switch ($key) {
case 'suffix':
$nameArr = explode('.', $file->getOriginalName());
$name = $nameArr[count($nameArr) - 1];
if (strpos($val, $name) !== false) {
} else {
return ['states' => false, 'msg' => $rule['msg'][$key]];
}
break;
case 'mimeType':
$type = explode('/', $file->getMime())[1];
if (strpos($val, $type) !== false) {
} else {
return ['states' => false, 'msg' => $rule['msg'][$key]];
}
break;
case 'size':
$size = (int)$file->getSize() / 1024000;
if ($size > $val) {
return ['states' => false, 'msg' => $rule['msg'][$key]];
}
break;
case 'image':
$image = Image::open($file->getRealPath());
$width = $image->width();
$height = $image->height();
$image = explode(',', $val);
if ($width > $image[0] || $height > $image[1]) {
return ['states' => false, 'msg' => $rule['msg'][$key]];
}
break;
default:
$pice_path = '/runtime/storage/other/';
break;
}
}
return ['states' => true, 'msg' => '效验正常'];
}
/**
* 按照类别上传文件到指定的目录
*
* @param $file
* @param null $rule
* @param $type
* @return array
* @throws \think\Exception
*/
public function handleUploadFile($file, $rule = null, $type)
{
$base_path = app()->getRootPath();
switch ($type) {
case 'view':
$pice_path = '/public/static/uploads/image/';
break;
case 'image':
$pice_path = '/runtime/storage/image/';
break;
case 'zip':
$pice_path = '/runtime/storage/zip/';
break;
case 'video':
$pice_path = '/runtime/storage/video/';
break;
case 'word':
$pice_path = '/runtime/storage/word/';
break;
default:
$pice_path = '/runtime/storage/other/';
break;
}
if ($rule == null) {
$status = ['states' => true, 'msg' => '无需效验'];
} else {
$status = $this->handleCheck($file, $rule);
}
if ($status['states'] == true) {
$type = explode('/', $file->getMime())[1];
$image_name = md5(date('Ymd-His') . mt_rand(1, 99999)) . "." . $type;
if (!file_exists($base_path . $pice_path)) {
mkdir($base_path . $pice_path, 0700);
}
$image_file_path = $pice_path . date('Ymd');
$image_file = $base_path . $image_file_path;
$imge_real_url = $image_file . '/' . $image_name;
$imge_web_url = $image_file_path . '/' . $image_name;
if (!file_exists($image_file)) {
mkdir($image_file, 0700);
// fopen($image_file . '/' . $image_name, "w");
}
$real_path = $file->getRealPath();
if (move_uploaded_file($real_path, $imge_real_url)) {
return ['code' => 0, 'data' => $imge_web_url, 'msg' => 'success', 'word' => '文件保存成功'];
} else {
return ['code' => 1, 'data' => null, 'msg' => 'error', 'word' => '上传失败'];
}
} else {
throw new \think\Exception($status['msg']);
}
}
}
如何使用?
try {
$thinkFile = new thinkFile();
$imge_web_url = $thinkFile->handleUploadFile($file, [
'rule' => [
'mimeType' => 'jpg,png,JPG,JPEG,PNG,jpeg',
'size' => '5',
'image' => '5000,5000',
'suffix' => 'jpg,png'
],
'msg' => [
'mimeType' => '图片格式错误',
'size' => '图片大小超过5MB',
'image' => '图片尺寸不符合规范',
'suffix' => '图片后缀错误'
]
], 'image');
return json(['code' => 0, 'data' => $imge_web_url['data'], 'msg' => 'success', 'word' => '上传完成']);
} catch (\think\Exception $e) {
return json(['code' => 1, 'data' => null, 'msg' => 'error', 'word' => $e->getMessage()]);
}