174 lines
5.6 KiB
PHP
174 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Media;
|
|
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Symfony\Component\Process\ExecutableFinder;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class ThumbnailDeriver
|
|
{
|
|
public function forElement(ContentElement $element): void
|
|
{
|
|
$version = $element->versions()->orderByDesc('number')->first();
|
|
$file = is_array($version?->metadata) ? ($version->metadata['file'] ?? null) : null;
|
|
|
|
if (! $version instanceof ContentElementVersion || ! is_string($file) || $file === '') {
|
|
$this->fail($element, 'Für dieses Medium liegt noch keine Datei vor.');
|
|
|
|
return;
|
|
}
|
|
|
|
if (in_array($element->output_class, ['film', 'audio'], true)) {
|
|
$this->frame($element, $file);
|
|
|
|
return;
|
|
}
|
|
|
|
$this->still($element, Storage::disk('media_derived'), $file, 'element-'.$element->id);
|
|
}
|
|
|
|
public function forCompositionUpload(Composition $composition, string $incoming): void
|
|
{
|
|
if (! str_starts_with($incoming, 'vorschau/') || str_contains($incoming, '..')) {
|
|
$this->failComposition($composition, 'Das Vorschaubild wurde nicht übernommen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->still($composition, Storage::disk('cms_incoming'), $incoming, 'komposition-'.$composition->id);
|
|
$this->rememberChoice($composition, 'eigen');
|
|
}
|
|
|
|
public function forCompositionFromElement(Composition $composition, ContentElement $element): void
|
|
{
|
|
$path = $element->thumbnail_path;
|
|
|
|
if (! is_string($path) || ! str_starts_with($path, 'vorschau/') || str_contains($path, '..')) {
|
|
$this->failComposition($composition, 'Für dieses Medium liegt noch kein Vorschaubild vor.');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->still($composition, Storage::disk('media_derived'), $path, 'komposition-'.$composition->id);
|
|
$this->rememberChoice($composition, 'element:'.$element->id);
|
|
}
|
|
|
|
private function frame(ContentElement $element, string $file): void
|
|
{
|
|
$binary = (new ExecutableFinder)->find('ffmpeg');
|
|
$disk = Storage::disk('media_derived');
|
|
|
|
if ($binary === null || ! $disk->exists($file)) {
|
|
$this->fail($element, 'Für dieses Medium liegt noch kein abgeleitetes Vorschaubild vor.');
|
|
|
|
return;
|
|
}
|
|
|
|
$target = 'vorschau/element-'.$element->id.'.jpg';
|
|
$disk->makeDirectory('vorschau');
|
|
$process = new Process([
|
|
$binary,
|
|
'-y',
|
|
'-ss',
|
|
'0',
|
|
'-i',
|
|
$disk->path($file),
|
|
'-frames:v',
|
|
'1',
|
|
$disk->path($target),
|
|
]);
|
|
$process->setTimeout(120);
|
|
$process->run();
|
|
|
|
if (! $process->isSuccessful() || ! $disk->exists($target)) {
|
|
$this->fail($element, 'Aus der Filmdatei ließ sich kein Vorschaubild erzeugen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$element->thumbnail_path = $target;
|
|
$element->thumbnail_alt = $element->working_title;
|
|
$element->thumbnail_error = null;
|
|
$element->save();
|
|
}
|
|
|
|
private function still(ContentElement|Composition $subject, Filesystem $disk, string $file, string $name): void
|
|
{
|
|
if (! $disk->exists($file)) {
|
|
$this->failSubject($subject, 'Die Bilddatei wurde nicht gefunden.');
|
|
|
|
return;
|
|
}
|
|
|
|
$image = @imagecreatefromstring($disk->get($file));
|
|
|
|
if ($image === false) {
|
|
$this->failSubject($subject, 'Die Bilddatei ließ sich nicht lesen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$width = imagesx($image);
|
|
$height = imagesy($image);
|
|
$targetWidth = 640;
|
|
$targetHeight = (int) max(1, round($height * ($targetWidth / max(1, $width))));
|
|
$canvas = imagecreatetruecolor($targetWidth, $targetHeight);
|
|
imagecopyresampled($canvas, $image, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
|
|
ob_start();
|
|
imagejpeg($canvas, null, 82);
|
|
$bytes = ob_get_clean();
|
|
imagedestroy($image);
|
|
imagedestroy($canvas);
|
|
|
|
if (! is_string($bytes) || $bytes === '') {
|
|
$this->failSubject($subject, 'Das Vorschaubild ließ sich nicht schreiben.');
|
|
|
|
return;
|
|
}
|
|
|
|
$path = 'vorschau/'.$name.'.jpg';
|
|
Storage::disk('media_derived')->put($path, $bytes);
|
|
$subject->thumbnail_path = $path;
|
|
|
|
if (! ($subject instanceof Composition && filled($subject->thumbnail_alt))) {
|
|
$subject->thumbnail_alt = $subject instanceof Composition ? $subject->designation : $subject->working_title;
|
|
}
|
|
|
|
$subject->thumbnail_error = null;
|
|
$subject->save();
|
|
}
|
|
|
|
private function rememberChoice(Composition $composition, string $source): void
|
|
{
|
|
$composition->refresh();
|
|
|
|
if ($composition->thumbnail_error !== null || ! filled($composition->thumbnail_path)) {
|
|
return;
|
|
}
|
|
|
|
$composition->thumbnail_source = $source;
|
|
$composition->save();
|
|
}
|
|
|
|
private function fail(ContentElement $element, string $reason): void
|
|
{
|
|
$this->failSubject($element, $reason);
|
|
}
|
|
|
|
private function failComposition(Composition $composition, string $reason): void
|
|
{
|
|
$this->failSubject($composition, $reason);
|
|
}
|
|
|
|
private function failSubject(ContentElement|Composition $subject, string $reason): void
|
|
{
|
|
$subject->thumbnail_error = $reason;
|
|
$subject->save();
|
|
}
|
|
}
|