fixed gallery

This commit is contained in:
2026-02-22 17:09:34 +01:00
parent 48e2055b6a
commit 5c97488e80
33 changed files with 2062 additions and 550 deletions

View File

@@ -1,20 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Illuminate\Support\Facades\Storage;
class ThumbnailService
{
// Use the thumbnails CDN host (HTTPS)
protected const CDN_HOST = 'https://files.skinbase.org';
/**
* CDN host is read from config/cdn.php FILES_CDN_URL env.
* Hardcoding the domain is forbidden per upload-agent spec §3A.
*/
protected static function cdnHost(): string
{
return rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/');
}
protected const VALID_SIZES = ['sm','md','lg','xl'];
/**
* Canonical size keys (upload-agent spec §8): thumb · sq · md · lg · xl
* 'sm' is kept as a backwards-compatible alias for 'thumb'.
*/
protected const VALID_SIZES = ['thumb', 'sq', 'sm', 'md', 'lg', 'xl'];
/** Size aliases: legacy 'sm' maps to the 'thumb' CDN directory. */
protected const SIZE_ALIAS = ['sm' => 'thumb'];
protected const THUMB_SIZES = [
'sm' => ['height' => 240, 'quality' => 78, 'dir' => 'sm'],
'md' => ['height' => 360, 'quality' => 82, 'dir' => 'md'],
'lg' => ['height' => 1200, 'quality' => 85, 'dir' => 'lg'],
'xl' => ['height' => 2400, 'quality' => 90, 'dir' => 'xl'],
'thumb' => ['height' => 320, 'quality' => 78, 'dir' => 'thumb'],
'sq' => ['height' => 512, 'quality' => 82, 'dir' => 'sq', 'square' => true],
'sm' => ['height' => 320, 'quality' => 78, 'dir' => 'thumb'], // alias for thumb
'md' => ['height' => 1024, 'quality' => 82, 'dir' => 'md'],
'lg' => ['height' => 1920, 'quality' => 85, 'dir' => 'lg'],
'xl' => ['height' => 2560, 'quality' => 90, 'dir' => 'xl'],
];
/**
@@ -26,7 +44,7 @@ class ThumbnailService
{
// If $filePath seems to be a content hash and $ext is provided, build directly
if (!empty($filePath) && !empty($ext) && preg_match('/^[0-9a-f]{16,128}$/i', $filePath)) {
$sizeKey = is_string($size) ? $size : (($size === 4) ? 'sm' : 'md');
$sizeKey = is_string($size) ? $size : (($size === 4) ? 'thumb' : 'md');
return self::fromHash($filePath, $ext, $sizeKey) ?: '';
}
@@ -39,7 +57,7 @@ class ThumbnailService
if ($art) {
$hash = $art->hash ?? null;
$extToUse = $ext ?? ($art->thumb_ext ?? null);
$sizeKey = is_string($size) ? $size : (($size === 4) ? 'sm' : 'md');
$sizeKey = is_string($size) ? $size : (($size === 4) ? 'thumb' : 'md');
if (!empty($hash) && !empty($extToUse)) {
return self::fromHash($hash, $extToUse, $sizeKey) ?: '';
}
@@ -68,11 +86,14 @@ class ThumbnailService
public static function fromHash(?string $hash, ?string $ext, string $sizeKey = 'md'): ?string
{
if (empty($hash) || empty($ext)) return null;
// Resolve alias (sm → thumb) then validate
$sizeKey = self::SIZE_ALIAS[$sizeKey] ?? $sizeKey;
$sizeKey = in_array($sizeKey, self::VALID_SIZES) ? $sizeKey : 'md';
$h = $hash;
$dir = self::THUMB_SIZES[$sizeKey]['dir'] ?? $sizeKey;
$h = $hash;
$h1 = substr($h, 0, 2);
$h2 = substr($h, 2, 2);
return sprintf('%s/%s/%s/%s/%s.%s', rtrim(self::CDN_HOST, '/'), $sizeKey, $h1, $h2, $h, $ext);
return sprintf('%s/%s/%s/%s/%s.%s', self::cdnHost(), $dir, $h1, $h2, $h, $ext);
}
/**
@@ -80,9 +101,9 @@ class ThumbnailService
*/
public static function srcsetFromHash(?string $hash, ?string $ext): ?string
{
$a = self::fromHash($hash, $ext, 'sm');
$b = self::fromHash($hash, $ext, 'md');
$a = self::fromHash($hash, $ext, 'thumb'); // 320px
$b = self::fromHash($hash, $ext, 'md'); // 1024px
if (!$a || !$b) return null;
return $a . ' 320w, ' . $b . ' 600w';
return $a . ' 320w, ' . $b . ' 1024w';
}
}