EasyImages2.0/application/process.php

150 lines
5.2 KiB
PHP
Raw Normal View History

2021-11-14 15:25:21 +00:00
<?php
2022-01-19 17:55:20 +00:00
2021-11-14 15:25:21 +00:00
require_once __DIR__ . '/function.php';
2022-02-04 12:28:33 +00:00
require_once __DIR__ . '/WaterMask.php';
2022-01-19 17:55:20 +00:00
// 压缩图片与图片鉴黄
2022-02-04 12:28:33 +00:00
function compress($absolutePath)
2021-11-14 15:25:21 +00:00
{
2022-01-01 07:40:23 +00:00
global $config;
// 压缩图片 后压缩模式,不影响前台输出速度
if ($config['compress']) {
if (!isAnimatedGif($absolutePath)) {
require_once __DIR__ . '/compress/Imagick/class.Imgcompress.php';
2022-01-27 09:25:46 +00:00
$percent = $config['compress_ratio'] / 100; // 压缩率
$img = new Imgcompress($absolutePath, $percent);
2022-01-01 07:40:23 +00:00
$img->compressImg($absolutePath);
// 释放
ob_flush();
flush();
}
2021-11-14 15:25:21 +00:00
}
2022-02-04 12:28:33 +00:00
}
2021-11-14 15:25:21 +00:00
2022-02-04 12:28:33 +00:00
// 设置水印
function water($source)
{
global $config;
// 文字水印
if ($config['watermark'] == 1) {
// 过滤gif
2022-05-03 17:23:09 +00:00
if (!isAnimatedGifWebp($source)) {
2022-02-04 12:28:33 +00:00
$arr = [
# 水印图片路径(如果不存在将会被当成是字符串水印)
'res' => $config['waterText'],
# 水印显示位置
'pos' => $config['waterPosition'],
# 不指定name(会覆盖原图也就是保存成thumb.jpeg)
'name' => $source,
'font' => APP_ROOT . $config['textFont'],
'fontSize' => $config['textSize'],
2022-03-12 06:19:45 +00:00
'color' => str_replace(array('rgba', '(', ')'), '', $config['textColor']),
2022-02-04 12:28:33 +00:00
];
Imgs::setWater($source, $arr);
}
}
// 图片水印
if ($config['watermark'] == 2) {
// 过滤gif
2022-05-03 17:23:09 +00:00
if (!isAnimatedGifWebp($source)) {
2022-02-04 12:28:33 +00:00
$arr = [
# 水印图片路径(如果不存在将会被当成是字符串水印)
'res' => APP_ROOT . $config['waterImg'],
# 水印显示位置
'pos' => $config['waterPosition'],
# 不指定name(会覆盖原图也就是保存成thumb.jpeg)
'name' => $source,
];
Imgs::setWater($source, $arr);
}
}
}
function process_checkImg($imgurl)
{
global $config;
2022-01-01 07:40:23 +00:00
// 图片违规检查
2022-01-31 19:51:02 +00:00
if ($config['checkImg'] == 1) {
2022-02-04 12:28:33 +00:00
checkImg($imgurl, 1);
2022-01-31 19:51:02 +00:00
}
if ($config['checkImg'] == 2) {
2022-02-04 12:28:33 +00:00
checkImg($imgurl, 2);
2021-11-14 15:25:21 +00:00
}
2022-01-19 17:55:20 +00:00
}
/**
* 写日志
2022-03-24 04:46:38 +00:00
*
* 格式:
* {
* 上传图片名称{
* source:源文件名称,
* date:上传日期(Asia/Shanghai),
* ip:上传者IP,port:IP端口,
* user_agent:上传者浏览器信息,
* path:文件相对路径,
* size:文件大小(格式化),
* md5:文件MD5,
* checkImg:鉴黄状态,
* form:上传方式web/API ID
* }
* }
*
2022-01-19 17:55:20 +00:00
* $filePath 文件相对路径
* $sourceName 源文件名称
* $absolutePath 图片的绝对路径
* $fileSize 图片的大小
2022-03-19 08:30:14 +00:00
* $form 来源如果是网页上传直接显示网页,如果是API上传则显示ID
2022-01-19 17:55:20 +00:00
*/
2022-03-19 08:30:14 +00:00
function write_log($filePath, $sourceName, $absolutePath, $fileSize, $from = "web")
2022-01-19 17:55:20 +00:00
{
global $config;
2022-09-20 05:44:48 +00:00
$checkImg = $config['checkImg'] == true ? "ON" : "OFF";
2022-03-24 04:46:38 +00:00
// $name = trim(basename($filePath), " \t\n\r\0\x0B"); // 当前图片名称
$log = array(basename($filePath) => array( // 以上传图片名称为Array
'source' => $sourceName, // 原始文件名称
'date' => date('Y-m-d H:i:s'), // 上传日期
'ip' => real_ip(), // 上传IP
'port' => $_SERVER['REMOTE_PORT'], // IP端口
'user_agent' => $_SERVER['HTTP_USER_AGENT'], // 浏览器信息
'path' => $filePath, // 文件相对路径
'size' => getDistUsed($fileSize), // 文件大小(格式化)
'md5' => md5_file($absolutePath), // 文件的md5
'checkImg' => $checkImg, // 鉴黄状态
'from' => $from, // 图片上传来源
2021-11-14 15:25:21 +00:00
));
2021-11-16 04:57:14 +00:00
// 创建日志文件夹
2022-01-01 07:40:23 +00:00
if (!is_dir(APP_ROOT . '/admin/logs/upload/')) {
mkdir(APP_ROOT . '/admin/logs/upload', 0755, true);
2021-11-16 04:57:14 +00:00
}
2022-05-22 07:06:50 +00:00
// logs文件组成
$logFileName = APP_ROOT . '/admin/logs/upload/' . date('Y-m') . '.php';
2022-01-30 06:46:29 +00:00
2022-05-22 07:06:50 +00:00
// 创建logs文件
2022-01-30 06:46:29 +00:00
if (!is_file($logFileName)) {
file_put_contents($logFileName, '<?php $logs=Array();?>');
}
2022-05-22 07:06:50 +00:00
// 引入logs
2022-01-30 06:46:29 +00:00
include $logFileName;
2022-05-22 07:06:50 +00:00
// // 写入禁止浏览器直接访问
// if (filesize($logFileName) == 0) {
// $php_exit = '<?php /** {图片名称{source:源文件名称,date:上传日期(Asia/Shanghai),ip:上传者IP,port:IP端口,user_agent:上传者浏览器信息,path:文件相对路径,size:文件大小(格式化),md5:文件MD5,checkImg:鉴黄状态,form:上传方式web/API ID}} */ exit;? >';
// file_put_contents($logFileName, $php_exit);
// }
// $log = json_encode($log, JSON_UNESCAPED_UNICODE);
// file_put_contents($logFileName, PHP_EOL . $log, FILE_APPEND | LOCK_EX);
2022-01-30 06:46:29 +00:00
$log = array_replace($logs, $log);
cache_write($logFileName, $log, 'logs');
2022-03-10 15:40:15 +00:00
}