optimizations

This commit is contained in:
2026-03-28 19:15:39 +01:00
parent 0b25d9570a
commit cab4fbd83e
509 changed files with 1016804 additions and 1605 deletions

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace App\Services\NovaCards;
use App\Models\NovaCardBackground;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Encoders\WebpEncoder;
use Intervention\Image\ImageManager;
use RuntimeException;
class NovaCardBackgroundService
{
private ?ImageManager $manager = null;
public function __construct()
{
try {
$this->manager = extension_loaded('gd') ? ImageManager::gd() : ImageManager::imagick();
} catch (\Throwable) {
$this->manager = null;
}
}
public function storeUploadedBackground(User $user, UploadedFile $file): NovaCardBackground
{
if ($this->manager === null) {
throw new RuntimeException('Nova card background processing requires Intervention Image.');
}
$uuid = (string) Str::uuid();
$extension = strtolower($file->getClientOriginalExtension() ?: 'jpg');
$originalDisk = Storage::disk((string) config('nova_cards.storage.private_disk', 'local'));
$processedDisk = Storage::disk((string) config('nova_cards.storage.public_disk', 'public'));
$originalPath = trim((string) config('nova_cards.storage.background_original_prefix', 'cards/backgrounds/original'), '/')
. '/' . $user->id . '/' . $uuid . '.' . $extension;
$processedPath = trim((string) config('nova_cards.storage.background_processed_prefix', 'cards/backgrounds/processed'), '/')
. '/' . $user->id . '/' . $uuid . '.webp';
$originalDisk->put($originalPath, file_get_contents($file->getRealPath()) ?: '');
$image = $this->manager->read($file->getRealPath())->scaleDown(width: 2200, height: 2200);
$encoded = (string) $image->encode(new WebpEncoder(88));
$processedDisk->put($processedPath, $encoded);
return NovaCardBackground::query()->create([
'user_id' => $user->id,
'original_path' => $originalPath,
'processed_path' => $processedPath,
'width' => $image->width(),
'height' => $image->height(),
'mime_type' => (string) ($file->getMimeType() ?: 'image/jpeg'),
'file_size' => (int) $file->getSize(),
'sha256' => hash_file('sha256', $file->getRealPath()) ?: null,
'visibility' => 'card-only',
]);
}
}