Files
fewo-stommer/app/Console/Commands/ImportPhotos.php
T
Lars StommerandClaude Opus 5.5 bb7eb62782 Inhaltsverwaltung: Bilder, Webseiten-Texte, Ausstattung, FAQ, Hinweis-Banner
- Bilder mit automatischen WebP-Webgrößen (640/1280/2048 px), Sortierung per Drag & Drop, Mehrfach-Upload, Import-Befehl photos:import
- Webseiten-Texte als feste, bearbeitbare Bausteine (config/content.php), Rich-Text bereinigt ausgegeben
- Ausstattung und FAQ aus der bisherigen Webseite übernommen
- Rolle Inhalte hat Zugriff auf alle Webseiten-Bereiche

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:19:51 +02:00

49 lines
1.7 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Enums\PhotoCategory;
use App\Models\Photo;
use App\Models\Property;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Signature('photos:import {directory : Folder with JPG/PNG/WebP files} {--category=apartment : apartment, outdoor or area}')]
#[Description('Import all images of a folder as website photos (e.g. when migrating from the old website)')]
class ImportPhotos extends Command
{
/**
* Execute the console command.
*/
public function handle(): int
{
$category = PhotoCategory::tryFrom((string) $this->option('category'));
if ($category === null) {
$this->error('Unbekannte Kategorie. Erlaubt: '.implode(', ', array_column(PhotoCategory::cases(), 'value')));
return self::FAILURE;
}
$files = glob(rtrim((string) $this->argument('directory'), '/\\').'/*.{jpg,jpeg,png,webp,JPG,JPEG,PNG,WEBP}', GLOB_BRACE) ?: [];
$propertyId = $category === PhotoCategory::Area ? null : Property::query()->value('id');
$sort = (int) Photo::max('sort_order');
foreach ($files as $file) {
$photo = Photo::create([
'property_id' => $propertyId,
'category' => $category,
'alt_text' => str(pathinfo($file, PATHINFO_FILENAME))->replace(['_', '-'], ' ')->squish()->ucfirst()->toString(),
'sort_order' => ++$sort,
]);
$photo->addMedia($file)->preservingOriginal()->toMediaCollection(Photo::COLLECTION);
$this->line("✓ {$photo->alt_text}");
}
$this->info(count($files).' Bilder importiert.');
return self::SUCCESS;
}
}