inhaus-plugin-verleih/src/Photos.php
Friederich Loheide 14aaa16b30 Verleih 1.0.0: Verleihsystem für Inhaus
Übersicht mit Status und Standort, Anfragen im Portal (Benachrichtigung +
E-Mail ans Team), Zusagen/Absagen mit automatischem Eintrag, Zurücknehmen,
Recht „Verleih verwalten“, einstellbare Bezeichnungen und Übernahme aus dem
WordPress-Plugin „FL-Verleihsystem“ (WXR mit Bildern).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 15:17:47 +00:00

67 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Inhaus\Plugins\Verleih;
use App\Support\Images;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Inhaus\Plugins\Verleih\Models\Item;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Encoders\WebpEncoder;
use Intervention\Image\ImageManager;
use Throwable;
/**
* Foto eines Objekts: verkleinert (max. 2000 px) und als Vorschau, beides ohne Zuschnitt –
* bei Kunstwerken soll nichts abgeschnitten werden. Nur für angemeldete Personen abrufbar.
*/
class Photos
{
public const DISK = 'local';
public function store(Item $item, UploadedFile $file): void
{
try {
Images::withLocalCopy($file, fn (string $local) => $this->storeLocal($item, $local));
} catch (ValidationException $e) {
throw $e;
} catch (Throwable $e) {
report($e);
throw ValidationException::withMessages(['image' => "„{$file->getClientOriginalName()}“ ließ sich nicht als Bild lesen."]);
}
}
/** Aus einer lokalen Datei (Import) */
public function storeLocal(Item $item, string $local): void
{
Images::raiseMemoryLimitFor($local);
$manager = ImageManager::usingDriver(Driver::class);
$image = $manager->decodePath($local)->scaleDown(2000, 2000);
$base = 'verleih/'.$item->id.'/'.Str::random(24);
$disk = Storage::disk(self::DISK);
$disk->put("{$base}.webp", $image->encode(new WebpEncoder(quality: 84))->toString());
$width = $image->width();
$height = $image->height();
$disk->put("{$base}-thumb.webp", $image->scaleDown(720, 720)->encode(new WebpEncoder(quality: 80))->toString());
$this->delete($item);
$item->forceFill(['image_path' => "{$base}.webp", 'thumb_path' => "{$base}-thumb.webp", 'image_width' => $width, 'image_height' => $height])->save();
}
public function delete(Item $item): void
{
if ($item->image_path) {
Storage::disk(self::DISK)->delete([$item->image_path, (string) $item->thumb_path]);
}
$item->forceFill(['image_path' => null, 'thumb_path' => null, 'image_width' => null, 'image_height' => null])->save();
}
public function deleteAll(Item $item): void
{
Storage::disk(self::DISK)->deleteDirectory('verleih/'.$item->id);
}
}