水印支持切换为覆盖原图或动态生成

This commit is contained in:
Wisp X 2022-05-23 17:28:45 +08:00
parent 315b5b8296
commit 4266fd60b7
6 changed files with 46 additions and 5 deletions

View File

@ -0,0 +1,9 @@
<?php
namespace App\Enums\Watermark;
final class Mode
{
const Overlay = 1; // 覆盖原图
const Dynamic = 2; // 动态生成
}

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Enums\GroupConfigKey;
use App\Enums\UserStatus;
use App\Enums\Watermark\Mode;
use App\Exceptions\UploadException;
use App\Http\Result;
use App\Models\Group;
@ -158,10 +159,15 @@ class Controller extends BaseController
$contents = Cache::get($cacheKey);
} else {
$contents = $image->filesystem()->read($image->pathname);
$configs = collect($image->group?->configs->get(GroupConfigKey::WatermarkConfigs));
// 是否启用了水印功能跳过gif和ico图片
if ($image->group?->configs->get(GroupConfigKey::IsEnableWatermark) && ! in_array($image->extension, ['ico', 'gif'])) {
$configs = $image->group?->configs->get(GroupConfigKey::WatermarkConfigs);
$contents = $service->stickWatermark($contents, collect($configs))->encode()->getEncoded();
if (
$image->group?->configs->get(GroupConfigKey::IsEnableWatermark) &&
$configs->get('mode', Mode::Overlay) == Mode::Dynamic &&
! in_array($image->extension, ['ico', 'gif'])
) {
$contents = $service->stickWatermark($contents, $configs)->encode()->getEncoded();
}
$cacheTtl = (int)$image->group?->configs->get(GroupConfigKey::ImageCacheTtl, 0);
// 是否启用了缓存

View File

@ -25,6 +25,7 @@ use App\Enums\UserConfigKey;
use App\Enums\UserStatus;
use App\Enums\Watermark\FontOption;
use App\Enums\Watermark\ImageOption;
use App\Enums\Watermark\Mode;
use App\Exceptions\UploadException;
use App\Models\Group;
use App\Models\Image;
@ -150,6 +151,17 @@ class ImageService
// 上传频率限制
$this->rateLimiter($configs, $request);
// 是否启用水印,覆盖原图片
if (
$configs->get(GroupConfigKey::IsEnableWatermark) &&
collect($configs->get(GroupConfigKey::WatermarkConfigs))->get('mode', Mode::Overlay) == Mode::Overlay &&
! in_array($extension, ['ico', 'gif'])
) {
$watermarkImage = $this->stickWatermark($file, collect($configs->get(GroupConfigKey::WatermarkConfigs)));
$watermarkImage->save();
$file = new UploadedFile($watermarkImage->basePath(), $file->getClientOriginalName(), $file->getMimeType());
}
$filename = $this->replacePathname(
$configs->get(GroupConfigKey::PathNamingRule).'/'.$configs->get(GroupConfigKey::FileNamingRule), $file,
);

View File

@ -13,6 +13,7 @@ use App\Enums\Scan\TencentOption;
use App\Enums\UserConfigKey;
use App\Enums\Watermark\FontOption;
use App\Enums\Watermark\ImageOption;
use App\Enums\Watermark\Mode;
return [
'app' => [
@ -75,6 +76,7 @@ return [
],
],
GroupConfigKey::WatermarkConfigs => [
'mode' => Mode::Overlay,
'driver' => 'font',
'drivers' => [
'font' => [

View File

@ -204,9 +204,15 @@
<div data-tab="watermark" class="hidden grid grid-cols-6 gap-6">
<p class="mb-3 text-red-600 text-sm"><i class="fas fa-exclamation"></i> 开启水印功能前请注意考虑图片版权问题。</p>
<div class="col-span-6 mb-4">
<x-fieldset title="开启水印" faq="请注意,水印功能仅在开启了「原图保护」功能的情况下生效。">
<x-fieldset title="开启水印" faq="请注意,水印模式为动态生成时,仅在开启了「原图保护」功能的情况下生效。">
<x-switch id="configs[is_enable_watermark]" name="configs[is_enable_watermark]" value="1"></x-switch>
</x-fieldset>
<div class="col-span-6 mt-4 mb-4">
<x-fieldset title="水印模式">
<x-fieldset-radio id="configs[watermark_configs][mode]_overlay" name="configs[watermark_configs][mode]" value="{{ \App\Enums\Watermark\Mode::Overlay }}" checked>覆盖原图</x-fieldset-radio>
<x-fieldset-radio id="configs[watermark_configs][mode]_dynamic" name="configs[watermark_configs][mode]" value="{{ \App\Enums\Watermark\Mode::Dynamic }}">动态生成</x-fieldset-radio>
</x-fieldset>
</div>
<div class="col-span-6 mt-4 mb-4">
<x-fieldset title="水印类型">
<x-fieldset-radio id="configs[watermark_configs][driver]_font" name="configs[watermark_configs][driver]" data-select="watermark" value="font" checked>文字水印</x-fieldset-radio>

View File

@ -204,9 +204,15 @@
<div data-tab="watermark" class="hidden grid grid-cols-6 gap-6">
<p class="mb-3 text-red-600 text-sm"><i class="fas fa-exclamation"></i> 开启水印功能前请注意考虑图片版权问题。</p>
<div class="col-span-6 mb-4">
<x-fieldset title="开启水印" faq="请注意,水印功能仅在开启了「原图保护」功能的情况下生效。">
<x-fieldset title="开启水印" faq="请注意,水印模式为动态生成时,仅在开启了「原图保护」功能的情况下生效。">
<x-switch id="configs[is_enable_watermark]" name="configs[is_enable_watermark]" value="1" :checked="(bool)$group->configs->get('is_enable_watermark')"></x-switch>
</x-fieldset>
<div class="col-span-6 mt-4 mb-4">
<x-fieldset title="水印模式">
<x-fieldset-radio id="configs[watermark_configs][mode]_overlay" name="configs[watermark_configs][mode]" value="{{ \App\Enums\Watermark\Mode::Overlay }}" :checked="($group->configs['watermark_configs']['mode'] ?? '') === 'overlay'">覆盖原图</x-fieldset-radio>
<x-fieldset-radio id="configs[watermark_configs][mode]_dynamic" name="configs[watermark_configs][mode]" value="{{ \App\Enums\Watermark\Mode::Dynamic }}" :checked="($group->configs['watermark_configs']['dynamic'] ?? '') === 'dynamic'">动态生成</x-fieldset-radio>
</x-fieldset>
</div>
<div class="col-span-6 mt-4 mb-4">
<x-fieldset title="水印类型">
<x-fieldset-radio id="configs[watermark_configs][driver]_font" name="configs[watermark_configs][driver]" data-select="watermark" value="font" :checked="($group->configs['watermark_configs']['driver'] ?? '') === 'font'">文字水印</x-fieldset-radio>