Loheide-eu-mitarbeiterplatt.../vendor/intervention/gif/src/Builder.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

233 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Intervention\Gif;
use Intervention\Gif\Blocks\FrameBlock;
use Intervention\Gif\Blocks\GraphicControlExtension;
use Intervention\Gif\Blocks\ImageDescriptor;
use Intervention\Gif\Blocks\NetscapeApplicationExtension;
use Intervention\Gif\Blocks\TableBasedImage;
use Intervention\Gif\Exceptions\DecoderException;
use Intervention\Gif\Exceptions\EncoderException;
use Intervention\Gif\Exceptions\StreamException;
use Intervention\Gif\Exceptions\InvalidArgumentException;
use Intervention\Gif\Exceptions\StateException;
use Intervention\Gif\Traits\CanHandleFiles;
class Builder
{
use CanHandleFiles;
/**
* Create new instance.
*/
public function __construct(protected GifDataStream $gif = new GifDataStream())
{
//
}
/**
* Create new canvas.
*
* @throws InvalidArgumentException
*/
public static function canvas(int $width, int $height): self
{
return (new self())->setSize($width, $height);
}
/**
* Get GifDataStream object we're currently building.
*/
public function gifDataStream(): GifDataStream
{
return $this->gif;
}
/**
* Set canvas size of gif.
*
* @throws InvalidArgumentException
*/
public function setSize(int $width, int $height): self
{
$this->gif->logicalScreenDescriptor()->setSize($width, $height);
return $this;
}
/**
* Set loop count.
*
* @throws StateException
* @throws InvalidArgumentException
*/
public function setLoops(int $loops): self
{
if ($loops < 0) {
throw new InvalidArgumentException('The loop count must be equal to or greater than 0');
}
if ($this->gif->frames() === []) {
throw new StateException('Add at least one frame before setting the loop count');
}
// with one single loop the netscape extension must be removed otherwise the
// gif is looped twice because the first repetition always takes place
if ($loops === 1) {
$this->gif->firstFrame()?->clearApplicationExtensions();
return $this;
}
// make sure a netscape extension is present to store the loop count
if ($this->gif->firstFrame()?->netscapeExtension() === null) {
$this->gif->firstFrame()?->addApplicationExtension(
new NetscapeApplicationExtension()
);
}
// the loop count is reduced by one because what is referred to here as
// the “loop count” actually means repetitions in GIF format, and thus
// the first repetition always takes place. A loop count of 0 howerver
// means infinite repetitions and remains unaltered.
$loops = $loops === 0 ? $loops : $loops - 1;
// add loop count to netscape extension on first frame
$this->gif->firstFrame()?->netscapeExtension()?->setLoops($loops);
return $this;
}
/**
* Create new animation frame from given source which can be path to a file or GIF image data.
*
* @throws DecoderException
* @throws StreamException
* @throws InvalidArgumentException
*/
public function addFrame(
mixed $source,
float $delay = 0,
int $left = 0,
int $top = 0,
bool $interlaced = false
): self {
$frame = new FrameBlock();
$source = Decoder::decode($source);
// store delay
$frame->setGraphicControlExtension(
$this->buildGraphicControlExtension(
$source,
intval($delay * 100)
)
);
// store image
$frame->setTableBasedImage(
$this->buildTableBasedImage($source, $left, $top, $interlaced)
);
// add frame
$this->gif->addFrame($frame);
return $this;
}
/**
* Build new graphic control extension with given delay & disposal method
*/
protected function buildGraphicControlExtension(
GifDataStream $source,
int $delay,
DisposalMethod $disposalMethod = DisposalMethod::BACKGROUND
): GraphicControlExtension {
// create extension
$extension = new GraphicControlExtension($delay, $disposalMethod);
// set transparency index
$control = $source->firstFrame()?->graphicControlExtension();
if ($control !== null && $control->transparentColorExistance()) {
$extension->setTransparentColorExistance();
$extension->setTransparentColorIndex(
$control->transparentColorIndex()
);
}
return $extension;
}
/**
* Build table based image object from given source.
*
* @throws DecoderException
*/
protected function buildTableBasedImage(
GifDataStream $source,
int $left,
int $top,
bool $interlaced
): TableBasedImage {
$block = new TableBasedImage();
$block->setImageDescriptor(new ImageDescriptor());
// set global color table from source as local color table
$block->imageDescriptor()->setLocalColorTableExistance();
$globalColorTable = $source->globalColorTable();
if ($globalColorTable === null) {
throw new DecoderException(
'Failed to build table based image. Unable to find global color table in gif data stream',
);
}
$block->setColorTable($globalColorTable);
$block->imageDescriptor()->setLocalColorTableSorted(
$source->logicalScreenDescriptor()->globalColorTableSorted()
);
try {
$block->imageDescriptor()->setLocalColorTableSize(
$source->logicalScreenDescriptor()->globalColorTableSize()
);
$block->imageDescriptor()->setSize(
$source->logicalScreenDescriptor()->width(),
$source->logicalScreenDescriptor()->height()
);
} catch (InvalidArgumentException $e) {
throw new DecoderException(
'Failed to decode image source',
previous: $e
);
}
// set position
$block->imageDescriptor()->setPosition($left, $top);
// set interlaced flag
$block->imageDescriptor()->setInterlaced($interlaced);
// add image data from source
$block->setImageData(
$source->firstFrame()?->imageData() ?: throw new DecoderException(
'Failed to build table based image. Unable to find image data',
)
);
return $block;
}
/**
* Encode the current build.
*
* @throws EncoderException
*/
public function encode(): string
{
return $this->gif->encode();
}
}