79 lines
2.3 KiB
PHP
79 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
use App\Services\ThumbnailService;
|
|
|
|
class Thumb
|
|
{
|
|
/**
|
|
* Return a thumbnail URL.
|
|
*
|
|
* Usage:
|
|
* - `Thumb::url($filePath)` - fallback mapping by filename or Storage::url
|
|
* - `Thumb::url($filePath, $id, $ext)` - resolve hash-based CDN URL when possible
|
|
*
|
|
* @param string|null $filePath
|
|
* @param int|null $id
|
|
* @param string|null $ext
|
|
* @param int $size // legacy size code: 4 -> small(320), 6 -> medium(600)
|
|
* @return string
|
|
*/
|
|
public static function url(?string $filePath, ?int $id = null, ?string $ext = null, int $size = 6): string
|
|
{
|
|
return ThumbnailService::url($filePath, $id, $ext, $size);
|
|
}
|
|
|
|
/**
|
|
* Build a new-style thumbnail URL using hash and extension.
|
|
* Example: http://files.skinbase.org/md/43/f8/43f87a...360.webp
|
|
*
|
|
* @param string|null $hash
|
|
* @param string|null $ext
|
|
* @param string $sizeKey One of sm, md, lg, xl
|
|
* @return string|null
|
|
*/
|
|
public static function fromHash(?string $hash, ?string $ext, string $sizeKey = 'md'): ?string
|
|
{
|
|
return ThumbnailService::fromHash($hash, $ext, $sizeKey);
|
|
}
|
|
|
|
/**
|
|
* Build a simple srcset for responsive thumbnails.
|
|
* Uses sm (320w) and md (600w) by default to match legacy sizes.
|
|
*
|
|
* @param string|null $hash
|
|
* @param string|null $ext
|
|
* @return string|null
|
|
*/
|
|
public static function srcsetFromHash(?string $hash, ?string $ext): ?string
|
|
{
|
|
return ThumbnailService::srcsetFromHash($hash, $ext);
|
|
}
|
|
|
|
/**
|
|
* Return encoded id string using legacy algorithm or fallback base62.
|
|
*/
|
|
public static function encodeId(int $id): string
|
|
{
|
|
if (class_exists('\App\Services\LegacyService') && method_exists('\App\Services\LegacyService', 'encode')) {
|
|
return \App\Services\LegacyService::encode($id);
|
|
}
|
|
|
|
return self::base62encode($id);
|
|
}
|
|
|
|
private static function base62encode(int $val, int $base = 62, string $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'): string
|
|
{
|
|
$str = '';
|
|
if ($val < 0) return $str;
|
|
do {
|
|
$i = $val % $base;
|
|
$str = $chars[$i] . $str;
|
|
$val = intdiv($val - $i, $base);
|
|
} while ($val > 0);
|
|
return $str;
|
|
}
|
|
}
|