From 5d87c5dbbc4a286af7c15c20046892d12d7acd02 Mon Sep 17 00:00:00 2001 From: Wisp X Date: Tue, 18 Jan 2022 11:07:32 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=94=B9=E8=BF=9B=E7=BC=A9?= =?UTF-8?q?=E7=95=A5=E5=9B=BE=E7=94=9F=E6=88=90=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/User/ImageController.php | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/app/Http/Controllers/User/ImageController.php b/app/Http/Controllers/User/ImageController.php index c24276a0..405651c7 100644 --- a/app/Http/Controllers/User/ImageController.php +++ b/app/Http/Controllers/User/ImageController.php @@ -9,6 +9,7 @@ use App\Http\Requests\ImageRenameRequest; use App\Models\Album; use App\Models\Image; use App\Models\User; +use App\Service\ImageService; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; use Illuminate\Http\Response; @@ -16,6 +17,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\View\View; +use Intervention\Image\ImageManager; use League\Flysystem\FilesystemException; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -90,7 +92,7 @@ class ImageController extends Controller return $this->success('success', compact('image')); } - public function output(Request $request): StreamedResponse + public function output(Request $request, ImageService $service): StreamedResponse { /** @var Image $image */ $image = Image::query() @@ -108,8 +110,9 @@ class ImageController extends Controller } // 是否启用了水印功能 if ($image->group->configs->get(GroupConfigKey::IsEnableWatermark)) { - // GroupConfigKey::WatermarkConfigs // TODO 动态生成水印并缓存 + $configs = $image->group->configs->get(GroupConfigKey::WatermarkConfigs); + $contents = (string)$service->stickWatermark($contents, collect($configs))->encode(); } return \response()->stream(function () use ($contents) { echo $contents; @@ -132,31 +135,17 @@ class ImageController extends Controller } else { $stream = $image->filesystem()->readStream($image->pathname); $img = \Intervention\Image\Facades\Image::make($stream); - switch (1) { - case $image->width >= 115200 && $image->height >= 64800: - $width = (int)($image->width / 32); - $height = (int)($image->height / 32); - break; - case $image->width >= 102400 && $image->height >= 43200: - $width = (int)($image->width / 28); - $height = (int)($image->height / 28); - break; - case $image->width >= 10240 && $image->height >= 5760: - $width = (int)($image->width / 10); - $height = (int)($image->height / 10); - break; - case $image->width >= 3840 && $image->height >= 2160: - $width = (int)($image->width / 8); - $height = (int)($image->height / 8); - break; - case $image->width >= 1920 && $image->height >= 1080: - $width = (int)($image->width / 6); - $height = (int)($image->height / 6); - break; - default: - $width = (int)($image->width / 3); - $height = (int)($image->height / 3); + + $w = $image->width; + $h = $image->height; + $width = $height = 400; + + if ($w > $width && $h > $height) { + $scale = min($width / $w, $height / $h); + $width = (int)($w * $scale); + $height = (int)($h * $scale); } + $contents = $img->fit($width, $height, fn($constraint) => $constraint->upsize())->encode(); Cache::rememberForever($cacheKey, fn () => (string)$contents); }