Added automatic watermark features. Closes (#196, #186, #138, #85, #75)

This commit is contained in:
WispX 2021-04-21 16:12:55 +08:00
parent 5e4d4a7161
commit 2ed547a735
6 changed files with 120 additions and 3 deletions

View File

@ -15,3 +15,4 @@ prefix = lsky_
single_user_mode = false
intercept_salacity = false
url_query = ''
watermark = false

View File

@ -15,6 +15,7 @@ use GuzzleHttp\Client;
use think\Controller;
use think\Exception;
use think\facade\Config;
use think\Image;
class Upload extends Controller
{
@ -90,6 +91,8 @@ class Upload extends Controller
}
}
$temp = $image->getPathname();
// 当前储存策略
$currentStrategy = strtolower($this->group->strategy);
$pathname = $this->makePathname($image->getInfo('name'));
@ -103,6 +106,45 @@ class Upload extends Controller
}
$url = make_url($domain, $pathname);
// 自动水印
if (Config::get('system.watermark') && $watermarkConfig = config("watermark.{$currentStrategy}")) {
if ($watermarkConfig['enable']) {
$watermarkImage = app()->getRuntimePath() . 'temp/' . md5($sha1.$md5);
$locates = [
1 => Image::WATER_NORTHWEST, 2 => Image::WATER_NORTH, 3 => Image::WATER_NORTHEAST,
4 => Image::WATER_WEST, 5 => Image::WATER_CENTER, 6 => Image::WATER_EAST,
7 => Image::WATER_SOUTHWEST, 8 => Image::WATER_SOUTH, 9 => Image::WATER_SOUTHEAST,
];
switch ($watermarkConfig['type']) {
case 1:
$watermark = Image::open($image)->text(
$watermarkConfig['text'],
$watermarkConfig['font'],
$watermarkConfig['size'],
$watermarkConfig['color'],
$locates[$watermarkConfig['locate']],
$watermarkConfig['offset'],
$watermarkConfig['angle']
);
break;
case 2:
$watermark = Image::open($image)->water(
$watermarkConfig['source'],
$watermarkConfig['locate'],
$watermarkConfig['alpha']
);
break;
default:
throw new Exception('自动水印功能配置异常');
}
$watermark->save($watermarkImage);
$temp = $watermarkImage;
$sha1 = sha1_file($temp);
$md5 = md5_file($temp);
$size = filesize($temp);
}
}
// 检测是否存在该图片,有则直接返回
if ($oldImage = Images::where('md5', $md5)->where('sha1', $sha1)->where('strategy', $currentStrategy)->find()) {
$pathname = $oldImage->pathname;
@ -110,13 +152,15 @@ class Upload extends Controller
goto exist;
}
if (!$this->strategy->create($pathname, $image->getPathname())) {
if (!$this->strategy->create($pathname, $temp)) {
if (Config::get('app.app_debug')) {
throw new Exception($this->strategy->getError());
}
throw new Exception('上传失败,请检查策略配置是否正确!');
}
isset($watermarkImage) && @unlink($watermarkImage);
exist:
// 图片鉴黄

View File

@ -12,5 +12,7 @@ return [
// 违规图片是否直接拦截
'intercept_salacity' => env('system.intercept_salacity', false),
// 图片链接额外参数(例: ?test=123)
'url_query' => env('system.url_query', '')
'url_query' => env('system.url_query', ''),
// 是否开启策略自动水印功能
'watermark' => env('system.watermark')
];

70
config/watermark.php Normal file
View File

@ -0,0 +1,70 @@
<?php
/**
* 水印配置文件(是否启用受环境变量中的 watermark 配置限制)
*
* 每个储存策略都可以设置独立的水印配置,分为文字水印和图片水印两种类型。
*
* 文字水印配置说明:
* `enable`: 是否启用水印功能
* `type`: 类型[1=文字水印,2=图片水印]
* `text`: 水印文字
* `font`: 字体文件绝对路径
* `size`: 文件大小
* `color`: 颜色
* `locate`: 水印位置[1=左上角,2=上居中,3=右上角,4=左居中,5=居中,6=右居中,7=左下角,8=下居中,9=右下角]
* `offset`: 文字相对当前位置的偏移量默认0
* `angle`: 文字倾斜角度默认0
*
* 图片水印配置说明
* `enable`: 是否启用水印功能
* `type`: 类型[1=文字水印,2=图片水印]
* `source`: 水印图片文件绝对路径
* `locate`: 水印位置[1=左上角,2=上居中,3=右上角,4=左居中,5=居中,6=右居中,7=左下角,8=下居中,9=右下角]
* `alpha`: 透明度
*
* FAQ
* 1.字体、水印图片如何配置?
* 路径下载你的字体文件或水印图片放置到项目任意目录, 变量 $root 则是项目根目录,即 public 上层目录
* 假如现在有个 test.ttf 字体文件,将它放置到 public 目录下,那么路径则是 $root . 'public/test.ttf'
* 2.文字偏移量(offset)和倾斜角度(angle)支持负数
* 3.每个策略的水印配置,只能同时存在一种类型,不可以同时配置文字水印和图片水印
* 4.启用水印功能后,系统不会保存原图!
*/
$root = app()->getRootPath();
return [
// 本地
'local' => [
'enable' => false,
'type' => 1,
'text' => 'Lsky Pro',
'font' => $root . 'public/static/app/font/test.ttf',
'size' => 20,
'color' => '#333333',
'locate' => 9,
'offset' => 0,
'angle' => 0,
],
// 阿里云 OSS
'oss' => [
'enable' => false,
'type' => 2,
'source' => $root . 'public/static/app/images/icon.png',
'locate' => 9,
'alpha' => 100,
],
// 腾讯云 COS
'cos' => [
'enable' => false,
],
// 又拍云 USS
'uss' => [
'enable' => false,
],
// 七牛云 Kodo
'kodo' => [
'enable' => false,
],
];

View File

@ -61,7 +61,7 @@ class Local implements Driver
{
$path = $this->rootPath . dirname($pathname) . DIRECTORY_SEPARATOR;
if (true === $this->checkPath($path)) {
if (move_uploaded_file($file, $pathname)) {
if (rename($file, $pathname)) {
return true;
}
}

Binary file not shown.