Loheide-eu-mitarbeiterplatt.../vendor/intervention/image/src/Format.php
Friederich Loheide 9c0f8de791
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
Phase 0: Fundament für Inhaus
- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3)
- Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung
- Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü
- Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person
- Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“
- Benutzerverwaltung im Admin, Zugang vorläufig über is_admin
- vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/
- PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung
- VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 20:35:30 +00:00

188 lines
5.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Intervention\Image;
use Error;
use Intervention\Image\Encoders\AvifEncoder;
use Intervention\Image\Encoders\BmpEncoder;
use Intervention\Image\Encoders\GifEncoder;
use Intervention\Image\Encoders\HeicEncoder;
use Intervention\Image\Encoders\IcoEncoder;
use Intervention\Image\Encoders\Jpeg2000Encoder;
use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Encoders\JxlEncoder;
use Intervention\Image\Encoders\PngEncoder;
use Intervention\Image\Encoders\TiffEncoder;
use Intervention\Image\Encoders\WebpEncoder;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Interfaces\EncoderInterface;
use ReflectionClass;
use ReflectionParameter;
enum Format
{
case AVIF;
case BMP;
case GIF;
case HEIC;
case ICO;
case JP2;
case JPEG;
case JXL;
case PNG;
case TIFF;
case WEBP;
/**
* Create format from given identifier.
*
* @throws InvalidArgumentException
*/
public static function create(string|self|MediaType|FileExtension $identifier): self
{
if ($identifier instanceof self) {
return $identifier;
}
if ($identifier instanceof MediaType) {
return $identifier->format();
}
if ($identifier instanceof FileExtension) {
return $identifier->format();
}
try {
$format = MediaType::from(strtolower($identifier))->format();
} catch (Error) {
try {
$format = FileExtension::from(strtolower($identifier))->format();
} catch (Error) {
throw new InvalidArgumentException('Unable to create format from "' . $identifier . '"');
}
}
return $format;
}
/**
* Try to create format from given identifier and return null on failure.
*
* @param string|Format|MediaType|FileExtension $identifier
* @return Format|null
*/
public static function tryCreate(string|self|MediaType|FileExtension $identifier): ?self
{
try {
return self::create($identifier);
} catch (InvalidArgumentException) {
return null;
}
}
/**
* Return the possible media (MIME) types for the current format.
*
* @return array<MediaType>
*/
public function mediaTypes(): array
{
return array_filter(
MediaType::cases(),
fn(MediaType $mediaType): bool => $mediaType->format() === $this,
);
}
/**
* Return the first found media type for the current format.
*
* @throws NotSupportedException
*/
public function mediaType(): MediaType
{
$types = $this->mediaTypes();
$result = reset($types);
if (!$result instanceof MediaType) {
throw new NotSupportedException('Unable to retrieve unsupported media type from format');
}
return $result;
}
/**
* Return the possible file extension for the current format.
*
* @return array<FileExtension>
*/
public function fileExtensions(): array
{
return array_filter(
FileExtension::cases(),
fn(FileExtension $fileExtension): bool => $fileExtension->format() === $this,
);
}
/**
* Return the first found file extension for the current format.
*
* @throws NotSupportedException
*/
public function fileExtension(): FileExtension
{
$extensions = $this->fileExtensions();
$result = reset($extensions);
if (!$result instanceof FileExtension) {
throw new NotSupportedException('Unable to retrieve unsupported file extension for format');
}
return $result;
}
/**
* Create an encoder instance with given options that matches the format.
*/
public function encoder(mixed ...$options): EncoderInterface
{
// get classname of target encoder from current format
$classname = match ($this) {
self::AVIF => AvifEncoder::class,
self::BMP => BmpEncoder::class,
self::GIF => GifEncoder::class,
self::HEIC => HeicEncoder::class,
self::ICO => IcoEncoder::class,
self::JP2 => Jpeg2000Encoder::class,
self::JPEG => JpegEncoder::class,
self::JXL => JxlEncoder::class,
self::PNG => PngEncoder::class,
self::TIFF => TiffEncoder::class,
self::WEBP => WebpEncoder::class,
};
// get parameters of target encoder
$parameters = [];
$reflectionClass = new ReflectionClass($classname);
$constructor = $reflectionClass->getConstructor();
if ($constructor !== null) {
$parameters = array_map(
fn(ReflectionParameter $parameter): string => $parameter->getName(),
$constructor->getParameters(),
);
}
// filter out unavailable options of target encoder
$options = array_filter(
$options,
fn(mixed $key): bool => in_array($key, $parameters),
ARRAY_FILTER_USE_KEY,
);
return new $classname(...$options);
}
}