2021-10-27 13:36:58 +00:00
|
|
|
|
<?php
|
2022-04-29 08:47:23 +00:00
|
|
|
|
|
|
|
|
|
namespace Verot\Upload;
|
|
|
|
|
|
2022-01-03 00:14:18 +00:00
|
|
|
|
require_once __DIR__ . '/../application/function.php';
|
2021-11-09 03:43:23 +00:00
|
|
|
|
require_once APP_ROOT . '/application/class.upload.php';
|
2021-10-27 13:36:58 +00:00
|
|
|
|
require_once APP_ROOT . '/config/api_key.php';
|
|
|
|
|
|
2022-01-02 12:12:16 +00:00
|
|
|
|
header('Access-Control-Allow-Origin:*');
|
2021-10-27 13:36:58 +00:00
|
|
|
|
|
2022-01-19 17:55:20 +00:00
|
|
|
|
// 黑/白IP名单上传
|
|
|
|
|
if ($config['check_ip']) {
|
|
|
|
|
if (checkIP(null, $config['check_ip_list'], $config['check_ip_model'])) {
|
|
|
|
|
// 上传错误 code:403 未授权IP
|
|
|
|
|
exit(json_encode(array(
|
|
|
|
|
"result" => "failed",
|
2022-04-11 05:35:48 +00:00
|
|
|
|
"code" => 401,
|
2022-01-19 17:55:20 +00:00
|
|
|
|
"message" => "黑名单内或白名单外用户不允许上传",
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-11 05:35:48 +00:00
|
|
|
|
$token = preg_replace('/[\W]/', '', $_POST['token']); // 获取Token并过滤非字母数字,删除空格;
|
|
|
|
|
|
|
|
|
|
// 检查api合法性
|
|
|
|
|
check_api($token);
|
|
|
|
|
$tokenID = $tokenList[$token]['id'];
|
|
|
|
|
|
|
|
|
|
if (empty($_FILES['image'])) {
|
|
|
|
|
exit(json_encode(
|
|
|
|
|
array(
|
|
|
|
|
"result" => "NoFile",
|
|
|
|
|
"code" => 402,
|
|
|
|
|
"message" => "没有选择上传的文件",
|
|
|
|
|
)
|
|
|
|
|
));
|
|
|
|
|
}
|
2022-01-19 17:55:20 +00:00
|
|
|
|
|
2021-10-27 13:36:58 +00:00
|
|
|
|
$handle = new Upload($_FILES['image'], 'zh_CN');
|
|
|
|
|
|
|
|
|
|
if ($handle->uploaded) {
|
|
|
|
|
// 允许上传的mime类型
|
|
|
|
|
$handle->allowed = array('image/*');
|
|
|
|
|
// 文件命名
|
2022-05-03 05:00:40 +00:00
|
|
|
|
$handle->file_new_name_body = imgName($handle->file_src_name_body);
|
|
|
|
|
// 添加Token ID
|
|
|
|
|
$handle->file_name_body_add = '-' . $tokenID;
|
2021-10-27 13:36:58 +00:00
|
|
|
|
// 最大上传限制
|
|
|
|
|
$handle->file_max_sizes = $config['maxSize'];
|
|
|
|
|
// 最大宽度
|
|
|
|
|
$handle->image_max_width = $config['maxWidth'];
|
|
|
|
|
// 最大高度
|
|
|
|
|
$handle->image_max_height = $config['maxHeight'];
|
|
|
|
|
// 最小宽度
|
|
|
|
|
$handle->image_min_width = $config['minWidth'];
|
|
|
|
|
// 最小高度
|
|
|
|
|
$handle->image_min_height = $config['minHeight'];
|
|
|
|
|
// 转换图片为指定格式
|
2022-05-03 05:00:40 +00:00
|
|
|
|
if ($config['imgConvert']) {
|
2022-03-10 15:40:15 +00:00
|
|
|
|
// 只转换非webp格式和非动态图片
|
|
|
|
|
if ($handle->file_src_name_ext !== 'webp' && !isAnimatedGif($handle->file_src_pathname)) {
|
|
|
|
|
$handle->image_convert = $config['imgConvert'];
|
2022-05-03 05:00:40 +00:00
|
|
|
|
// PNG 图像的压缩级别,介于 1(快速但大文件)和 9(慢但较小文件)之间
|
|
|
|
|
$handle->png_compression = 9 - round($config['compress_ratio'] / 11.2);
|
|
|
|
|
// WEBP 图像的压缩质量 1-100
|
|
|
|
|
$handle->webp_quality = $config['compress_ratio'];
|
|
|
|
|
// JPEG 图像的压缩质量 1-100
|
|
|
|
|
$handle->jpeg_quality = $config['compress_ratio'];
|
2022-03-10 15:40:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-27 13:36:58 +00:00
|
|
|
|
|
2022-05-03 05:00:40 +00:00
|
|
|
|
/* 等比例缩减图片 放到前端了*/
|
|
|
|
|
/*
|
|
|
|
|
if ($config['imgRatio']) {
|
|
|
|
|
$handle->image_resize = true;
|
|
|
|
|
$handle->image_x = $config['image_x'];
|
|
|
|
|
$handle->image_y = $config['image_y'];
|
|
|
|
|
// 如果调整后的图像大于原始图像,则取消调整大小,以防止放大
|
|
|
|
|
$handle->image_no_enlarging = true;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 默认目录
|
|
|
|
|
$Img_path = config_path();
|
|
|
|
|
|
|
|
|
|
if ($config['token_path_status'] == 1) {
|
|
|
|
|
$Img_path = config_path($tokenID . date('/Y/m/d/'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 13:36:58 +00:00
|
|
|
|
// 存储图片路径:images/201807/
|
2022-05-03 05:00:40 +00:00
|
|
|
|
$handle->process(APP_ROOT . $Img_path);
|
2021-10-27 13:36:58 +00:00
|
|
|
|
|
|
|
|
|
// 图片完整相对路径:/i/2021/05/03/k88e7p.jpg
|
|
|
|
|
if ($handle->processed) {
|
|
|
|
|
header('Content-type:text/json');
|
|
|
|
|
// 上传成功后返回json数据
|
2022-05-03 11:14:46 +00:00
|
|
|
|
// 图片相对路径
|
|
|
|
|
$pathIMG = $Img_path . $handle->file_dst_name;
|
|
|
|
|
// 图片访问网址
|
2022-05-03 05:00:40 +00:00
|
|
|
|
$imageUrl = rand_imgurl() . $pathIMG;
|
2022-05-03 11:14:46 +00:00
|
|
|
|
// 后续处理地址
|
|
|
|
|
$processUrl = $config['domain'] . $pathIMG;
|
2022-01-17 19:13:56 +00:00
|
|
|
|
|
2022-04-29 08:47:23 +00:00
|
|
|
|
// 原图保护 key值是由crc32加密的hide_key
|
2022-05-03 05:00:40 +00:00
|
|
|
|
// $hide_original = $config['hide'] == 1 ? $config['domain'] . '/application/hide.php?key=' . urlHash($pathIMG, 0, crc32($config['hide_key'])) : $imageUrl;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 以下为控制开启源图保护或者返回值隐藏config文件中的path目录所更改
|
|
|
|
|
* 2022年5月1日
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 隐藏config文件中的path目录,需要搭配网站设置
|
|
|
|
|
if ($config['hide_path'] == 1) {
|
|
|
|
|
$imageUrl = str_replace($config['path'], '/', $imageUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 源图保护 key值是由crc32加密的hide_key
|
|
|
|
|
if ($config['hide'] == 1) {
|
|
|
|
|
$imageUrl = $config['domain'] . '/application/hide.php?key=' . urlHash($pathIMG, 0, crc32($config['hide_key']));
|
|
|
|
|
}
|
2022-03-14 09:52:21 +00:00
|
|
|
|
|
2022-01-27 09:25:46 +00:00
|
|
|
|
// 关闭上传后显示加密删除链接
|
|
|
|
|
if ($config['show_user_hash_del']) {
|
|
|
|
|
// 判断PHP版本启用删除
|
|
|
|
|
if (PHP_VERSION >= '7') {
|
2022-02-05 08:23:57 +00:00
|
|
|
|
$delUrl = $config['domain'] . '/application/del.php?hash=' . urlHash($pathIMG, 0);
|
2022-01-27 09:25:46 +00:00
|
|
|
|
} else {
|
|
|
|
|
$delUrl = "Sever PHP version lower 7.0";
|
|
|
|
|
}
|
2022-01-17 19:13:56 +00:00
|
|
|
|
} else {
|
2022-01-27 09:25:46 +00:00
|
|
|
|
$delUrl = "Admin closed delete";
|
2022-01-17 19:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 13:36:58 +00:00
|
|
|
|
$reJson = array(
|
2022-02-05 08:23:57 +00:00
|
|
|
|
"result" => "success",
|
|
|
|
|
"code" => 200,
|
|
|
|
|
"url" => $imageUrl,
|
|
|
|
|
"srcName" => $handle->file_src_name_body,
|
|
|
|
|
"thumb" => $config['domain'] . '/application/thumb.php?img=' . $pathIMG,
|
|
|
|
|
"del" => $delUrl,
|
2021-10-27 13:36:58 +00:00
|
|
|
|
);
|
2022-01-03 00:14:18 +00:00
|
|
|
|
echo json_encode($reJson, JSON_UNESCAPED_UNICODE);
|
2021-10-27 13:36:58 +00:00
|
|
|
|
$handle->clean();
|
|
|
|
|
} else {
|
2022-03-14 09:52:21 +00:00
|
|
|
|
// 上传错误 code:400 客户端文件有问题
|
2021-10-27 13:36:58 +00:00
|
|
|
|
$reJson = array(
|
2022-01-02 12:12:16 +00:00
|
|
|
|
"result" => "failed",
|
2022-03-14 09:52:21 +00:00
|
|
|
|
"code" => 400,
|
2022-01-01 07:40:23 +00:00
|
|
|
|
"message" => $handle->error,
|
2021-10-27 13:36:58 +00:00
|
|
|
|
);
|
2022-01-02 12:12:16 +00:00
|
|
|
|
|
|
|
|
|
exit(json_encode($reJson, JSON_UNESCAPED_UNICODE));
|
2021-10-27 13:36:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 12:28:33 +00:00
|
|
|
|
/** 后续处理 */
|
|
|
|
|
require APP_ROOT . '/application/process.php';
|
2021-10-27 13:36:58 +00:00
|
|
|
|
|
2022-02-04 12:28:33 +00:00
|
|
|
|
// 使用fastcgi_finish_request操作
|
|
|
|
|
if (function_exists('fastcgi_finish_request')) {
|
|
|
|
|
fastcgi_finish_request();
|
2022-03-19 08:30:14 +00:00
|
|
|
|
// 鉴黄
|
2022-05-03 11:14:46 +00:00
|
|
|
|
@process_checkImg($processUrl);
|
2022-02-04 12:28:33 +00:00
|
|
|
|
// 日志
|
2022-04-11 05:35:48 +00:00
|
|
|
|
if ($config['upload_logs']) @write_log($pathIMG, $handle->file_src_name, $handle->file_dst_pathname, $handle->file_src_size, $tokenID);
|
2022-02-04 12:28:33 +00:00
|
|
|
|
// 水印
|
|
|
|
|
@water($handle->file_dst_pathname);
|
|
|
|
|
// 压缩
|
|
|
|
|
@compress($handle->file_dst_pathname);
|
|
|
|
|
} else {
|
2022-03-19 08:30:14 +00:00
|
|
|
|
// 鉴黄
|
2022-05-03 11:14:46 +00:00
|
|
|
|
@process_checkImg($processUrl);
|
2022-02-04 12:28:33 +00:00
|
|
|
|
// 日志
|
2022-04-11 05:35:48 +00:00
|
|
|
|
if ($config['upload_logs']) write_log($pathIMG, $handle->file_src_name, $handle->file_dst_pathname, $handle->file_src_size, $tokenID);
|
2022-02-04 12:28:33 +00:00
|
|
|
|
// 水印
|
|
|
|
|
@water($handle->file_dst_pathname);
|
|
|
|
|
// 压缩
|
|
|
|
|
@compress($handle->file_dst_pathname);
|
|
|
|
|
}
|
2022-02-13 08:14:46 +00:00
|
|
|
|
|
2021-10-27 13:36:58 +00:00
|
|
|
|
unset($handle);
|
|
|
|
|
}
|