fjffk简单的咖啡店科开发2方法2 环滁皆山也。其西南诸峰,林壑尤美,望之蔚然而深秀者,琅琊也。山行六七里,渐闻水声潺潺,而泻出于两峰之间者,酿泉也 ??????????????????????????????? ??????????????/????????????????? ............/................... .........................../... ??????????????????????????????? >>>>>>>>>>>>>/>>>>>>>>>>>>>>>>> ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ     ÿÄ J    ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú "SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5 ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e ríV ?> ......................................... ............................................................................. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>/>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> dsdfgif/LICENSE000064400000002077152665633640006344 0ustar00The MIT License (MIT) Copyright (c) 2020-present Oliver Vogel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. gif/phpunit.xml.dist000064400000001215152665633640010503 0ustar00 ./tests/Unit/ src gif/README.md000064400000005632152665633640006616 0ustar00# Intervention GIF ## Native PHP GIF Encoder/Decoder [![Latest Version](https://img.shields.io/packagist/v/intervention/gif.svg)](https://packagist.org/packages/intervention/gif) ![build](https://github.com/Intervention/gif/actions/workflows/build.yml/badge.svg) [![Monthly Downloads](https://img.shields.io/packagist/dm/intervention/gif.svg)](https://packagist.org/packages/intervention/gif/stats) [![Support me on Ko-fi](https://raw.githubusercontent.com/Intervention/gif/main/.github/images/support.svg)](https://ko-fi.com/interventionphp) Intervention GIF is a PHP encoder and decoder for the GIF image format that does not depend on any image processing extension. Only the special `Splitter::class` class divides the data stream of an animated GIF into individual `GDImage` objects for each frame and is therefore dependent on the GD library. The library is the main component of [Intervention Image](https://github.com/Intervention/image) for processing animated GIF files with the GD library, but also works independently. ## Installation You can easily install this package using [Composer](https://getcomposer.org). Just request the package with the following command: ```bash composer require intervention/gif ``` ## Code Examples ### Decoding ```php use Intervention\Gif\Decoder; // Decode filepath to Intervention\Gif\GifDataStream::class $gif = Decoder::decode('images/animation.gif'); // Decoder can also handle binary content directly $gif = Decoder::decode($contents); ``` ### Encoding Use the Builder class to create a new GIF image. ```php use Intervention\Gif\Builder; // create new gif canvas $gif = Builder::canvas(width: 32, height: 32); // add animation frames to canvas $delay = .25; // delay in seconds after next frame is displayed $left = 0; // position offset (left) $top = 0; // position offset (top) // add animation frames with optional delay in seconds // and optional position offset for each frame $gif->addFrame('images/frame01.gif', $delay, $left, $top); $gif->addFrame('images/frame02.gif', $delay, $left); $gif->addFrame('images/frame03.gif', $delay); $gif->addFrame('images/frame04.gif'); // set loop count; 0 for infinite looping $gif->setLoops(12); // encode $data = $gif->encode(); ``` ## Requirements - PHP >= 8.1 ## Development & Testing With this package comes a Docker image to build a test suite and analysis container. To build this container you have to have Docker installed on your system. You can run all tests with this command. ```bash docker-compose run --rm --build tests ``` Run the static analyzer on the code base. ```bash docker-compose run --rm --build analysis ``` ## Authors This library is developed and maintained by [Oliver Vogel](https://intervention.io) Thanks to the community of [contributors](https://github.com/Intervention/gif/graphs/contributors) who have helped to improve this project. ## License Intervention GIF is licensed under the [MIT License](LICENSE). gif/src/Exceptions/EncoderException.php000064400000000176152665633640014214 0ustar00 $frames * @param array $comments * @return void */ public function __construct( protected Header $header = new Header(), protected LogicalScreenDescriptor $logicalScreenDescriptor = new LogicalScreenDescriptor(), protected ?ColorTable $globalColorTable = null, protected array $frames = [], protected array $comments = [] ) { } /** * Get header * * @return Header */ public function getHeader(): Header { return $this->header; } /** * Set header * * @param Header $header */ public function setHeader(Header $header): self { $this->header = $header; return $this; } /** * Get logical screen descriptor * * @return LogicalScreenDescriptor */ public function getLogicalScreenDescriptor(): LogicalScreenDescriptor { return $this->logicalScreenDescriptor; } /** * Set logical screen descriptor * * @param LogicalScreenDescriptor $descriptor * @return GifDataStream */ public function setLogicalScreenDescriptor(LogicalScreenDescriptor $descriptor): self { $this->logicalScreenDescriptor = $descriptor; return $this; } /** * Return global color table if available else null * * @return null|ColorTable */ public function getGlobalColorTable(): ?ColorTable { return $this->globalColorTable; } /** * Set global color table * * @param ColorTable $table * @return GifDataStream */ public function setGlobalColorTable(ColorTable $table): self { $this->globalColorTable = $table; $this->logicalScreenDescriptor->setGlobalColorTableExistance(true); $this->logicalScreenDescriptor->setGlobalColorTableSize( $table->getLogicalSize() ); return $this; } /** * Get main graphic control extension * * @return NetscapeApplicationExtension */ public function getMainApplicationExtension(): ?NetscapeApplicationExtension { foreach ($this->frames as $frame) { if ($extension = $frame->getNetscapeExtension()) { return $extension; } } return null; } /** * Get array of frames * * @return array */ public function getFrames(): array { return $this->frames; } /** * Return array of "global" comments * * @return array */ public function getComments(): array { return $this->comments; } /** * Return first frame * * @return null|FrameBlock */ public function getFirstFrame(): ?FrameBlock { if (!array_key_exists(0, $this->frames)) { return null; } return $this->frames[0]; } /** * Add frame * * @param FrameBlock $frame * @return GifDataStream */ public function addFrame(FrameBlock $frame): self { $this->frames[] = $frame; return $this; } /** * Add comment extension * * @param CommentExtension $comment * @return GifDataStream */ public function addComment(CommentExtension $comment): self { $this->comments[] = $comment; return $this; } /** * Set the current data * * @param array $frames */ public function setFrames(array $frames): self { $this->frames = $frames; return $this; } /** * Get trailer * * @return Trailer */ public function getTrailer(): Trailer { return new Trailer(); } /** * Determine if gif is animated * * @return bool */ public function isAnimated(): bool { return count($this->getFrames()) > 1; } /** * Determine if global color table is set * * @return bool */ public function hasGlobalColorTable(): bool { return !is_null($this->globalColorTable); } } gif/src/Decoders/LogicalScreenDescriptorDecoder.php000064400000006360152665633640016425 0ustar00setSize( $this->decodeWidth($this->getNextBytesOrFail(2)), $this->decodeHeight($this->getNextBytesOrFail(2)) ); // byte 5 $packedField = $this->getNextByteOrFail(); $logicalScreenDescriptor->setGlobalColorTableExistance( $this->decodeGlobalColorTableExistance($packedField) ); $logicalScreenDescriptor->setBitsPerPixel( $this->decodeBitsPerPixel($packedField) ); $logicalScreenDescriptor->setGlobalColorTableSorted( $this->decodeGlobalColorTableSorted($packedField) ); $logicalScreenDescriptor->setGlobalColorTableSize( $this->decodeGlobalColorTableSize($packedField) ); // byte 6 $logicalScreenDescriptor->setBackgroundColorIndex( $this->decodeBackgroundColorIndex($this->getNextByteOrFail()) ); // byte 7 $logicalScreenDescriptor->setPixelAspectRatio( $this->decodePixelAspectRatio($this->getNextByteOrFail()) ); return $logicalScreenDescriptor; } /** * Decode width * * @return int */ protected function decodeWidth(string $source): int { return unpack('v*', $source)[1]; } /** * Decode height * * @return int */ protected function decodeHeight(string $source): int { return unpack('v*', $source)[1]; } /** * Decode existance of global color table * * @return bool */ protected function decodeGlobalColorTableExistance(string $byte): bool { return $this->hasPackedBit($byte, 0); } /** * Decode color resolution in bits per pixel * * @return int */ protected function decodeBitsPerPixel(string $byte): int { return bindec($this->getPackedBits($byte, 1, 3)) + 1; } /** * Decode global color table sorted status * * @return bool */ protected function decodeGlobalColorTableSorted(string $byte): bool { return $this->hasPackedBit($byte, 4); } /** * Decode size of global color table * * @return int */ protected function decodeGlobalColorTableSize(string $byte): int { return bindec($this->getPackedBits($byte, 5, 3)); } /** * Decode background color index * * @return int */ protected function decodeBackgroundColorIndex(string $source): int { return unpack('C', $source)[1]; } /** * Decode pixel aspect ratio * * @return int */ protected function decodePixelAspectRatio(string $source): int { return unpack('C', $source)[1]; } } gif/src/Decoders/GraphicControlExtensionDecoder.php000064400000004464152665633640016472 0ustar00getNextBytesOrFail(3); // skip marker, label & bytesize // byte #4 $packedField = $this->getNextByteOrFail(); $result->setDisposalMethod($this->decodeDisposalMethod($packedField)); $result->setUserInput($this->decodeUserInput($packedField)); $result->setTransparentColorExistance($this->decodeTransparentColorExistance($packedField)); // bytes 5-6 $result->setDelay($this->decodeDelay($this->getNextBytesOrFail(2))); // byte #7 $result->setTransparentColorIndex($this->decodeTransparentColorIndex( $this->getNextByteOrFail() )); // byte #8 (terminator) $this->getNextByteOrFail(); return $result; } /** * Decode disposal method * * @return DisposalMethod */ protected function decodeDisposalMethod(string $byte): DisposalMethod { return DisposalMethod::from( bindec($this->getPackedBits($byte, 3, 3)) ); } /** * Decode user input flag * * @return bool */ protected function decodeUserInput(string $byte): bool { return $this->hasPackedBit($byte, 6); } /** * Decode transparent color existance * * @return bool */ protected function decodeTransparentColorExistance(string $byte): bool { return $this->hasPackedBit($byte, 7); } /** * Decode delay value * * @return int */ protected function decodeDelay(string $bytes): int { return unpack('v*', $bytes)[1]; } /** * Decode transparent color index * * @return int */ protected function decodeTransparentColorIndex(string $byte): int { return unpack('C', $byte)[1]; } } gif/src/Decoders/ColorDecoder.php000064400000001513152665633640012725 0ustar00setRed($this->decodeColorValue($this->getNextByteOrFail())); $color->setGreen($this->decodeColorValue($this->getNextByteOrFail())); $color->setBlue($this->decodeColorValue($this->getNextByteOrFail())); return $color; } /** * Decode red value from source * * @return int */ protected function decodeColorValue(string $byte): int { return unpack('C', $byte)[1]; } } gif/src/Decoders/ColorTableDecoder.php000064400000001145152665633640013676 0ustar00getLength() / 3); $i++) { $table->addColor(Color::decode($this->handle)); } return $table; } } gif/src/Decoders/NetscapeApplicationExtensionDecoder.php000064400000000231152665633640017466 0ustar00setVersion($this->decodeVersion()); return $header; } /** * Decode version string * * @throws DecoderException * @return string */ protected function decodeVersion(): string { $parsed = (bool) preg_match("/^GIF(?P[0-9]{2}[a-z])$/", $this->getNextBytesOrFail(6), $matches); if ($parsed === false) { throw new DecoderException('Unable to parse file header.'); } return $matches['version']; } } gif/src/Decoders/PlainTextExtensionDecoder.php000064400000002663152665633640015463 0ustar00getNextBytesOrFail(2); // skip info block $this->getNextBytesOrFail($this->getInfoBlockSize()); // text blocks $extension->setText($this->decodeTextBlocks()); return $extension; } /** * Get number of bytes in header block * * @throws DecoderException * @return int */ protected function getInfoBlockSize(): int { return unpack('C', $this->getNextByteOrFail())[1]; } /** * Decode text sub blocks * * @throws DecoderException * @return array */ protected function decodeTextBlocks(): array { $blocks = []; do { $char = $this->getNextByteOrFail(); $size = (int) unpack('C', $char)[1]; if ($size > 0) { $blocks[] = $this->getNextBytesOrFail($size); } } while ($char !== PlainTextExtension::TERMINATOR); return $blocks; } } gif/src/Decoders/ImageDescriptorDecoder.php000064400000004324152665633640014733 0ustar00getNextByteOrFail(); // skip separator $descriptor->setPosition( $this->decodeMultiByte($this->getNextBytesOrFail(2)), $this->decodeMultiByte($this->getNextBytesOrFail(2)) ); $descriptor->setSize( $this->decodeMultiByte($this->getNextBytesOrFail(2)), $this->decodeMultiByte($this->getNextBytesOrFail(2)) ); $packedField = $this->getNextByteOrFail(); $descriptor->setLocalColorTableExistance( $this->decodeLocalColorTableExistance($packedField) ); $descriptor->setLocalColorTableSorted( $this->decodeLocalColorTableSorted($packedField) ); $descriptor->setLocalColorTableSize( $this->decodeLocalColorTableSize($packedField) ); $descriptor->setInterlaced( $this->decodeInterlaced($packedField) ); return $descriptor; } /** * Decode local color table existance * * @return bool */ protected function decodeLocalColorTableExistance(string $byte): bool { return $this->hasPackedBit($byte, 0); } /** * Decode local color table sort method * * @return bool */ protected function decodeLocalColorTableSorted(string $byte): bool { return $this->hasPackedBit($byte, 2); } /** * Decode local color table size * * @return int */ protected function decodeLocalColorTableSize(string $byte): int { return bindec($this->getPackedBits($byte, 5, 3)); } /** * Decode interlaced flag * * @return bool */ protected function decodeInterlaced(string $byte): bool { return $this->hasPackedBit($byte, 1); } } gif/src/Decoders/CommentExtensionDecoder.php000064400000002561152665633640015152 0ustar00getNextBytesOrFail(2); // skip marker & label $extension = new CommentExtension(); foreach ($this->decodeComments() as $comment) { $extension->addComment($comment); } return $extension; } /** * Decode comment from current source * * @throws DecoderException * @return array */ protected function decodeComments(): array { $comments = []; do { $byte = $this->getNextByteOrFail(); $size = $this->decodeBlocksize($byte); if ($size > 0) { $comments[] = $this->getNextBytesOrFail($size); } } while ($byte !== CommentExtension::TERMINATOR); return $comments; } /** * Decode blocksize of following comment * * @param string $byte * @return int */ protected function decodeBlocksize(string $byte): int { return (int) @unpack('C', $byte)[1]; } } gif/src/Decoders/AbstractDecoder.php000064400000006250152665633640013415 0ustar00handle = $handle; return $this; } /** * Read given number of bytes and move file pointer * * @param int $length * @throws DecoderException * @return string */ protected function getNextBytesOrFail(int $length): string { $bytes = fread($this->handle, $length); if (strlen($bytes) !== $length) { throw new DecoderException('Unexpected end of file.'); } return $bytes; } /** * Read given number of bytes and move pointer back to previous position * * @param int $length * @throws DecoderException * @return string */ protected function viewNextBytesOrFail(int $length): string { $bytes = $this->getNextBytesOrFail($length); $this->movePointer($length * -1); return $bytes; } /** * Read next byte and move pointer back to previous position * * @throws DecoderException * @return string */ protected function viewNextByteOrFail(): string { return $this->viewNextBytesOrFail(1); } /** * Read all remaining bytes from file handler * * @return string */ protected function getRemainingBytes(): string { $all = ''; do { $byte = fread($this->handle, 1); $all .= $byte; } while (!feof($this->handle)); return $all; } /** * Get next byte in stream and move file pointer * * @throws DecoderException * @return string */ protected function getNextByteOrFail(): string { return $this->getNextBytesOrFail(1); } /** * Move file pointer on handle by given offset * * @param int $offset * @return self */ protected function movePointer(int $offset): self { fseek($this->handle, $offset, SEEK_CUR); return $this; } /** * Decode multi byte value * * @return int */ protected function decodeMultiByte(string $bytes): int { return unpack('v*', $bytes)[1]; } /** * Set length * * @param int $length */ public function setLength(int $length): self { $this->length = $length; return $this; } /** * Get length * * @return null|int */ public function getLength(): ?int { return $this->length; } /** * Get current handle position * * @return int */ public function getPosition(): int { return ftell($this->handle); } } gif/src/Decoders/DataSubBlockDecoder.php000064400000001155152665633640014147 0ustar00getNextByteOrFail(); $size = (int) unpack('C', $char)[1]; return new DataSubBlock($this->getNextBytesOrFail($size)); } } gif/src/Decoders/AbstractPackedBitDecoder.php000064400000001667152665633640015173 0ustar00getPackedBits($byte)[$num]; } /** * Get packed bits * * @param int $start * @param int $length * @return string */ protected function getPackedBits(string $byte, int $start = 0, int $length = 8): string { $bits = str_pad(decbin($this->decodePackedByte($byte)), 8, '0', STR_PAD_LEFT); return substr($bits, $start, $length); } } gif/src/Decoders/GifDataStreamDecoder.php000064400000003340152665633640014322 0ustar00setHeader( Header::decode($this->handle), ); $gif->setLogicalScreenDescriptor( LogicalScreenDescriptor::decode($this->handle), ); if ($gif->getLogicalScreenDescriptor()->hasGlobalColorTable()) { $length = $gif->getLogicalScreenDescriptor()->getGlobalColorTableByteSize(); $gif->setGlobalColorTable( ColorTable::decode($this->handle, $length) ); } while ($this->viewNextByteOrFail() !== Trailer::MARKER) { match ($this->viewNextBytesOrFail(2)) { // trailing "global" comment blocks which are not part of "FrameBlock" AbstractExtension::MARKER . CommentExtension::LABEL => $gif->addComment( CommentExtension::decode($this->handle) ), default => $gif->addFrame( FrameBlock::decode($this->handle) ), }; } return $gif; } } gif/src/Decoders/ApplicationExtensionDecoder.php000064400000003735152665633640016017 0ustar00getNextByteOrFail(); // marker $this->getNextByteOrFail(); // label $blocksize = $this->decodeBlockSize($this->getNextByteOrFail()); $application = $this->getNextBytesOrFail($blocksize); if ($application === NetscapeApplicationExtension::IDENTIFIER . NetscapeApplicationExtension::AUTH_CODE) { $result = new NetscapeApplicationExtension(); // skip length $this->getNextByteOrFail(); $result->setBlocks([ new DataSubBlock( $this->getNextBytesOrFail(3) ) ]); // skip terminator $this->getNextByteOrFail(); return $result; } $result->setApplication($application); // decode data sub blocks $blocksize = $this->decodeBlockSize($this->getNextByteOrFail()); while ($blocksize > 0) { $result->addBlock(new DataSubBlock($this->getNextBytesOrFail($blocksize))); $blocksize = $this->decodeBlockSize($this->getNextByteOrFail()); } return $result; } /** * Decode block size of ApplicationExtension from given byte * * @param string $byte * @return int */ protected function decodeBlockSize(string $byte): int { return (int) @unpack('C', $byte)[1]; } } gif/src/Decoders/TableBasedImageDecoder.php000064400000002025152665633640014577 0ustar00setImageDescriptor(ImageDescriptor::decode($this->handle)); if ($block->getImageDescriptor()->hasLocalColorTable()) { $block->setColorTable( ColorTable::decode( $this->handle, $block->getImageDescriptor()->getLocalColorTableByteSize() ) ); } $block->setImageData( ImageData::decode($this->handle) ); return $block; } } gif/src/Decoders/ImageDataDecoder.php000064400000002065152665633640013466 0ustar00getNextByteOrFail(); $size = (int) unpack('C', $char)[1]; $data->setLzwMinCodeSize($size); do { // decode sub blocks $char = $this->getNextByteOrFail(); $size = (int) unpack('C', $char)[1]; if ($size > 0) { $data->addBlock(new DataSubBlock($this->getNextBytesOrFail($size))); } } while ($char !== AbstractEntity::TERMINATOR); return $data; } } gif/src/Decoders/FrameBlockDecoder.php000064400000003644152665633640013663 0ustar00viewNextBytesOrFail(2)) { AbstractExtension::MARKER . GraphicControlExtension::LABEL => GraphicControlExtension::decode($this->handle), AbstractExtension::MARKER . NetscapeApplicationExtension::LABEL => NetscapeApplicationExtension::decode($this->handle), AbstractExtension::MARKER . ApplicationExtension::LABEL => ApplicationExtension::decode($this->handle), AbstractExtension::MARKER . PlainTextExtension::LABEL => PlainTextExtension::decode($this->handle), AbstractExtension::MARKER . CommentExtension::LABEL => CommentExtension::decode($this->handle), default => match ($this->viewNextByteOrFail()) { ImageDescriptor::SEPARATOR => TableBasedImage::decode($this->handle), default => throw new DecoderException('Unable to decode Data Block'), } }; $frame->addEntity($block); } while (!($block instanceof TableBasedImage)); return $frame; } } gif/src/Traits/CanEncode.php000064400000002052152665633640011715 0ustar00getEncoder()->encode(); } /** * Get encoder object for current entity * * @throws EncoderException * @return AbstractEncoder */ protected function getEncoder(): AbstractEncoder { $classname = $this->getEncoderClassname(); if (!class_exists($classname)) { throw new EncoderException("Encoder for '" . $this::class . "' not found."); } return new $classname($this); } /** * Get encoder classname for current entity * * @return string */ protected function getEncoderClassname(): string { return sprintf('Intervention\Gif\Encoders\%sEncoder', $this->getShortClassname()); } } gif/src/Traits/CanHandleFiles.php000064400000002212152665633640012674 0ustar00decode(); } /** * Get decoder for current instance * * @param resource $source * @param null|int $length * @throws DecoderException * @return AbstractDecoder */ protected static function getDecoder($source, ?int $length = null): AbstractDecoder { $classname = self::getDecoderClassname(); if (!class_exists($classname)) { throw new DecoderException("Decoder for '" . static::class . "' not found."); } return new $classname($source, $length); } /** * Get classname of decoder for current classname * * @return string */ protected static function getDecoderClassname(): string { return sprintf('Intervention\Gif\Decoders\%sDecoder', self::getShortClassname()); } } gif/src/AbstractExtension.php000064400000000234152665633640012270 0ustar00getShortName(); } /** * Cast object to string * * @throws EncoderException * @return string */ public function __toString(): string { return $this->encode(); } } gif/src/Splitter.php000064400000017636152665633640010454 0ustar00 */ class Splitter implements IteratorAggregate { /** * Single frames resolved to GifDataStream * * @var array */ protected array $frames = []; /** * Delays of each frame * * @var array */ protected array $delays = []; /** * Create new instance * * @param GifDataStream $stream */ public function __construct(protected GifDataStream $stream) { // } /** * Iterator * * @return Traversable */ public function getIterator(): Traversable { return new ArrayIterator($this->frames); } /** * Get frames * * @return array */ public function getFrames(): array { return $this->frames; } /** * Get delays * * @return array */ public function getDelays(): array { return $this->delays; } /** * Set stream of instance * * @param GifDataStream $stream */ public function setStream(GifDataStream $stream): self { $this->stream = $stream; return $this; } /** * Static constructor method * * @param GifDataStream $stream * @return Splitter */ public static function create(GifDataStream $stream): self { return new self($stream); } /** * Split current stream into array of seperate streams for each frame * * @return Splitter */ public function split(): self { $this->frames = []; foreach ($this->stream->getFrames() as $frame) { // create separate stream for each frame $gif = Builder::canvas( $this->stream->getLogicalScreenDescriptor()->getWidth(), $this->stream->getLogicalScreenDescriptor()->getHeight() )->getGifDataStream(); // check if working stream has global color table if ($this->stream->hasGlobalColorTable()) { $gif->setGlobalColorTable($this->stream->getGlobalColorTable()); $gif->getLogicalScreenDescriptor()->setGlobalColorTableExistance(true); $gif->getLogicalScreenDescriptor()->setGlobalColorTableSorted( $this->stream->getLogicalScreenDescriptor()->getGlobalColorTableSorted() ); $gif->getLogicalScreenDescriptor()->setGlobalColorTableSize( $this->stream->getLogicalScreenDescriptor()->getGlobalColorTableSize() ); $gif->getLogicalScreenDescriptor()->setBackgroundColorIndex( $this->stream->getLogicalScreenDescriptor()->getBackgroundColorIndex() ); $gif->getLogicalScreenDescriptor()->setPixelAspectRatio( $this->stream->getLogicalScreenDescriptor()->getPixelAspectRatio() ); $gif->getLogicalScreenDescriptor()->setBitsPerPixel( $this->stream->getLogicalScreenDescriptor()->getBitsPerPixel() ); } // copy original frame $gif->addFrame($frame); $this->frames[] = $gif; $this->delays[] = match (is_object($frame->getGraphicControlExtension())) { true => $frame->getGraphicControlExtension()->getDelay(), default => 0, }; } return $this; } /** * Return array of GD library resources for each frame * * @throws EncoderException * @return array */ public function toResources(): array { $resources = []; foreach ($this->frames as $frame) { $resource = imagecreatefromstring($frame->encode()); imagepalettetotruecolor($resource); imagesavealpha($resource, true); $resources[] = $resource; } return $resources; } /** * Return array of coalesced GD library resources for each frame * * @throws EncoderException * @return array */ public function coalesceToResources(): array { $resources = $this->toResources(); // static gif files don't need to be coalesced if (count($resources) === 1) { return $resources; } $width = imagesx($resources[0]); $height = imagesy($resources[0]); $transparent = imagecolortransparent($resources[0]); foreach ($resources as $key => $resource) { // get meta data $gif = $this->frames[$key]; $descriptor = $gif->getFirstFrame()->getImageDescriptor(); $offset_x = $descriptor->getLeft(); $offset_y = $descriptor->getTop(); $w = $descriptor->getWidth(); $h = $descriptor->getHeight(); if (in_array($this->getDisposalMethod($gif), [DisposalMethod::NONE, DisposalMethod::PREVIOUS])) { if ($key >= 1) { // create normalized gd image $canvas = imagecreatetruecolor($width, $height); if (imagecolortransparent($resource) != -1) { $transparent = imagecolortransparent($resource); } else { $transparent = imagecolorallocatealpha($resource, 255, 0, 255, 127); } // fill with transparent imagefill($canvas, 0, 0, $transparent); imagecolortransparent($canvas, $transparent); imagealphablending($canvas, true); // insert last as base imagecopy( $canvas, $resources[$key - 1], 0, 0, 0, 0, $width, $height ); // insert resource imagecopy( $canvas, $resource, $offset_x, $offset_y, 0, 0, $w, $h ); } else { imagealphablending($resource, true); $canvas = $resource; } } else { // create normalized gd image $canvas = imagecreatetruecolor($width, $height); if (imagecolortransparent($resource) != -1) { $transparent = imagecolortransparent($resource); } else { $transparent = imagecolorallocatealpha($resource, 255, 0, 255, 127); } // fill with transparent imagefill($canvas, 0, 0, $transparent); imagecolortransparent($canvas, $transparent); imagealphablending($canvas, true); // insert frame resource imagecopy( $canvas, $resource, $offset_x, $offset_y, 0, 0, $w, $h ); } $resources[$key] = $canvas; } return $resources; } /** * Find and return disposal method of given gif data stream * * @param GifDataStream $gif * @return DisposalMethod */ private function getDisposalMethod(GifDataStream $gif): DisposalMethod { return $gif->getFirstFrame()->getGraphicControlExtension()->getDisposalMethod(); } } gif/src/Blocks/Header.php000064400000001225152665633640011236 0ustar00version = $value; return $this; } /** * Return current version * * @return string */ public function getVersion(): string { return $this->version; } } gif/src/Blocks/ImageData.php000064400000002514152665633640011664 0ustar00 */ protected array $blocks = []; /** * Get LZW min. code size * * @return int */ public function getLzwMinCodeSize(): int { return $this->lzw_min_code_size; } /** * Set lzw min. code size * * @param int $size * @return ImageData */ public function setLzwMinCodeSize(int $size): self { $this->lzw_min_code_size = $size; return $this; } /** * Get current data sub blocks * * @return array */ public function getBlocks(): array { return $this->blocks; } /** * Addd sub block * * @param DataSubBlock $block * @return ImageData */ public function addBlock(DataSubBlock $block): self { $this->blocks[] = $block; return $this; } /** * Determine if data sub blocks are present * * @return bool */ public function hasBlocks(): bool { return count($this->blocks) >= 1; } } gif/src/Blocks/DataSubBlock.php000064400000001603152665633640012344 0ustar00getSize() > 255) { throw new FormatException( 'Data Sub-Block can not have a block size larger than 255 bytes.' ); } } /** * Return size of current block * * @return int */ public function getSize(): int { return strlen($this->value); } /** * Return block value * * @return string */ public function getValue(): string { return $this->value; } } gif/src/Blocks/Color.php000064400000002775152665633640011137 0ustar00r; } /** * Set red value * * @param int $value */ public function setRed(int $value): self { $this->r = $value; return $this; } /** * Get green value * * @return int */ public function getGreen(): int { return $this->g; } /** * Set green value * * @param int $value */ public function setGreen(int $value): self { $this->g = $value; return $this; } /** * Get blue value * * @return int */ public function getBlue(): int { return $this->b; } /** * Set blue value * * @param int $value */ public function setBlue(int $value): self { $this->b = $value; return $this; } /** * Return hash value of current color * * @return string */ public function getHash(): string { return md5($this->r . $this->g . $this->b); } } gif/src/Blocks/LogicalScreenDescriptor.php000064400000010734152665633640014624 0ustar00width = $width; $this->height = $height; return $this; } /** * Get width of current instance * * @return int */ public function getWidth(): int { return intval($this->width); } /** * Get height of current instance * * @return int */ public function getHeight(): int { return intval($this->height); } /** * Determine if global color table is present * * @return bool */ public function getGlobalColorTableExistance(): bool { return $this->globalColorTableExistance; } /** * Alias of getGlobalColorTableExistance * * @return bool */ public function hasGlobalColorTable(): bool { return $this->getGlobalColorTableExistance(); } /** * Set global color table flag * * @param bool $existance * @return self */ public function setGlobalColorTableExistance(bool $existance = true): self { $this->globalColorTableExistance = $existance; return $this; } /** * Get global color table sorted flag * * @return bool */ public function getGlobalColorTableSorted(): bool { return $this->globalColorTableSorted; } /** * Set global color table sorted flag * * @param bool $sorted * @return self */ public function setGlobalColorTableSorted(bool $sorted = true): self { $this->globalColorTableSorted = $sorted; return $this; } /** * Get size of global color table * * @return int */ public function getGlobalColorTableSize(): int { return $this->globalColorTableSize; } /** * Get byte size of global color table * * @return int */ public function getGlobalColorTableByteSize(): int { return 3 * pow(2, $this->getGlobalColorTableSize() + 1); } /** * Set size of global color table * * @param int $size */ public function setGlobalColorTableSize(int $size): self { $this->globalColorTableSize = $size; return $this; } /** * Get background color index * * @return int */ public function getBackgroundColorIndex(): int { return $this->backgroundColorIndex; } /** * Set background color index * * @param int $index */ public function setBackgroundColorIndex(int $index): self { $this->backgroundColorIndex = $index; return $this; } /** * Get current pixel aspect ration * * @return int */ public function getPixelAspectRatio(): int { return $this->pixelAspectRatio; } /** * Set pixel aspect ratio * * @param int $ratio */ public function setPixelAspectRatio(int $ratio): self { $this->pixelAspectRatio = $ratio; return $this; } /** * Get color resolution * * @return int */ public function getBitsPerPixel(): int { return $this->bitsPerPixel; } /** * Set color resolution * * @param int $value */ public function setBitsPerPixel(int $value): self { $this->bitsPerPixel = $value; return $this; } } gif/src/Blocks/GraphicControlExtension.php000064400000005660152665633640014670 0ustar00delay = $value; return $this; } /** * Return delay time (1/100 second) * * @return int */ public function getDelay(): int { return $this->delay; } /** * Set disposal method * * @param DisposalMethod $method * @return self */ public function setDisposalMethod(DisposalMethod $method): self { $this->disposalMethod = $method; return $this; } /** * Get disposal method * * @return DisposalMethod */ public function getDisposalMethod(): DisposalMethod { return $this->disposalMethod; } /** * Get transparent color index * * @return int */ public function getTransparentColorIndex(): int { return $this->transparentColorIndex; } /** * Set transparent color index * * @param int $index */ public function setTransparentColorIndex(int $index): self { $this->transparentColorIndex = $index; return $this; } /** * Get current transparent color existance * * @return bool */ public function getTransparentColorExistance(): bool { return $this->transparentColorExistance; } /** * Set existance flag of transparent color * * @param bool $existance */ public function setTransparentColorExistance(bool $existance = true): self { $this->transparentColorExistance = $existance; return $this; } /** * Get user input flag * * @return bool */ public function getUserInput(): bool { return $this->userInput; } /** * Set user input flag * * @param bool $value */ public function setUserInput(bool $value = true): self { $this->userInput = $value; return $this; } } gif/src/Blocks/PlainTextExtension.php000064400000001757152665633640013665 0ustar00 */ protected array $text = []; /** * Get current text * * @return array */ public function getText(): array { return $this->text; } /** * Add text * * @param string $text */ public function addText(string $text): self { $this->text[] = $text; return $this; } /** * Set text array of extension * * @param array $text */ public function setText(array $text): self { $this->text = $text; return $this; } /** * Determine if any text is present * * @return bool */ public function hasText(): bool { return $this->text !== []; } } gif/src/Blocks/CommentExtension.php000064400000001624152665633640013350 0ustar00 */ protected array $comments = []; /** * Get all or one comment * * @return array */ public function getComments(): array { return $this->comments; } /** * Get one comment by key * * @param int $key * @return mixed */ public function getComment(int $key): mixed { return array_key_exists($key, $this->comments) ? $this->comments[$key] : null; } /** * Set comment text * * @param string $value */ public function addComment(string $value): self { $this->comments[] = $value; return $this; } } gif/src/Blocks/ImageDescriptor.php000064400000010414152665633640013127 0ustar00width); } /** * Get current width * * @return int */ public function getHeight(): int { return intval($this->height); } /** * Get current Top * * @return int */ public function getTop(): int { return intval($this->top); } /** * Get current Left * * @return int */ public function getLeft(): int { return intval($this->left); } /** * Set size of current instance * * @param int $width * @param int $height */ public function setSize(int $width, int $height): self { $this->width = $width; $this->height = $height; return $this; } /** * Set position of current instance * * @param int $left * @param int $top */ public function setPosition(int $left, int $top): self { $this->left = $left; $this->top = $top; return $this; } /** * Determine if frame is interlaced * * @return bool */ public function isInterlaced(): bool { return $this->interlaced; } /** * Set or unset interlaced value * * @param bool $value */ public function setInterlaced(bool $value = true): self { $this->interlaced = $value; return $this; } /** * Determine if local color table is present * * @return bool */ public function getLocalColorTableExistance(): bool { return $this->localColorTableExistance; } /** * Alias for getLocalColorTableExistance * * @return bool */ public function hasLocalColorTable(): bool { return $this->getLocalColorTableExistance(); } /** * Set local color table flag * * @param bool $existance * @return self */ public function setLocalColorTableExistance(bool $existance = true): self { $this->localColorTableExistance = $existance; return $this; } /** * Get local color table sorted flag * * @return bool */ public function getLocalColorTableSorted(): bool { return $this->localColorTableSorted; } /** * Set local color table sorted flag * * @param bool $sorted * @return self */ public function setLocalColorTableSorted(bool $sorted = true): self { $this->localColorTableSorted = $sorted; return $this; } /** * Get size of local color table * * @return int */ public function getLocalColorTableSize(): int { return $this->localColorTableSize; } /** * Get byte size of global color table * * @return int */ public function getLocalColorTableByteSize(): int { return 3 * pow(2, $this->getLocalColorTableSize() + 1); } /** * Set size of local color table * * @param int $size */ public function setLocalColorTableSize(int $size): self { $this->localColorTableSize = $size; return $this; } } gif/src/Blocks/ApplicationExtension.php000064400000003263152665633640014212 0ustar00 */ protected array $blocks = []; /** * Get size of block * * @return int */ public function getBlockSize(): int { return strlen($this->application); } /** * Set application name * * @param string $value * @return ApplicationExtension */ public function setApplication(string $value): self { $this->application = $value; return $this; } /** * Get application name * * @return string */ public function getApplication(): string { return $this->application; } /** * Add block to application extension * * @param DataSubBlock $block * @return ApplicationExtension */ public function addBlock(DataSubBlock $block): self { $this->blocks[] = $block; return $this; } /** * Set data sub blocks of instance * * @param array $blocks * @return ApplicationExtension */ public function setBlocks(array $blocks): self { $this->blocks = $blocks; return $this; } /** * Get blocks of ApplicationExtension * * @return array */ public function getBlocks(): array { return $this->blocks; } } gif/src/Blocks/Trailer.php000064400000000266152665633640011454 0ustar00 $applicationExtensions */ protected array $applicationExtensions = []; /** * @var array $commentExtensions */ protected array $commentExtensions = []; public function __construct( protected ImageDescriptor $imageDescriptor = new ImageDescriptor(), protected ImageData $imageData = new ImageData() ) { // } public function addEntity(AbstractEntity $entity): self { return match (true) { $entity instanceof TableBasedImage => $this->setTableBasedImage($entity), $entity instanceof GraphicControlExtension => $this->setGraphicControlExtension($entity), $entity instanceof ImageDescriptor => $this->setImageDescriptor($entity), $entity instanceof ColorTable => $this->setColorTable($entity), $entity instanceof ImageData => $this->setImageData($entity), $entity instanceof PlainTextExtension => $this->setPlainTextExtension($entity), $entity instanceof NetscapeApplicationExtension, $entity instanceof ApplicationExtension => $this->addApplicationExtension($entity), $entity instanceof CommentExtension => $this->addCommentExtension($entity), default => $this, }; } /** * Return application extensions of current frame block * * @return array */ public function getApplicationExtensions(): array { return $this->applicationExtensions; } /** * Return comment extensions of current frame block * * @return array */ public function getCommentExtensions(): array { return $this->commentExtensions; } /** * Set the graphic control extension * * @param GraphicControlExtension $extension * @return self */ public function setGraphicControlExtension(GraphicControlExtension $extension): self { $this->graphicControlExtension = $extension; return $this; } /** * Get the graphic control extension of the current frame block * * @return null|GraphicControlExtension */ public function getGraphicControlExtension(): ?GraphicControlExtension { return $this->graphicControlExtension; } /** * Set the image descriptor * * @param ImageDescriptor $descriptor * @return self */ public function setImageDescriptor(ImageDescriptor $descriptor): self { $this->imageDescriptor = $descriptor; return $this; } /** * Get the image descriptor of the frame block * * @return ImageDescriptor */ public function getImageDescriptor(): ImageDescriptor { return $this->imageDescriptor; } /** * Set the color table of the current frame block * * @param ColorTable $table * @return FrameBlock */ public function setColorTable(ColorTable $table): self { $this->colorTable = $table; return $this; } /** * Get color table * * @return null|ColorTable */ public function getColorTable(): ?ColorTable { return $this->colorTable; } /** * Determine if frame block has color table * * @return bool */ public function hasColorTable(): bool { return !is_null($this->colorTable); } /** * Set image data of frame block * * @param ImageData $data * @return self */ public function setImageData(ImageData $data): self { $this->imageData = $data; return $this; } /** * Get image data of current frame block * * @return ImageData */ public function getImageData(): ImageData { return $this->imageData; } /** * Set plain text extension * * @param PlainTextExtension $extension * @return self */ public function setPlainTextExtension(PlainTextExtension $extension): self { $this->plainTextExtension = $extension; return $this; } /** * Get plain text extension * * @return null|PlainTextExtension */ public function getPlainTextExtension(): ?PlainTextExtension { return $this->plainTextExtension; } /** * Add given application extension to the current frame block * * @param ApplicationExtension $extension * @return self */ public function addApplicationExtension(ApplicationExtension $extension): self { $this->applicationExtensions[] = $extension; return $this; } /** * Add given comment extension to the current frame block * * @param CommentExtension $extension * @return self */ public function addCommentExtension(CommentExtension $extension): self { $this->commentExtensions[] = $extension; return $this; } /** * Return netscape extension of the frame block if available * * @return null|NetscapeApplicationExtension */ public function getNetscapeExtension(): ?NetscapeApplicationExtension { $extensions = array_filter( $this->applicationExtensions, fn(ApplicationExtension $extension): bool => $extension instanceof NetscapeApplicationExtension, ); return count($extensions) ? reset($extensions) : null; } /** * Set the table based image of the current frame block * * @param TableBasedImage $tableBasedImage * @return self */ public function setTableBasedImage(TableBasedImage $tableBasedImage): self { $this->setImageDescriptor($tableBasedImage->getImageDescriptor()); if ($colorTable = $tableBasedImage->getColorTable()) { $this->setColorTable($colorTable); } $this->setImageData($tableBasedImage->getImageData()); return $this; } } gif/src/Blocks/ColorTable.php000064400000004760152665633640012103 0ustar00 $colors * @return void */ public function __construct(protected array $colors = []) { // } /** * Return array of current colors * * @return array */ public function getColors(): array { return array_values($this->colors); } /** * Add color to table * * @param int $r * @param int $g * @param int $b * @return self */ public function addRgb(int $r, int $g, int $b): self { $this->addColor(new Color($r, $g, $b)); return $this; } /** * Add color to table * * @param Color $color * @return self */ public function addColor(Color $color): self { $this->colors[] = $color; return $this; } /** * Reset colors to array of color objects * * @param array $colors * @return self */ public function setColors(array $colors): self { $this->empty(); foreach ($colors as $color) { $this->addColor($color); } return $this; } /** * Count colors of current instance * * @return int */ public function countColors(): int { return count($this->colors); } /** * Determine if any colors are present on the current table * * @return bool */ public function hasColors(): bool { return $this->countColors() >= 1; } /** * Empty color table * * @return self */ public function empty(): self { $this->colors = []; return $this; } /** * Get size of color table in logical screen descriptor * * @return int */ public function getLogicalSize(): int { return match ($this->countColors()) { 4 => 1, 8 => 2, 16 => 3, 32 => 4, 64 => 5, 128 => 6, 256 => 7, default => 0, }; } /** * Calculate the number of bytes contained by the current table * * @return int */ public function getByteSize(): int { if (!$this->hasColors()) { return 0; } return 3 * pow(2, $this->getLogicalSize() + 1); } } gif/src/Blocks/TableBasedImage.php000064400000001745152665633640013006 0ustar00imageDescriptor; } public function setImageDescriptor(ImageDescriptor $descriptor): self { $this->imageDescriptor = $descriptor; return $this; } public function getImageData(): ImageData { return $this->imageData; } public function setImageData(ImageData $data): self { $this->imageData = $data; return $this; } public function getColorTable(): ?ColorTable { return $this->colorTable; } public function setColorTable(ColorTable $table): self { $this->colorTable = $table; return $this; } } gif/src/Blocks/NetscapeApplicationExtension.php000064400000002145152665633640015673 0ustar00setApplication(self::IDENTIFIER . self::AUTH_CODE); $this->setBlocks([new DataSubBlock(self::SUB_BLOCK_PREFIX . "\x00\x00")]); } /** * Get number of loops * * @return int */ public function getLoops(): int { return unpack('v*', substr($this->getBlocks()[0]->getValue(), 1))[1]; } /** * Set number of loops * * @param int $loops * @throws FormatException * @return self */ public function setLoops(int $loops): self { $this->setBlocks([ new DataSubBlock(self::SUB_BLOCK_PREFIX . pack('v*', $loops)) ]); return $this; } } gif/src/Builder.php000064400000013561152665633640010225 0ustar00gif; } /** * Set canvas size of gif * * @param int $width * @param int $height * @return Builder */ public function setSize(int $width, int $height): self { $this->gif->getLogicalScreenDescriptor()->setSize($width, $height); return $this; } /** * Create new canvas * * @param int $width * @param int $height * @return self */ public static function canvas(int $width, int $height): self { return (new self())->setSize($width, $height); } /** * Set loop count * * @param int $loops * @return Builder * @throws Exception */ public function setLoops(int $loops): self { if ($this->gif->getFrames() === []) { throw new Exception('Add at least one frame before setting the loop count'); } if ($loops >= 0) { // add frame count to existing or new netscape extension on first frame if (!$this->gif->getFirstFrame()->getNetscapeExtension()) { $this->gif->getFirstFrame()->addApplicationExtension( new NetscapeApplicationExtension() ); } $this->gif->getFirstFrame()->getNetscapeExtension()->setLoops($loops); } return $this; } /** * Create new animation frame from given source * which can be path to a file or GIF image data * * @param string|resource $source * @param float $delay time delay in seconds * @param int $left position offset in pixels from left * @param int $top position offset in pixels from top * @param bool $interlaced * @throws DecoderException * @return Builder */ 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 * * @param GifDataStream $source * @param int $delay * @param DisposalMethod $disposalMethod * @return GraphicControlExtension */ protected function buildGraphicControlExtension( GifDataStream $source, int $delay, DisposalMethod $disposalMethod = DisposalMethod::BACKGROUND ): GraphicControlExtension { // create extension $extension = new GraphicControlExtension($delay, $disposalMethod); // set transparency index $control = $source->getFirstFrame()->getGraphicControlExtension(); if ($control && $control->getTransparentColorExistance()) { $extension->setTransparentColorExistance(); $extension->setTransparentColorIndex( $control->getTransparentColorIndex() ); } return $extension; } /** * Build table based image object from given source * * @param GifDataStream $source * @param int $left * @param int $top * @param bool $interlaced * @return TableBasedImage */ 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->getImageDescriptor()->setLocalColorTableExistance(); $block->setColorTable($source->getGlobalColorTable()); $block->getImageDescriptor()->setLocalColorTableSorted( $source->getLogicalScreenDescriptor()->getGlobalColorTableSorted() ); $block->getImageDescriptor()->setLocalColorTableSize( $source->getLogicalScreenDescriptor()->getGlobalColorTableSize() ); $block->getImageDescriptor()->setSize( $source->getLogicalScreenDescriptor()->getWidth(), $source->getLogicalScreenDescriptor()->getHeight() ); // set position $block->getImageDescriptor()->setPosition($left, $top); // set interlaced flag $block->getImageDescriptor()->setInterlaced($interlaced); // add image data from source $block->setImageData($source->getFirstFrame()->getImageData()); return $block; } /** * Encode the current build * * @throws EncoderException * @return string */ public function encode(): string { return $this->gif->encode(); } } gif/src/Encoders/ImageDescriptorEncoder.php000064400000004504152665633640014757 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { return implode('', [ ImageDescriptor::SEPARATOR, $this->encodeLeft(), $this->encodeTop(), $this->encodeWidth(), $this->encodeHeight(), $this->encodePackedField(), ]); } /** * Encode left value * * @return string */ protected function encodeLeft(): string { return pack('v*', $this->source->getLeft()); } /** * Encode top value * * @return string */ protected function encodeTop(): string { return pack('v*', $this->source->getTop()); } /** * Encode width value * * @return string */ protected function encodeWidth(): string { return pack('v*', $this->source->getWidth()); } /** * Encode height value * * @return string */ protected function encodeHeight(): string { return pack('v*', $this->source->getHeight()); } /** * Encode size of local color table * * @return string */ protected function encodeLocalColorTableSize(): string { return str_pad(decbin($this->source->getLocalColorTableSize()), 3, '0', STR_PAD_LEFT); } /** * Encode reserved field * * @return string */ protected function encodeReservedField(): string { return str_pad('0', 2, '0', STR_PAD_LEFT); } /** * Encode packed field * * @return string */ protected function encodePackedField(): string { return pack('C', bindec(implode('', [ (int) $this->source->getLocalColorTableExistance(), (int) $this->source->isInterlaced(), (int) $this->source->getLocalColorTableSorted(), $this->encodeReservedField(), $this->encodeLocalColorTableSize(), ]))); } } gif/src/Encoders/TableBasedImageEncoder.php000064400000001223152665633640014622 0ustar00source = $source; } public function encode(): string { return implode('', [ $this->source->getImageDescriptor()->encode(), $this->source->getColorTable() ? $this->source->getColorTable()->encode() : '', $this->source->getImageData()->encode(), ]); } } gif/src/Encoders/FrameBlockEncoder.php000064400000003004152665633640013675 0ustar00source = $source; } /** * @throws EncoderException */ public function encode(): string { $graphicControlExtension = $this->source->getGraphicControlExtension(); $colorTable = $this->source->getColorTable(); $plainTextExtension = $this->source->getPlainTextExtension(); return implode('', [ implode('', array_map( fn(ApplicationExtension $extension): string => $extension->encode(), $this->source->getApplicationExtensions(), )), implode('', array_map( fn(CommentExtension $extension): string => $extension->encode(), $this->source->getCommentExtensions(), )), $plainTextExtension ? $plainTextExtension->encode() : '', $graphicControlExtension ? $graphicControlExtension->encode() : '', $this->source->getImageDescriptor()->encode(), $colorTable ? $colorTable->encode() : '', $this->source->getImageData()->encode(), ]); } } gif/src/Encoders/CommentExtensionEncoder.php000064400000001756152665633640015203 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { return implode('', [ CommentExtension::MARKER, CommentExtension::LABEL, $this->encodeComments(), CommentExtension::TERMINATOR, ]); } /** * Encode comment blocks * * @return string */ protected function encodeComments(): string { return implode('', array_map(function (string $comment): string { return pack('C', strlen($comment)) . $comment; }, $this->source->getComments())); } } gif/src/Encoders/LogicalScreenDescriptorEncoder.php000064400000004775152665633640016461 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { return implode('', [ $this->encodeWidth(), $this->encodeHeight(), $this->encodePackedField(), $this->encodeBackgroundColorIndex(), $this->encodePixelAspectRatio(), ]); } /** * Encode width of current instance * * @return string */ protected function encodeWidth(): string { return pack('v*', $this->source->getWidth()); } /** * Encode height of current instance * * @return string */ protected function encodeHeight(): string { return pack('v*', $this->source->getHeight()); } /** * Encode background color index of global color table * * @return string */ protected function encodeBackgroundColorIndex(): string { return pack('C', $this->source->getBackgroundColorIndex()); } /** * Encode pixel aspect ratio * * @return string */ protected function encodePixelAspectRatio(): string { return pack('C', $this->source->getPixelAspectRatio()); } /** * Return color resolution for encoding * * @return string */ protected function encodeColorResolution(): string { return str_pad(decbin($this->source->getBitsPerPixel() - 1), 3, '0', STR_PAD_LEFT); } /** * Encode size of global color table * * @return string */ protected function encodeGlobalColorTableSize(): string { return str_pad(decbin($this->source->getGlobalColorTableSize()), 3, '0', STR_PAD_LEFT); } /** * Encode packed field of current instance * * @return string */ protected function encodePackedField(): string { return pack('C', bindec(implode('', [ (int) $this->source->getGlobalColorTableExistance(), $this->encodeColorResolution(), (int) $this->source->getGlobalColorTableSorted(), $this->encodeGlobalColorTableSize(), ]))); } } gif/src/Encoders/ColorTableEncoder.php000064400000001335152665633640013723 0ustar00source = $source; } /** * Encode current source * * @throws EncoderException * @return string */ public function encode(): string { return implode('', array_map( fn(Color $color): string => $color->encode(), $this->source->getColors(), )); } } gif/src/Encoders/DataSubBlockEncoder.php000064400000001054152665633640014171 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { return pack('C', $this->source->getSize()) . $this->source->getValue(); } } gif/src/Encoders/GraphicControlExtensionEncoder.php000064400000003303152665633640016505 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { return implode('', [ GraphicControlExtension::MARKER, GraphicControlExtension::LABEL, GraphicControlExtension::BLOCKSIZE, $this->encodePackedField(), $this->encodeDelay(), $this->encodeTransparentColorIndex(), GraphicControlExtension::TERMINATOR, ]); } /** * Encode delay time * * @return string */ protected function encodeDelay(): string { return pack('v*', $this->source->getDelay()); } /** * Encode transparent color index * * @return string */ protected function encodeTransparentColorIndex(): string { return pack('C', $this->source->getTransparentColorIndex()); } /** * Encode packed field * * @return string */ protected function encodePackedField(): string { return pack('C', bindec(implode('', [ str_pad('0', 3, '0', STR_PAD_LEFT), str_pad(decbin($this->source->getDisposalMethod()->value), 3, '0', STR_PAD_LEFT), (int) $this->source->getUserInput(), (int) $this->source->getTransparentColorExistance(), ]))); } } gif/src/Encoders/ApplicationExtensionEncoder.php000064400000002002152665633640016025 0ustar00source = $source; } /** * Encode current source * * @throws EncoderException * @return string */ public function encode(): string { return implode('', [ ApplicationExtension::MARKER, ApplicationExtension::LABEL, pack('C', $this->source->getBlockSize()), $this->source->getApplication(), implode('', array_map(fn(DataSubBlock $block): string => $block->encode(), $this->source->getBlocks())), ApplicationExtension::TERMINATOR, ]); } } gif/src/Encoders/HeaderEncoder.php000064400000001004152665633640013056 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { return Header::SIGNATURE . $this->source->getVersion(); } } gif/src/Encoders/PlainTextExtensionEncoder.php000064400000002417152665633640015504 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { if (!$this->source->hasText()) { return ''; } return implode('', [ PlainTextExtension::MARKER, PlainTextExtension::LABEL, $this->encodeHead(), $this->encodeTexts(), PlainTextExtension::TERMINATOR, ]); } /** * Encode head block * * @return string */ protected function encodeHead(): string { return "\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; } /** * Encode text chunks * * @return string */ protected function encodeTexts(): string { return implode('', array_map( fn(string $text): string => pack('C', strlen($text)) . $text, $this->source->getText(), )); } } gif/src/Encoders/NetscapeApplicationExtensionEncoder.php000064400000002016152665633640017515 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { return implode('', [ ApplicationExtension::MARKER, ApplicationExtension::LABEL, pack('C', $this->source->getBlockSize()), $this->source->getApplication(), implode('', array_map(fn(DataSubBlock $block): string => $block->encode(), $this->source->getBlocks())), ApplicationExtension::TERMINATOR, ]); } } gif/src/Encoders/ColorEncoder.php000064400000001530152665633640012750 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { return implode('', [ $this->encodeColorValue($this->source->getRed()), $this->encodeColorValue($this->source->getGreen()), $this->encodeColorValue($this->source->getBlue()), ]); } /** * Encode color value * * @return string */ protected function encodeColorValue(int $value): string { return pack('C', $value); } } gif/src/Encoders/GifDataStreamEncoder.php000064400000003542152665633640014352 0ustar00source = $source; } /** * Encode current source * * @throws EncoderException * @return string */ public function encode(): string { return implode('', [ $this->source->getHeader()->encode(), $this->source->getLogicalScreenDescriptor()->encode(), $this->maybeEncodeGlobalColorTable(), $this->encodeFrames(), $this->encodeComments(), $this->source->getTrailer()->encode(), ]); } protected function maybeEncodeGlobalColorTable(): string { if (!$this->source->hasGlobalColorTable()) { return ''; } return $this->source->getGlobalColorTable()->encode(); } /** * Encode data blocks of source * * @throws EncoderException * @return string */ protected function encodeFrames(): string { return implode('', array_map( fn(FrameBlock $frame): string => $frame->encode(), $this->source->getFrames(), )); } /** * Encode comment extension blocks of source * * @throws EncoderException * @return string */ protected function encodeComments(): string { return implode('', array_map( fn(CommentExtension $commentExtension): string => $commentExtension->encode(), $this->source->getComments() )); } } gif/src/Encoders/AbstractEncoder.php000064400000000567152665633640013446 0ustar00source = $source; } /** * Encode current source * * @throws EncoderException * @return string */ public function encode(): string { if (!$this->source->hasBlocks()) { throw new EncoderException("No data blocks in ImageData."); } return implode('', [ pack('C', $this->source->getLzwMinCodeSize()), implode('', array_map( fn(DataSubBlock $block): string => $block->encode(), $this->source->getBlocks(), )), AbstractEntity::TERMINATOR, ]); } } gif/src/Encoders/TrailerEncoder.php000064400000000750152665633640013277 0ustar00source = $source; } /** * Encode current source * * @return string */ public function encode(): string { return Trailer::MARKER; } } gif/src/Decoder.php000064400000002217152665633640010200 0ustar00 self::getHandleFromFilePath($input), is_string($input) => self::getHandleFromData($input), self::isFileHandle($input) => $input, default => throw new DecoderException( 'Decoder input must be either file path, file pointer resource or binary data.' ) }; rewind($handle); return GifDataStream::decode($handle); } /** * Determine if input is file pointer resource * * @param mixed $input * @return bool */ private static function isFileHandle(mixed $input): bool { return is_resource($input) && get_resource_type($input) === 'stream'; } } gif/composer.json000064400000002033152665633640010051 0ustar00{ "name": "intervention/gif", "description": "Native PHP GIF Encoder/Decoder", "homepage": "https://github.com/intervention/gif", "keywords": [ "image", "gd", "gif", "animation" ], "license": "MIT", "authors": [ { "name": "Oliver Vogel", "email": "oliver@intervention.io", "homepage": "https://intervention.io/" } ], "require": { "php": "^8.1" }, "require-dev": { "phpunit/phpunit": "^10.0 || ^11.0 || ^12.0", "phpstan/phpstan": "^2.1", "squizlabs/php_codesniffer": "^3.8", "slevomat/coding-standard": "~8.0" }, "autoload": { "psr-4": { "Intervention\\Gif\\": "src" } }, "autoload-dev": { "psr-4": { "Intervention\\Gif\\Tests\\": "tests" } }, "minimum-stability": "stable", "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } } } image/readme.md000064400000005456152665633640007437 0ustar00# Intervention Image ## PHP Image Processing [![Latest Version](https://img.shields.io/packagist/v/intervention/image.svg)](https://packagist.org/packages/intervention/image) [![Build Status](https://github.com/Intervention/image/actions/workflows/run-tests.yml/badge.svg)](https://github.com/Intervention/image/actions) [![Monthly Downloads](https://img.shields.io/packagist/dm/intervention/image.svg)](https://packagist.org/packages/intervention/image/stats) [![Support me on Ko-fi](https://raw.githubusercontent.com/Intervention/image/develop/.github/images/support.svg)](https://ko-fi.com/interventionphp) Intervention Image is a **PHP image processing library** that provides a simple and expressive way to create, edit, and compose images. It comes with a universal interface for the two most popular PHP image manipulation extensions. You can choose between the GD library or Imagick as the base layer for all operations. - Simple interface for common image editing tasks - Interchangeable driver architecture - Support for animated images - Framework-agnostic - PSR-12 compliant ## Installation You can easily install this library using [Composer](https://getcomposer.org). Simply request the package with the following command: ```bash composer require intervention/image ``` ## Getting Started Learn the [basics](https://image.intervention.io/v3/basics/instantiation/) on how to use Intervention Image and more with the [official documentation](https://image.intervention.io/v3/). ## Code Examples ```php use Intervention\Image\ImageManager; // create image manager with desired driver $manager = new ImageManager( new Intervention\Image\Drivers\Gd\Driver() ); // open an image file $image = $manager->read('images/example.gif'); // resize image instance $image->resize(height: 300); // insert a watermark $image->place('images/watermark.png'); // encode edited image $encoded = $image->toJpg(); // save encoded image $encoded->save('images/example.jpg'); ``` ## Requirements Before you begin with the installation make sure that your server environment supports the following requirements. - PHP >= 8.1 - Mbstring PHP Extension - Image Processing PHP Extension ## Supported Image Libraries Depending on your environment Intervention Image lets you choose between different image processing extensions. - GD Library - Imagick PHP extension - [libvips](https://github.com/Intervention/image-driver-vips) ## Security If you discover any security related issues, please email oliver@intervention.io directly. ## Authors This library is developed and maintained by [Oliver Vogel](https://intervention.io) Thanks to the community of [contributors](https://github.com/Intervention/image/graphs/contributors) who have helped to improve this project. ## License Intervention Image is licensed under the [MIT License](LICENSE). image/LICENSE000064400000002077152665633640006661 0ustar00The MIT License (MIT) Copyright (c) 2013-present Oliver Vogel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. image/src/ModifierStack.php000064400000001646152665633640011701 0ustar00 $modifiers * @return void */ public function __construct(protected array $modifiers) { // } /** * Apply all modifiers in stack to the given image */ public function apply(ImageInterface $image): ImageInterface { foreach ($this->modifiers as $modifier) { $modifier->apply($image); } return $image; } /** * Append new modifier to the stack */ public function push(ModifierInterface $modifier): self { $this->modifiers[] = $modifier; return $this; } } image/src/Analyzers/PixelColorsAnalyzer.php000064400000000444152665633640015071 0ustar00mediaType; } /** * {@inheritdoc} * * @see EncodedImageInterface::mimetype() */ public function mimetype(): string { return $this->mediaType(); } /** * {@inheritdoc} * * @see EncodedImageInterface::toDataUri() */ public function toDataUri(): string { return sprintf('data:%s;base64,%s', $this->mediaType(), base64_encode((string) $this)); } /** * Show debug info for the current image * * @return array */ public function __debugInfo(): array { return [ 'mediaType' => $this->mediaType(), 'size' => $this->size(), ]; } } image/src/Modifiers/PixelateModifier.php000064400000000403152665633640014316 0ustar00size() ->contain( $this->width, $this->height ) ->alignPivotTo( $this->getResizeSize($image), $this->position ); } public function getResizeSize(ImageInterface $image): SizeInterface { return new Rectangle($this->width, $this->height); } } image/src/Modifiers/SliceAnimationModifier.php000064400000000453152665633640015447 0ustar00 new Rectangle( is_null($this->width) ? $image->width() : $image->width() + $this->width, is_null($this->height) ? $image->height() : $image->height() + $this->height, ), default => new Rectangle( is_null($this->width) ? $image->width() : $this->width, is_null($this->height) ? $image->height() : $this->height, ), }; return $size->alignPivotTo($image->size(), $this->position); } } image/src/Modifiers/BlurModifier.php000064400000000401152665633640013445 0ustar00core()->frame($this->normalizePosition($image)); } /** * Return the position of the selected frame as integer * * @throws InputException */ protected function normalizePosition(ImageInterface $image): int { if (is_int($this->position)) { return $this->position; } if (is_numeric($this->position)) { return (int) $this->position; } // calculate position from percentage value if (preg_match("/^(?P[0-9]{1,3})%$/", $this->position, $matches) != 1) { throw new InputException( 'Position must be either integer or a percent value as string.' ); } $total = count($image); $position = intval(round($total / 100 * intval($matches['percent']))); return $position == $total ? $position - 1 : $position; } } image/src/Modifiers/ScaleDownModifier.php000064400000000203152665633640014420 0ustar00size() ->containMax( $this->width, $this->height ) ->alignPivotTo( $this->getResizeSize($image), $this->position ); } } image/src/Modifiers/RotateModifier.php000064400000000745152665633640014012 0ustar00angle, 360); } } image/src/Modifiers/DrawPolygonModifier.php000064400000000624152665633640015015 0ustar00drawable; } } image/src/Modifiers/AbstractDrawModifier.php000064400000002307152665633640015131 0ustar00driver()->handleInput($this->drawable()->backgroundColor()); } catch (DecoderException) { return $this->driver()->handleInput('transparent'); } return $color; } /** * @throws RuntimeException */ public function borderColor(): ColorInterface { try { $color = $this->driver()->handleInput($this->drawable()->borderColor()); } catch (DecoderException) { return $this->driver()->handleInput('transparent'); } return $color; } } image/src/Modifiers/FlopModifier.php000064400000000275152665633640013452 0ustar00handleInput( $this->color ?: $driver->config()->blendingColor ); // replace alpha channel value with opaque value if ($color->isTransparent()) { return new Color( $color->channel(Red::class)->value(), $color->channel(Green::class)->value(), $color->channel(Blue::class)->value(), ); } return $color; } } image/src/Modifiers/TrimModifier.php000064400000000525152665633640013463 0ustar00driver()->handleInput($this->font->color()); if ($this->font->hasStrokeEffect() && $color->isTransparent()) { throw new ColorException( 'The text color must be fully opaque when using the stroke effect.' ); } return $color; } /** * Decode outline stroke color * * @throws RuntimeException * @throws ColorException */ protected function strokeColor(): ColorInterface { $color = $this->driver()->handleInput($this->font->strokeColor()); if ($color->isTransparent()) { throw new ColorException( 'The stroke color must be fully opaque.' ); } return $color; } /** * Return array of offset points to draw text stroke effect below the actual text * * @return array */ protected function strokeOffsets(FontInterface $font): array { $offsets = []; if ($font->strokeWidth() <= 0) { return $offsets; } for ($x = $font->strokeWidth() * -1; $x <= $font->strokeWidth(); $x++) { for ($y = $font->strokeWidth() * -1; $y <= $font->strokeWidth(); $y++) { $offsets[] = new Point($x, $y); } } return $offsets; } } image/src/Modifiers/DrawEllipseModifier.php000064400000000624152665633640014763 0ustar00drawable; } } image/src/Modifiers/ColorspaceModifier.php000064400000002064152665633640014642 0ustar00target)) { return $this->target; } if (in_array($this->target, ['rgb', 'RGB', RgbColorspace::class])) { return new RgbColorspace(); } if (in_array($this->target, ['cmyk', 'CMYK', CmykColorspace::class])) { return new CmykColorspace(); } throw new NotSupportedException('Given colorspace is not supported.'); } } image/src/Modifiers/ResizeModifier.php000064400000000563152665633640014013 0ustar00drawable; } } image/src/Modifiers/ResizeDownModifier.php000064400000000205152665633640014634 0ustar00drawable; } } image/src/Modifiers/InvertModifier.php000064400000000277152665633640014023 0ustar00drawable; } } image/src/Modifiers/CoverModifier.php000064400000002234152665633640013625 0ustar00size(); $crop = new Rectangle($this->width, $this->height); return $crop->contain( $imagesize->width(), $imagesize->height() )->alignPivotTo($imagesize, $this->position); } /** * @throws RuntimeException */ public function getResizeSize(SizeInterface $size): SizeInterface { return $size->resize($this->width, $this->height); } } image/src/Modifiers/ResizeCanvasRelativeModifier.php000064400000000225152665633640016636 0ustar00position instanceof PointInterface; } } image/src/Modifiers/BrightnessModifier.php000064400000000406152665633640014656 0ustar00width, $this->height); $crop->align($this->position); return $crop->alignPivotTo( $image->size(), $this->position ); } } image/src/Modifiers/PlaceModifier.php000064400000002135152665633640013573 0ustar00size()->movePivot( $this->position, $this->offset_x, $this->offset_y ); $watermark_size = $watermark->size()->movePivot( $this->position ); return $image_size->relativePositionTo($watermark_size); } } image/src/Modifiers/ProfileModifier.php000064400000000506152665633640014147 0ustar00 */ public function specializable(): array; /** * Set the driver for which the object is specialized * * @throws DriverException */ public function setDriver(DriverInterface $driver): self; /** * Return the driver for which the object was specialized */ public function driver(): DriverInterface; } image/src/Interfaces/FileInterface.php000064400000001301152665633640013724 0ustar00 $objects * @throws NotSupportedException * @throws DriverException * @return array */ public function specializeMultiple(array $objects): array; /** * Create new image instance with the current driver in given dimensions * * @throws RuntimeException */ public function createImage(int $width, int $height): ImageInterface; /** * Create new animated image * * @throws RuntimeException */ public function createAnimation(callable $init): ImageInterface; /** * Handle given input by decoding it to ImageInterface or ColorInterface * * @param array $decoders * @throws RuntimeException */ public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface; /** * Return color processor for the given colorspace */ public function colorProcessor(ColorspaceInterface $colorspace): ColorProcessorInterface; /** * Return font processor of the current driver */ public function fontProcessor(): FontProcessorInterface; /** * Check whether all requirements for operating the driver are met and * throw exception if the check fails. * * @throws DriverException */ public function checkHealth(): void; /** * Check if the current driver supports the given format and if the * underlying PHP extension was built with support for the format. */ public function supports(string|Format|FileExtension|MediaType $identifier): bool; } image/src/Interfaces/ColorChannelInterface.php000064400000002424152665633640015423 0ustar00 $normalized */ public function colorFromNormalized(array $normalized): ColorInterface; } image/src/Interfaces/ImageManagerInterface.php000064400000004120152665633640015364 0ustar00|DecoderInterface $decoders * @throws RuntimeException */ public function read(mixed $input, string|array|DecoderInterface $decoders = []): ImageInterface; /** * Create new animated image by given callback * * @link https://image.intervention.io/v3/basics/instantiation#create-animations * * @throws RuntimeException */ public function animate(callable $init): ImageInterface; /** * Return currently used driver */ public function driver(): DriverInterface; } image/src/Interfaces/CoreInterface.php000064400000003006152665633640013741 0ustar00 */ public function setNative(mixed $native): self; /** * Count number of frames of animated image core */ public function count(): int; /** * Return frame of given position in an animated image * * @throws AnimationException */ public function frame(int $position): FrameInterface; /** * Add new frame to core * * @return CoreInterface */ public function add(FrameInterface $frame): self; /** * Return number of repetitions of an animated image */ public function loops(): int; /** * Set the number of repetitions for an animation. Where a * value of 0 means infinite repetition. * * @return CoreInterface */ public function setLoops(int $loops): self; /** * Get first frame in core * * @throws AnimationException */ public function first(): FrameInterface; /** * Get last frame in core * * @throws AnimationException */ public function last(): FrameInterface; } image/src/Interfaces/ImageInterface.php000064400000056642152665633640014111 0ustar00 */ interface ImageInterface extends IteratorAggregate, Countable { /** * Return driver of current image */ public function driver(): DriverInterface; /** * Return core of current image */ public function core(): CoreInterface; /** * Return the origin of the image */ public function origin(): Origin; /** * Set the origin of the image */ public function setOrigin(Origin $origin): self; /** * Return width of current image * * @link https://image.intervention.io/v3/basics/meta-information#read-the-pixel-width * * @throws RuntimeException */ public function width(): int; /** * Return height of current image * * @link https://image.intervention.io/v3/basics/meta-information#read-the-pixel-height * * @throws RuntimeException */ public function height(): int; /** * Return size of current image * * @link https://image.intervention.io/v3/basics/meta-information#read-the-image-size-as-an-object * * @throws RuntimeException */ public function size(): SizeInterface; /** * Encode image with given encoder * * @link https://image.intervention.io/v3/basics/image-output#encode-images * * @throws RuntimeException */ public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedImageInterface; /** * Save the image to the specified path in the file system. If no path is * given, the image will be saved at its original location. * * @link https://image.intervention.io/v3/basics/image-output#encode--save-combined * * @throws RuntimeException */ public function save(?string $path = null, mixed ...$options): self; /** * Apply given modifier to current image * * @link https://image.intervention.io/v3/modifying-images/custom-modifiers * * @throws RuntimeException */ public function modify(ModifierInterface $modifier): self; /** * Analyzer current image with given analyzer * * @throws RuntimeException */ public function analyze(AnalyzerInterface $analyzer): mixed; /** * Determine if current image is animated * * @link https://image.intervention.io/v3/modifying-images/animations#check-the-current-image-instance-for-animation */ public function isAnimated(): bool; /** * Remove all frames but keep the one at the specified position * * It is possible to specify the position as integer or string values. * With the former, the exact position passed is searched for, while * string values must represent a percentage value between '0%' and '100%' * and the respective frame position is only determined approximately. * * @link https://image.intervention.io/v3/modifying-images/animations#remove-animation * * @throws RuntimeException */ public function removeAnimation(int|string $position = 0): self; /** * Extract animation frames based on given values and discard the rest * * @link https://image.intervention.io/v3/modifying-images/animations#change-the-animation-iteration-count * * @throws RuntimeException */ public function sliceAnimation(int $offset = 0, ?int $length = null): self; /** * Return loop count of animated image * * @link https://image.intervention.io/v3/modifying-images/animations#read-the-animation-iteration-count */ public function loops(): int; /** * Set loop count of animated image * * @link https://image.intervention.io/v3/modifying-images/animations#change-the-animation-iteration-count */ public function setLoops(int $loops): self; /** * Return exif data of current image * * @link https://image.intervention.io/v3/basics/meta-information#exif-information */ public function exif(?string $query = null): mixed; /** * Set exif data for the image object */ public function setExif(CollectionInterface $exif): self; /** * Return image resolution/density * * @link https://image.intervention.io/v3/basics/meta-information#image-resolution * * @throws RuntimeException */ public function resolution(): ResolutionInterface; /** * Set image resolution * * @link https://image.intervention.io/v3/basics/meta-information#image-resolution * * @throws RuntimeException */ public function setResolution(float $x, float $y): self; /** * Get the colorspace of the image * * @link https://image.intervention.io/v3/basics/colors#read-the-image-colorspace * * @throws RuntimeException */ public function colorspace(): ColorspaceInterface; /** * Transform image to given colorspace * * @link https://image.intervention.io/v3/basics/colors#change-the-image-colorspace * * @throws RuntimeException */ public function setColorspace(string|ColorspaceInterface $colorspace): self; /** * Return color of pixel at given position on given frame position * * @link https://image.intervention.io/v3/basics/colors#color-information * * @throws RuntimeException */ public function pickColor(int $x, int $y, int $frame_key = 0): ColorInterface; /** * Return all colors of pixel at given position for all frames of image * * @link https://image.intervention.io/v3/basics/colors#color-information * * @throws RuntimeException */ public function pickColors(int $x, int $y): CollectionInterface; /** * Return color that is mixed with transparent areas when converting to a format which * does not support transparency. * * @throws RuntimeException */ public function blendingColor(): ColorInterface; /** * Set blending color will have no effect unless image is converted into a format * which does not support transparency. * * @throws RuntimeException */ public function setBlendingColor(mixed $color): self; /** * Replace transparent areas of the image with given color * * @throws RuntimeException */ public function blendTransparency(mixed $color = null): self; /** * Retrieve ICC color profile of image * * @link https://image.intervention.io/v3/basics/colors#color-profiles * * @throws RuntimeException */ public function profile(): ProfileInterface; /** * Set given icc color profile to image * * @link https://image.intervention.io/v3/basics/colors#color-profiles * * @throws RuntimeException */ public function setProfile(ProfileInterface $profile): self; /** * Remove ICC color profile from the current image * * @link https://image.intervention.io/v3/basics/colors#color-profiles * * @throws RuntimeException */ public function removeProfile(): self; /** * Apply color quantization to the current image * * @link https://image.intervention.io/v3/modifying-images/effects#reduce-colors * * @throws RuntimeException */ public function reduceColors(int $limit, mixed $background = 'transparent'): self; /** * Sharpen the current image with given strength * * @link https://image.intervention.io/v3/modifying-images/effects#sharpening-effect * * @throws RuntimeException */ public function sharpen(int $amount = 10): self; /** * Turn image into a greyscale version * * @link https://image.intervention.io/v3/modifying-images/effects#convert-image-to-a-greyscale-version * * @throws RuntimeException */ public function greyscale(): self; /** * Adjust brightness of the current image * * @link https://image.intervention.io/v3/modifying-images/effects#change-the-image-brightness * * @throws RuntimeException */ public function brightness(int $level): self; /** * Adjust color contrast of the current image * * @link https://image.intervention.io/v3/modifying-images/effects#change-the-image-contrast * * @throws RuntimeException */ public function contrast(int $level): self; /** * Apply gamma correction on the current image * * @link https://image.intervention.io/v3/modifying-images/effects#gamma-correction * * @throws RuntimeException */ public function gamma(float $gamma): self; /** * Adjust the intensity of the RGB color channels * * @link https://image.intervention.io/v3/modifying-images/effects#color-correction * * @throws RuntimeException */ public function colorize(int $red = 0, int $green = 0, int $blue = 0): self; /** * Mirror the current image horizontally * * @link https://image.intervention.io/v3/modifying-images/effects#mirror-image-vertically * * @throws RuntimeException */ public function flip(): self; /** * Mirror the current image vertically * * @link https://image.intervention.io/v3/modifying-images/effects#mirror-image-horizontally * * @throws RuntimeException */ public function flop(): self; /** * Blur current image by given strength * * @link https://image.intervention.io/v3/modifying-images/effects#blur-effect * * @throws RuntimeException */ public function blur(int $amount = 5): self; /** * Invert the colors of the current image * * @link https://image.intervention.io/v3/modifying-images/effects#invert-colors * * @throws RuntimeException */ public function invert(): self; /** * Apply pixelation filter effect on current image * * @link https://image.intervention.io/v3/modifying-images/effects#pixelation-effect * * @throws RuntimeException */ public function pixelate(int $size): self; /** * Rotate current image by given angle * * @link https://image.intervention.io/v3/modifying-images/effects#image-rotation * * @param string $background * @throws RuntimeException */ public function rotate(float $angle, mixed $background = 'ffffff'): self; /** * Rotate the image to be upright according to exif information * * @link https://image.intervention.io/v3/modifying-images/effects#image-orientation-according-to-exif-data * * @throws RuntimeException */ public function orient(): self; /** * Draw text on image * * @link https://image.intervention.io/v3/modifying-images/text-fonts * * @throws RuntimeException */ public function text(string $text, int $x, int $y, callable|Closure|FontInterface $font): self; /** * Resize image to the given width and/or height * * @link https://image.intervention.io/v3/modifying-images/resizing#simple-image-resizing * * @throws RuntimeException */ public function resize(?int $width = null, ?int $height = null): self; /** * Resize image to the given width and/or height without exceeding the original dimensions * * @link https://image.intervention.io/v3/modifying-images/resizing#resize-without-exceeding-the-original-size * * @throws RuntimeException */ public function resizeDown(?int $width = null, ?int $height = null): self; /** * Resize image to the given width and/or height and keep the original aspect ratio * * @link https://image.intervention.io/v3/modifying-images/resizing#resize-images-proportionally * * @throws RuntimeException */ public function scale(?int $width = null, ?int $height = null): self; /** * Resize image to the given width and/or height, keep the original aspect ratio * and do not exceed the original image width or height * * @link https://image.intervention.io/v3/modifying-images/resizing#scale-images-but-do-not-exceed-the-original-size * * @throws RuntimeException */ public function scaleDown(?int $width = null, ?int $height = null): self; /** * Takes the specified width and height and scales them to the largest * possible size that fits within the original size. This scaled size is * then positioned on the original and cropped, before this result is resized * to the desired size using the arguments * * @link https://image.intervention.io/v3/modifying-images/resizing#cropping--resizing-combined * * @throws RuntimeException */ public function cover(int $width, int $height, string $position = 'center'): self; /** * Same as cover() but do not exceed the original image size * * @link https://image.intervention.io/v3/modifying-images/resizing#fitted-resizing-without-exceeding-the-original-size * * @throws RuntimeException */ public function coverDown(int $width, int $height, string $position = 'center'): self; /** * Resize the boundaries of the current image to given width and height. * An anchor position can be defined to determine where the original image * is fixed. A background color can be passed to define the color of the * new emerging areas. * * @link https://image.intervention.io/v3/modifying-images/resizing#resize-image-boundaries-without-resampling-the-original-image * * @throws RuntimeException */ public function resizeCanvas( ?int $width = null, ?int $height = null, mixed $background = 'ffffff', string $position = 'center' ): self; /** * Resize canvas in the same way as resizeCanvas() but takes relative values * for the width and height, which will be added or subtracted to the * original image size. * * @link https://image.intervention.io/v3/modifying-images/resizing#resize-image-boundaries-relative-to-the-original * * @throws RuntimeException */ public function resizeCanvasRelative( ?int $width = null, ?int $height = null, mixed $background = 'ffffff', string $position = 'center' ): self; /** * Padded resizing means that the original image is scaled until it fits the * defined target size with unchanged aspect ratio. The original image is * not scaled up but only down. * * Compared to the cover() method, this method does not create cropped areas, * but possibly new empty areas on the sides of the result image. These are * filled with the specified background color. * * @link https://image.intervention.io/v3/modifying-images/resizing#resizing--padding-combined * * @param string $background * @throws RuntimeException */ public function pad( int $width, int $height, mixed $background = 'ffffff', string $position = 'center' ): self; /** * This method does the same as pad(), but the original image is also scaled * up if the target size exceeds the original size. * * @link https://image.intervention.io/v3/modifying-images/resizing#padded-resizing-with-upscaling * * @param string $background * @throws RuntimeException */ public function contain( int $width, int $height, mixed $background = 'ffffff', string $position = 'center' ): self; /** * Cut out a rectangular part of the current image with given width and * height at a given position. Define optional x,y offset coordinates * to move the cutout by the given amount of pixels. * * @link https://image.intervention.io/v3/modifying-images/resizing#cut-out-a-rectangular-part * * @throws RuntimeException */ public function crop( int $width, int $height, int $offset_x = 0, int $offset_y = 0, mixed $background = 'ffffff', string $position = 'top-left' ): self; /** * Trim the image by removing border areas of similar color within a the given tolerance * * @link https://image.intervention.io/v3/modifying-images/resizing#remove-border-areas-in-similar-color * * @throws RuntimeException * @throws AnimationException */ public function trim(int $tolerance = 0): self; /** * Place another image into the current image instance * * @link https://image.intervention.io/v3/modifying-images/inserting#insert-images * * @throws RuntimeException */ public function place( mixed $element, string $position = 'top-left', int $offset_x = 0, int $offset_y = 0, int $opacity = 100 ): self; /** * Fill image with given color * * If an optional position is specified for the filling process ln the form * of x and y coordinates, the process is executed as flood fill. This means * that the color at the specified position is taken as a reference and all * adjacent pixels are also filled with the filling color. * * If no coordinates are specified, the entire image area is filled. * * @link https://image.intervention.io/v3/modifying-images/drawing#fill-images-with-color * * @throws RuntimeException */ public function fill(mixed $color, ?int $x = null, ?int $y = null): self; /** * Draw a single pixel at given position defined by the coordinates x and y in a given color. * * @link https://image.intervention.io/v3/modifying-images/drawing#draw-pixels * * @throws RuntimeException */ public function drawPixel(int $x, int $y, mixed $color): self; /** * Draw a rectangle on the current image * * @link https://image.intervention.io/v3/modifying-images/drawing#draw-a-rectangle * * @throws RuntimeException */ public function drawRectangle(int $x, int $y, callable|Closure|Rectangle $init): self; /** * Draw ellipse on the current image * * @link https://image.intervention.io/v3/modifying-images/drawing#draw-ellipses * * @throws RuntimeException */ public function drawEllipse(int $x, int $y, callable|Closure|Ellipse $init): self; /** * Draw circle on the current image * * @link https://image.intervention.io/v3/modifying-images/drawing#draw-a-circle * * @throws RuntimeException */ public function drawCircle(int $x, int $y, callable|Closure|Circle $init): self; /** * Draw a polygon on the current image * * @link https://image.intervention.io/v3/modifying-images/drawing#draw-a-polygon * * @throws RuntimeException */ public function drawPolygon(callable|Closure|Polygon $init): self; /** * Draw a line on the current image * * @link https://image.intervention.io/v3/modifying-images/drawing#draw-a-line * * @throws RuntimeException */ public function drawLine(callable|Closure|Line $init): self; /** * Draw a bezier curve on the current image * * @link https://image.intervention.io/v3/modifying-images/drawing#draw-bezier-curves * * @throws RuntimeException */ public function drawBezier(callable|Closure|Bezier $init): self; /** * Encode image to given media (mime) type. If no type is given the image * will be encoded to the format of the originally read image. * * @link https://image.intervention.io/v3/basics/image-output#encode-images-by-media-mime-type * * @throws RuntimeException */ public function encodeByMediaType(null|string|MediaType $type = null, mixed ...$options): EncodedImageInterface; /** * Encode the image into the format represented by the given extension. If no * extension is given the image will be encoded to the format of the * originally read image. * * @link https://image.intervention.io/v3/basics/image-output#encode-images-by-file-extension * * @throws RuntimeException */ public function encodeByExtension( null|string|FileExtension $extension = null, mixed ...$options ): EncodedImageInterface; /** * Encode the image into the format represented by the given extension of * the given file path extension is given the image will be encoded to * the format of the originally read image. * * @link https://image.intervention.io/v3/basics/image-output#encode-images-by-file-path * * @throws RuntimeException */ public function encodeByPath(?string $path = null, mixed ...$options): EncodedImageInterface; /** * Encode image to JPEG format * * @link https://image.intervention.io/v3/basics/image-output#encode-jpeg-format * * @throws RuntimeException */ public function toJpeg(mixed ...$options): EncodedImageInterface; /** * Encode image to Jpeg2000 format * * @link https://image.intervention.io/v3/basics/image-output#encode-jpeg-2000-format * * @throws RuntimeException */ public function toJpeg2000(mixed ...$options): EncodedImageInterface; /** * Encode image to Webp format * * @link https://image.intervention.io/v3/basics/image-output#encode-webp-format * * @throws RuntimeException */ public function toWebp(mixed ...$options): EncodedImageInterface; /** * Encode image to PNG format * * @link https://image.intervention.io/v3/basics/image-output#encode-png-format * * @throws RuntimeException */ public function toPng(mixed ...$options): EncodedImageInterface; /** * Encode image to GIF format * * @link https://image.intervention.io/v3/basics/image-output#encode-gif-format * * @throws RuntimeException */ public function toGif(mixed ...$options): EncodedImageInterface; /** * Encode image to Bitmap format * * @link https://image.intervention.io/v3/basics/image-output#encode-windows-bitmap-format * * @throws RuntimeException */ public function toBitmap(mixed ...$options): EncodedImageInterface; /** * Encode image to AVIF format * * @link https://image.intervention.io/v3/basics/image-output#encode-av1-image-file-format-avif * * @throws RuntimeException */ public function toAvif(mixed ...$options): EncodedImageInterface; /** * Encode image to TIFF format * * @link https://image.intervention.io/v3/basics/image-output#encode-tiff-format * * @throws RuntimeException */ public function toTiff(mixed ...$options): EncodedImageInterface; /** * Encode image to HEIC format * * @link https://image.intervention.io/v3/basics/image-output#encode-heic-format * * @throws RuntimeException */ public function toHeic(mixed ...$options): EncodedImageInterface; } image/src/Interfaces/FontProcessorInterface.php000064400000002471152665633640015664 0ustar00 */ interface CollectionInterface extends Traversable { /** * Determine if the collection has item at given key */ public function has(int|string $key): bool; /** * Add item to collection * * @return CollectionInterface */ public function push(mixed $item): self; /** * Return item for given key or return default is key does not exist */ public function get(int|string $key, mixed $default = null): mixed; /** * Return item at given numeric position starting at 0 */ public function getAtPosition(int $key = 0, mixed $default = null): mixed; /** * Return first item in collection */ public function first(): mixed; /** * Return last item in collection */ public function last(): mixed; /** * Return item count of collection */ public function count(): int; /** * Empty collection * * @return CollectionInterface */ public function empty(): self; /** * Transform collection as array * * @return array */ public function toArray(): array; /** * Extract items based on given values and discard the rest. * * @return CollectionInterface */ public function slice(int $offset, ?int $length = 0): self; } image/src/Interfaces/ModifierInterface.php000064400000000537152665633640014615 0ustar00 */ public function toArray(): array; /** * Cast color object to hex encoded web color */ public function toHex(string $prefix = ''): string; /** * Return array of all color channels * * @return array */ public function channels(): array; /** * Return array of normalized color channel values * * @return array */ public function normalize(): array; /** * Retrieve the color channel by its classname * * @throws ColorException */ public function channel(string $classname): ColorChannelInterface; /** * Convert color to given colorspace */ public function convertTo(string|ColorspaceInterface $colorspace): self; /** * Determine if the current color is gray */ public function isGreyscale(): bool; /** * Determine if the current color is (semi) transparent */ public function isTransparent(): bool; /** * Determine whether the current color is completely transparent */ public function isClear(): bool; /** * Cast color object to string */ public function __toString(): string; } image/src/Traits/CanBeDriverSpecialized.php000064400000003615152665633640014722 0ustar00getConstructor()) { foreach ($constructor->getParameters() as $parameter) { $specializable[$parameter->getName()] = $this->{$parameter->getName()}; } } return $specializable; } /** * {@inheritdoc} * * @see SpecializableInterface::driver() */ public function driver(): DriverInterface { return $this->driver; } /** * {@inheritdoc} * * @see SpecializableInterface::setDriver() */ public function setDriver(DriverInterface $driver): SpecializableInterface { if (!$this->belongsToDriver($driver)) { throw new DriverException( "Class '" . $this::class . "' can not be used with " . $driver->id() . " driver." ); } $this->driver = $driver; return $this; } /** * Determine if the current object belongs to the given driver's namespace */ protected function belongsToDriver(object $driver): bool { $namespace = function (object $object): string { return (new ReflectionClass($object))->getNamespaceName(); }; return str_starts_with($namespace($this), $namespace($driver)); } } image/src/Traits/CanBuildFilePointer.php000064400000001766152665633640014250 0ustar00prepareOptions($options) as $name => $value) { if (!property_exists($this, $name)) { throw new InputException('Property ' . $name . ' does not exists for ' . $this::class . '.'); } $this->{$name} = $value; } return $this; } /** * This method makes it possible to call self::setOptions() with a single * array instead of named parameters * * @param array $options * @return array */ private function prepareOptions(array $options): array { if ($options === []) { return $options; } if (count($options) > 1) { return $options; } if (!array_key_exists(0, $options)) { return $options; } if (!is_array($options[0])) { return $options; } return $options[0]; } } image/src/Geometry/Polygon.php000064400000021401152665633640012366 0ustar00 * @implements ArrayAccess */ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInterface { use HasBorder; use HasBackgroundColor; /** * Create new polygon instance * * @param array $points * @return void */ public function __construct( protected array $points = [], protected PointInterface $pivot = new Point() ) { // } /** * {@inheritdoc} * * @see DrawableInterface::position() */ public function position(): PointInterface { return $this->pivot; } /** * {@inheritdoc} * * @see DrawableInterface::setPosition() */ public function setPosition(PointInterface $position): self { $this->pivot = $position; return $this; } /** * Implement iteration through all points of polygon * * @return Traversable */ public function getIterator(): Traversable { return new ArrayIterator($this->points); } /** * Return current pivot point */ public function pivot(): PointInterface { return $this->pivot; } /** * Change pivot point to given point */ public function setPivot(PointInterface $pivot): self { $this->pivot = $pivot; return $this; } /** * Return first point of polygon */ public function first(): ?PointInterface { if ($point = reset($this->points)) { return $point; } return null; } /** * Return last point of polygon */ public function last(): ?PointInterface { if ($point = end($this->points)) { return $point; } return null; } /** * Return polygon's point count */ public function count(): int { return count($this->points); } /** * Determine if point exists at given offset */ public function offsetExists(mixed $offset): bool { return array_key_exists($offset, $this->points); } /** * Return point at given offset */ public function offsetGet(mixed $offset): mixed { return $this->points[$offset]; } /** * Set point at given offset */ public function offsetSet(mixed $offset, mixed $value): void { $this->points[$offset] = $value; } /** * Unset offset at given offset */ public function offsetUnset(mixed $offset): void { unset($this->points[$offset]); } /** * Add given point to polygon */ public function addPoint(PointInterface $point): self { $this->points[] = $point; return $this; } /** * Calculate total horizontal span of polygon */ public function width(): int { return abs($this->mostLeftPoint()->x() - $this->mostRightPoint()->x()); } /** * Calculate total vertical span of polygon */ public function height(): int { return abs($this->mostBottomPoint()->y() - $this->mostTopPoint()->y()); } /** * Return most left point of all points in polygon */ public function mostLeftPoint(): PointInterface { $points = $this->points; usort($points, function (PointInterface $a, PointInterface $b): int { if ($a->x() === $b->x()) { return 0; } return $a->x() < $b->x() ? -1 : 1; }); return $points[0]; } /** * Return most right point in polygon */ public function mostRightPoint(): PointInterface { $points = $this->points; usort($points, function (PointInterface $a, PointInterface $b): int { if ($a->x() === $b->x()) { return 0; } return $a->x() > $b->x() ? -1 : 1; }); return $points[0]; } /** * Return most top point in polygon */ public function mostTopPoint(): PointInterface { $points = $this->points; usort($points, function (PointInterface $a, PointInterface $b): int { if ($a->y() === $b->y()) { return 0; } return $a->y() > $b->y() ? -1 : 1; }); return $points[0]; } /** * Return most bottom point in polygon */ public function mostBottomPoint(): PointInterface { $points = $this->points; usort($points, function (PointInterface $a, PointInterface $b): int { if ($a->y() === $b->y()) { return 0; } return $a->y() < $b->y() ? -1 : 1; }); return $points[0]; } /** * Return point in absolute center of the polygon */ public function centerPoint(): PointInterface { return new Point( $this->mostRightPoint()->x() - (intval(round($this->width() / 2))), $this->mostTopPoint()->y() - (intval(round($this->height() / 2))) ); } /** * Align all points of polygon horizontally to given position around pivot point */ public function align(string $position): self { switch (strtolower($position)) { case 'center': case 'middle': $diff = $this->centerPoint()->x() - $this->pivot()->x(); break; case 'right': $diff = $this->mostRightPoint()->x() - $this->pivot()->x(); break; default: case 'left': $diff = $this->mostLeftPoint()->x() - $this->pivot()->x(); break; } foreach ($this->points as $point) { $point->setX( intval($point->x() - $diff) ); } return $this; } /** * Align all points of polygon vertically to given position around pivot point */ public function valign(string $position): self { switch (strtolower($position)) { case 'center': case 'middle': $diff = $this->centerPoint()->y() - $this->pivot()->y(); break; case 'top': $diff = $this->mostTopPoint()->y() - $this->pivot()->y() - $this->height(); break; default: case 'bottom': $diff = $this->mostBottomPoint()->y() - $this->pivot()->y() + $this->height(); break; } foreach ($this->points as $point) { $point->setY( intval($point->y() - $diff), ); } return $this; } /** * Rotate points of polygon around pivot point with given angle */ public function rotate(float $angle): self { $sin = sin(deg2rad($angle)); $cos = cos(deg2rad($angle)); foreach ($this->points as $point) { // translate point to pivot $point->setX( intval($point->x() - $this->pivot()->x()), ); $point->setY( intval($point->y() - $this->pivot()->y()), ); // rotate point $x = $point->x() * $cos - $point->y() * $sin; $y = $point->x() * $sin + $point->y() * $cos; // translate point back $point->setX( intval($x + $this->pivot()->x()), ); $point->setY( intval($y + $this->pivot()->y()), ); } return $this; } /** * Move all points by given amount on the x-axis */ public function movePointsX(int $amount): self { foreach ($this->points as $point) { $point->moveX($amount); } return $this; } /** * Move all points by given amount on the y-axis */ public function movePointsY(int $amount): self { foreach ($this->points as $point) { $point->moveY($amount); } return $this; } /** * Return array of all x/y values of all points of polygon * * @return array */ public function toArray(): array { $coordinates = []; foreach ($this->points as $point) { $coordinates[] = $point->x(); $coordinates[] = $point->y(); } return $coordinates; } } image/src/Geometry/Rectangle.php000064400000020515152665633640012650 0ustar00addPoint(new Point($this->pivot->x(), $this->pivot->y())); $this->addPoint(new Point($this->pivot->x() + $width, $this->pivot->y())); $this->addPoint(new Point($this->pivot->x() + $width, $this->pivot->y() - $height)); $this->addPoint(new Point($this->pivot->x(), $this->pivot->y() - $height)); } /** * Set size of rectangle */ public function setSize(int $width, int $height): self { return $this->setWidth($width)->setHeight($height); } /** * Set width of rectangle */ public function setWidth(int $width): self { $this[1]->setX($this[0]->x() + $width); $this[2]->setX($this[3]->x() + $width); return $this; } /** * Set height of rectangle */ public function setHeight(int $height): self { $this[2]->setY($this[1]->y() + $height); $this[3]->setY($this[0]->y() + $height); return $this; } /** * Return pivot point of rectangle */ public function pivot(): PointInterface { return $this->pivot; } /** * Set pivot point of rectangle */ public function setPivot(PointInterface $pivot): self { $this->pivot = $pivot; return $this; } /** * Move pivot to the given position in the rectangle and adjust the new * position by given offset values. */ public function movePivot(string $position, int $offset_x = 0, int $offset_y = 0): self { switch (strtolower($position)) { case 'top': case 'top-center': case 'top-middle': case 'center-top': case 'middle-top': $x = intval(round($this->width() / 2)) + $offset_x; $y = $offset_y; break; case 'top-right': case 'right-top': $x = $this->width() - $offset_x; $y = $offset_y; break; case 'left': case 'left-center': case 'left-middle': case 'center-left': case 'middle-left': $x = $offset_x; $y = intval(round($this->height() / 2)) + $offset_y; break; case 'right': case 'right-center': case 'right-middle': case 'center-right': case 'middle-right': $x = $this->width() - $offset_x; $y = intval(round($this->height() / 2)) + $offset_y; break; case 'bottom-left': case 'left-bottom': $x = $offset_x; $y = $this->height() - $offset_y; break; case 'bottom': case 'bottom-center': case 'bottom-middle': case 'center-bottom': case 'middle-bottom': $x = intval(round($this->width() / 2)) + $offset_x; $y = $this->height() - $offset_y; break; case 'bottom-right': case 'right-bottom': $x = $this->width() - $offset_x; $y = $this->height() - $offset_y; break; case 'center': case 'middle': case 'center-center': case 'middle-middle': $x = intval(round($this->width() / 2)) + $offset_x; $y = intval(round($this->height() / 2)) + $offset_y; break; default: case 'top-left': case 'left-top': $x = $offset_x; $y = $offset_y; break; } $this->pivot->setPosition($x, $y); return $this; } /** * Align pivot relative to given size at given position */ public function alignPivotTo(SizeInterface $size, string $position): self { $reference = new self($size->width(), $size->height()); $reference->movePivot($position); $this->movePivot($position)->setPivot( $reference->relativePositionTo($this) ); return $this; } /** * Return relative position to given rectangle */ public function relativePositionTo(SizeInterface $rectangle): PointInterface { return new Point( $this->pivot()->x() - $rectangle->pivot()->x(), $this->pivot()->y() - $rectangle->pivot()->y() ); } /** * Return aspect ration of rectangle */ public function aspectRatio(): float { return $this->width() / $this->height(); } /** * Determine if rectangle fits into given rectangle */ public function fitsInto(SizeInterface $size): bool { if ($this->width() > $size->width()) { return false; } if ($this->height() > $size->height()) { return false; } return true; } /** * Determine if rectangle has landscape format */ public function isLandscape(): bool { return $this->width() > $this->height(); } /** * Determine if rectangle has landscape format */ public function isPortrait(): bool { return $this->width() < $this->height(); } /** * Return most top left point of rectangle */ public function topLeftPoint(): PointInterface { return $this->points[0]; } /** * Return bottom right point of rectangle */ public function bottomRightPoint(): PointInterface { return $this->points[2]; } /** * @see SizeInterface::resize() * * @throws GeometryException */ public function resize(?int $width = null, ?int $height = null): SizeInterface { return $this->resizer($width, $height)->resize($this); } /** * @see SizeInterface::resizeDown() * * @throws GeometryException */ public function resizeDown(?int $width = null, ?int $height = null): SizeInterface { return $this->resizer($width, $height)->resizeDown($this); } /** * @see SizeInterface::scale() * * @throws GeometryException */ public function scale(?int $width = null, ?int $height = null): SizeInterface { return $this->resizer($width, $height)->scale($this); } /** * @see SizeInterface::scaleDown() * * @throws GeometryException */ public function scaleDown(?int $width = null, ?int $height = null): SizeInterface { return $this->resizer($width, $height)->scaleDown($this); } /** * @see SizeInterface::cover() * * @throws GeometryException */ public function cover(int $width, int $height): SizeInterface { return $this->resizer($width, $height)->cover($this); } /** * @see SizeInterface::contain() * * @throws GeometryException */ public function contain(int $width, int $height): SizeInterface { return $this->resizer($width, $height)->contain($this); } /** * @see SizeInterface::containMax() * * @throws GeometryException */ public function containMax(int $width, int $height): SizeInterface { return $this->resizer($width, $height)->containDown($this); } /** * Create resizer instance with given target size * * @throws GeometryException */ protected function resizer(?int $width = null, ?int $height = null): RectangleResizer { return new RectangleResizer($width, $height); } /** * Show debug info for the current rectangle * * @return array */ public function __debugInfo(): array { return [ 'width' => $this->width(), 'height' => $this->height(), 'pivot' => $this->pivot, ]; } } image/src/Geometry/Ellipse.php000064400000003551152665633640012342 0ustar00pivot; } /** * {@inheritdoc} * * @see DrawableInterface::setPosition() */ public function setPosition(PointInterface $position): self { $this->pivot = $position; return $this; } /** * Return pivot point of Ellipse */ public function pivot(): PointInterface { return $this->pivot; } /** * Set size of Ellipse */ public function setSize(int $width, int $height): self { return $this->setWidth($width)->setHeight($height); } /** * Set width of Ellipse */ public function setWidth(int $width): self { $this->width = $width; return $this; } /** * Set height of Ellipse */ public function setHeight(int $height): self { $this->height = $height; return $this; } /** * Get width of Ellipse */ public function width(): int { return $this->width; } /** * Get height of Ellipse */ public function height(): int { return $this->height; } } image/src/Geometry/Bezier.php000064400000010007152665633640012157 0ustar00 * @implements ArrayAccess */ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInterface { use HasBorder; use HasBackgroundColor; /** * Create new bezier instance * * @param array $points * @return void */ public function __construct( protected array $points = [], protected PointInterface $pivot = new Point() ) { // } /** * {@inheritdoc} * * @see DrawableInterface::position() */ public function position(): PointInterface { return $this->pivot; } /** * {@inheritdoc} * * @see DrawableInterface::setPosition() */ public function setPosition(PointInterface $position): DrawableInterface { $this->pivot = $position; return $this; } /** * Implement iteration through all points of bezier * * @return Traversable */ public function getIterator(): Traversable { return new ArrayIterator($this->points); } /** * Return current pivot point */ public function pivot(): PointInterface { return $this->pivot; } /** * Change pivot point to given point */ public function setPivot(PointInterface $pivot): self { $this->pivot = $pivot; return $this; } /** * Return first control point of bezier */ public function first(): ?PointInterface { if ($point = reset($this->points)) { return $point; } return null; } /** * Return second control point of bezier */ public function second(): ?PointInterface { if (array_key_exists(1, $this->points)) { return $this->points[1]; } return null; } /** * Return third control point of bezier */ public function third(): ?PointInterface { if (array_key_exists(2, $this->points)) { return $this->points[2]; } return null; } /** * Return last control point of bezier */ public function last(): ?PointInterface { if ($point = end($this->points)) { return $point; } return null; } /** * Return bezier's point count */ public function count(): int { return count($this->points); } /** * Determine if point exists at given offset */ public function offsetExists(mixed $offset): bool { return array_key_exists($offset, $this->points); } /** * Return point at given offset */ public function offsetGet(mixed $offset): mixed { return $this->points[$offset]; } /** * Set point at given offset */ public function offsetSet(mixed $offset, mixed $value): void { $this->points[$offset] = $value; } /** * Unset offset at given offset */ public function offsetUnset(mixed $offset): void { unset($this->points[$offset]); } /** * Add given point to bezier */ public function addPoint(PointInterface $point): self { $this->points[] = $point; return $this; } /** * Return array of all x/y values of all points of bezier * * @return array */ public function toArray(): array { $coordinates = []; foreach ($this->points as $point) { $coordinates[] = $point->x(); $coordinates[] = $point->y(); } return $coordinates; } } image/src/Geometry/Pixel.php000064400000001471152665633640012025 0ustar00background = $background; return $this; } /** * {@inheritdoc} * * @see DrawableInterface::backgroundColor() */ public function backgroundColor(): ColorInterface { return $this->background; } } image/src/Geometry/Traits/HasBorder.php000064400000002571152665633640014065 0ustar00setBorderSize($size)->setBorderColor($color); } /** * {@inheritdoc} * * @see DrawableInterface::setBorderSize() */ public function setBorderSize(int $size): self { $this->borderSize = $size; return $this; } /** * {@inheritdoc} * * @see DrawableInterface::borderSize() */ public function borderSize(): int { return $this->borderSize; } /** * {@inheritdoc} * * @see DrawableInterface::setBorderColor() */ public function setBorderColor(mixed $color): self { $this->borderColor = $color; return $this; } /** * {@inheritdoc} * * @see DrawableInterface::borderColor() */ public function borderColor(): mixed { return $this->borderColor; } /** * {@inheritdoc} * * @see DrawableInterface::hasBorder() */ public function hasBorder(): bool { return $this->borderSize > 0 && !is_null($this->borderColor); } } image/src/Geometry/Traits/HasBackgroundColor.php000064400000001413152665633640015720 0ustar00backgroundColor = $color; return $this; } /** * {@inheritdoc} * * @see DrawableInterface::backgroundColor() */ public function backgroundColor(): mixed { return $this->backgroundColor; } /** * {@inheritdoc} * * @see DrawableInterface::hasBackgroundColor() */ public function hasBackgroundColor(): bool { return !empty($this->backgroundColor); } } image/src/Geometry/Tools/RectangleResizer.php000064400000021747152665633640015324 0ustar00width); } /** * Return target width of resizer if available */ protected function getTargetWidth(): ?int { return $this->hasTargetWidth() ? $this->width : null; } /** * Determine if resize has target height */ protected function hasTargetHeight(): bool { return is_int($this->height); } /** * Return target width of resizer if available */ protected function getTargetHeight(): ?int { return $this->hasTargetHeight() ? $this->height : null; } /** * Return target size object * * @throws GeometryException */ protected function getTargetSize(): SizeInterface { if (!$this->hasTargetWidth() || !$this->hasTargetHeight()) { throw new GeometryException('Target size needs width and height.'); } return new Rectangle($this->width, $this->height); } /** * Set target width of resizer */ public function toWidth(int $width): self { $this->width = $width; return $this; } /** * Set target height of resizer */ public function toHeight(int $height): self { $this->height = $height; return $this; } /** * Set target size to given size object */ public function toSize(SizeInterface $size): self { $this->width = $size->width(); $this->height = $size->height(); return $this; } /** * Get proportinal width */ protected function getProportionalWidth(SizeInterface $size): int { if (!$this->hasTargetHeight()) { return $size->width(); } return max([1, (int) round($this->height * $size->aspectRatio())]); } /** * Get proportinal height */ protected function getProportionalHeight(SizeInterface $size): int { if (!$this->hasTargetWidth()) { return $size->height(); } return max([1, (int) round($this->width / $size->aspectRatio())]); } /** * Resize given size to target size of the resizer */ public function resize(SizeInterface $size): SizeInterface { $resized = new Rectangle($size->width(), $size->height()); if ($width = $this->getTargetWidth()) { $resized->setWidth($width); } if ($height = $this->getTargetHeight()) { $resized->setHeight($height); } return $resized; } /** * Resize given size to target size of the resizer but do not exceed original size */ public function resizeDown(SizeInterface $size): SizeInterface { $resized = new Rectangle($size->width(), $size->height()); if ($width = $this->getTargetWidth()) { $resized->setWidth( min($width, $size->width()) ); } if ($height = $this->getTargetHeight()) { $resized->setHeight( min($height, $size->height()) ); } return $resized; } /** * Resize given size to target size proportinally */ public function scale(SizeInterface $size): SizeInterface { $resized = new Rectangle($size->width(), $size->height()); if ($this->hasTargetWidth() && $this->hasTargetHeight()) { $resized->setWidth(min( $this->getProportionalWidth($size), $this->getTargetWidth() )); $resized->setHeight(min( $this->getProportionalHeight($size), $this->getTargetHeight() )); } elseif ($this->hasTargetWidth()) { $resized->setWidth($this->getTargetWidth()); $resized->setHeight($this->getProportionalHeight($size)); } elseif ($this->hasTargetHeight()) { $resized->setWidth($this->getProportionalWidth($size)); $resized->setHeight($this->getTargetHeight()); } return $resized; } /** * Resize given size to target size proportinally but do not exceed original size */ public function scaleDown(SizeInterface $size): SizeInterface { $resized = new Rectangle($size->width(), $size->height()); if ($this->hasTargetWidth() && $this->hasTargetHeight()) { $resized->setWidth(min( $this->getProportionalWidth($size), $this->getTargetWidth(), $size->width() )); $resized->setHeight(min( $this->getProportionalHeight($size), $this->getTargetHeight(), $size->height() )); } elseif ($this->hasTargetWidth()) { $resized->setWidth(min( $this->getTargetWidth(), $size->width() )); $resized->setHeight(min( $this->getProportionalHeight($size), $size->height() )); } elseif ($this->hasTargetHeight()) { $resized->setWidth(min( $this->getProportionalWidth($size), $size->width() )); $resized->setHeight(min( $this->getTargetHeight(), $size->height() )); } return $resized; } /** * Scale given size to cover target size * * @param SizeInterface $size Size to be resized * @throws GeometryException */ public function cover(SizeInterface $size): SizeInterface { $resized = new Rectangle($size->width(), $size->height()); // auto height $resized->setWidth($this->getTargetWidth()); $resized->setHeight($this->getProportionalHeight($size)); if ($resized->fitsInto($this->getTargetSize())) { // auto width $resized->setWidth($this->getProportionalWidth($size)); $resized->setHeight($this->getTargetHeight()); } return $resized; } /** * Scale given size to contain target size * * @param SizeInterface $size Size to be resized * @throws GeometryException */ public function contain(SizeInterface $size): SizeInterface { $resized = new Rectangle($size->width(), $size->height()); // auto height $resized->setWidth($this->getTargetWidth()); $resized->setHeight($this->getProportionalHeight($size)); if (!$resized->fitsInto($this->getTargetSize())) { // auto width $resized->setWidth($this->getProportionalWidth($size)); $resized->setHeight($this->getTargetHeight()); } return $resized; } /** * Scale given size to contain target size but prevent upsizing * * @param SizeInterface $size Size to be resized * @throws GeometryException */ public function containDown(SizeInterface $size): SizeInterface { $resized = new Rectangle($size->width(), $size->height()); // auto height $resized->setWidth( min($size->width(), $this->getTargetWidth()) ); $resized->setHeight( min($size->height(), $this->getProportionalHeight($size)) ); if (!$resized->fitsInto($this->getTargetSize())) { // auto width $resized->setWidth( min($size->width(), $this->getProportionalWidth($size)) ); $resized->setHeight( min($size->height(), $this->getTargetHeight()) ); } return $resized; } /** * Crop target size out of given size at given position (i.e. move the pivot point) */ public function crop(SizeInterface $size, string $position = 'top-left'): SizeInterface { return $this->resize($size)->alignPivotTo( $size->movePivot($position), $position ); } } image/src/Geometry/Point.php000064400000005103152665633640012031 0ustar00 */ class Point implements PointInterface, IteratorAggregate { /** * Create new point instance * * @return void */ public function __construct( protected int $x = 0, protected int $y = 0 ) { // } /** * {@inheritdoc} * * @see IteratorAggregate::getIterator() */ public function getIterator(): Traversable { return new ArrayIterator([$this->x, $this->y]); } /** * {@inheritdoc} * * @see PointInterface::setX() */ public function setX(int $x): self { $this->x = $x; return $this; } /** * {@inheritdoc} * * @see PointInterface::x() */ public function x(): int { return $this->x; } /** * {@inheritdoc} * * @see PointInterface::setY() */ public function setY(int $y): self { $this->y = $y; return $this; } /** * {@inheritdoc} * * @see PointInterface::y() */ public function y(): int { return $this->y; } /** * {@inheritdoc} * * @see PointInterface::moveX() */ public function moveX(int $value): self { $this->x += $value; return $this; } /** * {@inheritdoc} * * @see PointInterface::moveY() */ public function moveY(int $value): self { $this->y += $value; return $this; } /** * {@inheritdoc} * * @see PointInterface::move() */ public function move(int $x, int $y): self { return $this->moveX($x)->moveY($y); } /** * {@inheritdoc} * * @see PointInterface::setPosition() */ public function setPosition(int $x, int $y): self { $this->setX($x); $this->setY($y); return $this; } /** * {@inheritdoc} * * @see PointInterface::rotate() */ public function rotate(float $angle, PointInterface $pivot): self { $sin = round(sin(deg2rad($angle)), 6); $cos = round(cos(deg2rad($angle)), 6); return $this->setPosition( intval($cos * ($this->x() - $pivot->x()) - $sin * ($this->y() - $pivot->y()) + $pivot->x()), intval($sin * ($this->x() - $pivot->x()) + $cos * ($this->y() - $pivot->y()) + $pivot->y()) ); } } image/src/Geometry/Circle.php000064400000002027152665633640012143 0ustar00setWidth($diameter); $this->setHeight($diameter); return $this; } /** * Get diameter of circle */ public function diameter(): int { return $this->width(); } /** * Set radius of circle */ public function setRadius(int $radius): self { return $this->setDiameter(intval($radius * 2)); } /** * Get radius of circle */ public function radius(): int { return intval(round($this->diameter() / 2)); } } image/src/Geometry/Factories/LineFactory.php000064400000005120152665633640015075 0ustar00line = is_a($init, Line::class) ? $init : new Line(new Point(), new Point()); if (is_callable($init)) { $init($this); } } /** * {@inheritdoc} * * @see DrawableFactoryInterface::init() */ public static function init(null|Closure|DrawableInterface $init = null): self { return new self($init); } /** * {@inheritdoc} * * @see DrawableFactoryInterface::create() */ public function create(): DrawableInterface { return $this->line; } /** * Set the color of the line to be produced */ public function color(mixed $color): self { $this->line->setBackgroundColor($color); $this->line->setBorderColor($color); return $this; } /** * Set the (background) color of the line to be produced */ public function background(mixed $color): self { $this->line->setBackgroundColor($color); $this->line->setBorderColor($color); return $this; } /** * Set the border size & border color of the line to be produced */ public function border(mixed $color, int $size = 1): self { $this->line->setBackgroundColor($color); $this->line->setBorderColor($color); $this->line->setWidth($size); return $this; } /** * Set the width of the line to be produced */ public function width(int $size): self { $this->line->setWidth($size); return $this; } /** * Set the coordinates of the starting point of the line to be produced */ public function from(int $x, int $y): self { $this->line->setStart(new Point($x, $y)); return $this; } /** * Set the coordinates of the end point of the line to be produced */ public function to(int $x, int $y): self { $this->line->setEnd(new Point($x, $y)); return $this; } /** * Produce the line */ public function __invoke(): Line { return $this->line; } } image/src/Geometry/Factories/CircleFactory.php000064400000004276152665633640015422 0ustar00circle = is_a($init, Circle::class) ? $init : new Circle(0); $this->circle->setPosition($pivot); if (is_callable($init)) { $init($this); } } /** * {@inheritdoc} * * @see DrawableFactoryInterface::init() */ public static function init(null|Closure|DrawableInterface $init = null): self { return new self(init: $init); } /** * {@inheritdoc} * * @see DrawableFactoryInterface::create() */ public function create(): DrawableInterface { return $this->circle; } /** * Set the radius of the circle to be produced */ public function radius(int $radius): self { $this->circle->setSize($radius * 2, $radius * 2); return $this; } /** * Set the diameter of the circle to be produced */ public function diameter(int $diameter): self { $this->circle->setSize($diameter, $diameter); return $this; } /** * Set the background color of the circle to be produced */ public function background(mixed $color): self { $this->circle->setBackgroundColor($color); return $this; } /** * Set the border color & border size of the ellipse to be produced */ public function border(mixed $color, int $size = 1): self { $this->circle->setBorder($color, $size); return $this; } /** * Produce the circle */ public function __invoke(): Circle { return $this->circle; } } image/src/Geometry/Factories/Drawable.php000064400000002030152665633640014374 0ustar00bezier = is_a($init, Bezier::class) ? $init : new Bezier([]); if (is_callable($init)) { $init($this); } } /** * {@inheritdoc} * * @see DrawableFactoryInterface::init() */ public static function init(null|Closure|DrawableInterface $init = null): self { return new self($init); } /** * {@inheritdoc} * * @see DrawableFactoryInterface::create() */ public function create(): DrawableInterface { return $this->bezier; } /** * Add a point to the bezier to be produced */ public function point(int $x, int $y): self { $this->bezier->addPoint(new Point($x, $y)); return $this; } /** * Set the background color of the bezier to be produced */ public function background(mixed $color): self { $this->bezier->setBackgroundColor($color); return $this; } /** * Set the border color & border size of the bezier to be produced */ public function border(mixed $color, int $size = 1): self { $this->bezier->setBorder($color, $size); return $this; } /** * Produce the bezier */ public function __invoke(): Bezier { return $this->bezier; } } image/src/Geometry/Factories/PolygonFactory.php000064400000003522152665633640015641 0ustar00polygon = is_a($init, Polygon::class) ? $init : new Polygon([]); if (is_callable($init)) { $init($this); } } /** * {@inheritdoc} * * @see DrawableFactoryInterface::init() */ public static function init(null|Closure|DrawableInterface $init = null): self { return new self($init); } /** * {@inheritdoc} * * @see DrawableFactoryInterface::create() */ public function create(): DrawableInterface { return $this->polygon; } /** * Add a point to the polygon to be produced */ public function point(int $x, int $y): self { $this->polygon->addPoint(new Point($x, $y)); return $this; } /** * Set the background color of the polygon to be produced */ public function background(mixed $color): self { $this->polygon->setBackgroundColor($color); return $this; } /** * Set the border color & border size of the polygon to be produced */ public function border(mixed $color, int $size = 1): self { $this->polygon->setBorder($color, $size); return $this; } /** * Produce the polygon */ public function __invoke(): Polygon { return $this->polygon; } } image/src/Geometry/Factories/RectangleFactory.php000064400000004656152665633640016127 0ustar00rectangle = is_a($init, Rectangle::class) ? $init : new Rectangle(0, 0, $pivot); $this->rectangle->setPosition($pivot); if (is_callable($init)) { $init($this); } } /** * {@inheritdoc} * * @see DrawableFactoryInterface::init() */ public static function init(null|Closure|DrawableInterface $init = null): self { return new self(init: $init); } /** * {@inheritdoc} * * @see DrawableFactoryInterface::create() */ public function create(): DrawableInterface { return $this->rectangle; } /** * Set the size of the rectangle to be produced */ public function size(int $width, int $height): self { $this->rectangle->setSize($width, $height); return $this; } /** * Set the width of the rectangle to be produced */ public function width(int $width): self { $this->rectangle->setWidth($width); return $this; } /** * Set the height of the rectangle to be produced */ public function height(int $height): self { $this->rectangle->setHeight($height); return $this; } /** * Set the background color of the rectangle to be produced */ public function background(mixed $color): self { $this->rectangle->setBackgroundColor($color); return $this; } /** * Set the border color & border size of the rectangle to be produced */ public function border(mixed $color, int $size = 1): self { $this->rectangle->setBorder($color, $size); return $this; } /** * Produce the rectangle */ public function __invoke(): Rectangle { return $this->rectangle; } } image/src/Geometry/Factories/EllipseFactory.php000064400000004600152665633640015605 0ustar00ellipse = is_a($init, Ellipse::class) ? $init : new Ellipse(0, 0); $this->ellipse->setPosition($pivot); if (is_callable($init)) { $init($this); } } /** * {@inheritdoc} * * @see DrawableFactoryInterface::init() */ public static function init(null|Closure|DrawableInterface $init = null): self { return new self(init: $init); } /** * {@inheritdoc} * * @see DrawableFactoryInterface::create() */ public function create(): DrawableInterface { return $this->ellipse; } /** * Set the size of the ellipse to be produced */ public function size(int $width, int $height): self { $this->ellipse->setSize($width, $height); return $this; } /** * Set the width of the ellipse to be produced */ public function width(int $width): self { $this->ellipse->setWidth($width); return $this; } /** * Set the height of the ellipse to be produced */ public function height(int $height): self { $this->ellipse->setHeight($height); return $this; } /** * Set the background color of the ellipse to be produced */ public function background(mixed $color): self { $this->ellipse->setBackgroundColor($color); return $this; } /** * Set the border color & border size of the ellipse to be produced */ public function border(mixed $color, int $size = 1): self { $this->ellipse->setBorder($color, $size); return $this; } /** * Produce the ellipse */ public function __invoke(): Ellipse { return $this->ellipse; } } image/src/Geometry/Line.php000064400000004424152665633640011634 0ustar00start; } /** * {@inheritdoc} * * @see DrawableInterface::setPosition() */ public function setPosition(PointInterface $position): DrawableInterface { $this->start = $position; return $this; } /** * Return line width */ public function width(): int { return $this->width; } /** * Set line width */ public function setWidth(int $width): self { $this->width = $width; return $this; } /** * Get starting point of line */ public function start(): PointInterface { return $this->start; } /** * get end point of line */ public function end(): PointInterface { return $this->end; } /** * Set starting point of line */ public function setStart(PointInterface $start): self { $this->start = $start; return $this; } /** * Set starting point of line by coordinates */ public function from(int $x, int $y): self { $this->start()->setX($x); $this->start()->setY($y); return $this; } /** * Set end point of line by coordinates */ public function to(int $x, int $y): self { $this->end()->setX($x); $this->end()->setY($y); return $this; } /** * Set end point of line */ public function setEnd(PointInterface $end): self { $this->end = $end; return $this; } } image/src/Collection.php000064400000010677152665633640011254 0ustar00 */ class Collection implements CollectionInterface, IteratorAggregate, Countable { /** * Create new collection object * * @param array $items * @return void */ public function __construct(protected array $items = []) { // } /** * Static constructor * * @param array $items * @return self */ public static function create(array $items = []): self { return new self($items); } /** * {@inheritdoc} * * @see CollectionInterface::has() */ public function has(int|string $key): bool { return array_key_exists($key, $this->items); } /** * Returns Iterator * * @return Traversable */ public function getIterator(): Traversable { return new ArrayIterator($this->items); } /** * {@inheritdoc} * * @see CollectionInterface::toArray() */ public function toArray(): array { return $this->items; } /** * Count items in collection */ public function count(): int { return count($this->items); } /** * Append new item to collection * * @return CollectionInterface */ public function push(mixed $item): CollectionInterface { $this->items[] = $item; return $this; } /** * Return first item in collection */ public function first(): mixed { if ($item = reset($this->items)) { return $item; } return null; } /** * Returns last item in collection */ public function last(): mixed { if ($item = end($this->items)) { return $item; } return null; } /** * Return item at given position starting at 0 */ public function getAtPosition(int $key = 0, mixed $default = null): mixed { if ($this->count() == 0) { return $default; } $positions = array_values($this->items); if (!array_key_exists($key, $positions)) { return $default; } return $positions[$key]; } /** * {@inheritdoc} * * @see CollectionInterface::get() */ public function get(int|string $query, mixed $default = null): mixed { if ($this->count() == 0) { return $default; } if (is_int($query) && array_key_exists($query, $this->items)) { return $this->items[$query]; } if (is_string($query) && !str_contains($query, '.')) { return array_key_exists($query, $this->items) ? $this->items[$query] : $default; } $query = explode('.', (string) $query); $result = $default; $items = $this->items; foreach ($query as $key) { if (!is_array($items) || !array_key_exists($key, $items)) { $result = $default; break; } $result = $items[$key]; $items = $result; } return $result; } /** * Map each item of collection by given callback */ public function map(callable $callback): self { return new self( array_map( fn(mixed $item) => $callback($item), $this->items, ) ); } /** * Run callback on each item of the collection an remove it if it does not return true */ public function filter(callable $callback): self { return new self( array_filter( $this->items, fn(mixed $item) => $callback($item), ) ); } /** * {@inheritdoc} * * @see CollectionInterface::empty() */ public function empty(): CollectionInterface { $this->items = []; return $this; } /** * {@inheritdoc} * * @see CollectionInterface::slice() */ public function slice(int $offset, ?int $length = null): CollectionInterface { $this->items = array_slice($this->items, $offset, $length); return $this; } } image/src/InputHandler.php000064400000010614152665633640011545 0ustar00 */ protected array $decoders = [ NativeObjectDecoder::class, ImageObjectDecoder::class, ColorObjectDecoder::class, RgbHexColorDecoder::class, RgbStringColorDecoder::class, CmykStringColorDecoder::class, HsvStringColorDecoder::class, HslStringColorDecoder::class, TransparentColorDecoder::class, HtmlColornameDecoder::class, FilePointerImageDecoder::class, FilePathImageDecoder::class, SplFileInfoImageDecoder::class, BinaryImageDecoder::class, DataUriImageDecoder::class, Base64ImageDecoder::class, EncodedImageObjectDecoder::class, ]; /** * Driver with which the decoder classes are specialized */ protected ?DriverInterface $driver = null; /** * Create new input handler instance with given decoder classnames * * @param array $decoders * @return void */ public function __construct(array $decoders = [], ?DriverInterface $driver = null) { $this->decoders = count($decoders) ? $decoders : $this->decoders; $this->driver = $driver; } /** * Static factory method * * @param array $decoders */ public static function withDecoders(array $decoders, ?DriverInterface $driver = null): self { return new self($decoders, $driver); } /** * {@inheritdoc} * * @see InputHandlerInterface::handle() */ public function handle(mixed $input): ImageInterface|ColorInterface { foreach ($this->decoders as $decoder) { try { // decode with driver specialized decoder return $this->resolve($decoder)->decode($input); } catch (DecoderException | NotSupportedException $e) { // try next decoder } } if (isset($e)) { throw new ($e::class)($e->getMessage()); } throw new DecoderException('Unable to decode input.'); } /** * Resolve the given classname to an decoder object * * @throws DriverException * @throws NotSupportedException */ private function resolve(string|DecoderInterface $decoder): DecoderInterface { if (($decoder instanceof DecoderInterface) && empty($this->driver)) { return $decoder; } if (($decoder instanceof DecoderInterface) && !empty($this->driver)) { return $this->driver->specialize($decoder); } if (empty($this->driver)) { return new $decoder(); } return $this->driver->specialize(new $decoder()); } } image/src/FileExtension.php000064400000006172152665633640011730 0ustar00fileExtension(); } if ($identifier instanceof MediaType) { return $identifier->fileExtension(); } try { $extension = self::from(strtolower($identifier)); } catch (Error) { try { $extension = MediaType::from(strtolower($identifier))->fileExtension(); } catch (Error) { throw new NotSupportedException('Unable to create file extension from "' . $identifier . '".'); } } return $extension; } /** * Try to create media type from given identifier and return null on failure * * @param string|Format|MediaType|FileExtension $identifier * @return FileExtension|null */ public static function tryCreate(string|self|Format|MediaType $identifier): ?self { try { return self::create($identifier); } catch (NotSupportedException) { return null; } } /** * Return the matching format for the current file extension */ public function format(): Format { return match ($this) { self::JPEG, self::JPG => Format::JPEG, self::WEBP => Format::WEBP, self::GIF => Format::GIF, self::PNG => Format::PNG, self::AVIF => Format::AVIF, self::BMP => Format::BMP, self::TIF, self::TIFF => Format::TIFF, self::JP2, self::JP2K, self::J2K, self::JPF, self::JPM, self::JPG2, self::J2C, self::JPC, self::JPX => Format::JP2, self::HEIC, self::HEIF => Format::HEIC, }; } /** * Return media types for the current format * * @return array */ public function mediaTypes(): array { return $this->format()->mediaTypes(); } /** * Return the first found media type for the current format */ public function mediaType(): MediaType { return $this->format()->mediaType(); } } image/src/Typography/FontFactory.php000064400000005276152665633640013564 0ustar00font = is_a($init, FontInterface::class) ? $init : new Font(); if (is_callable($init)) { $init($this); } } /** * Set the filename of the font to be built */ public function filename(string $value): self { $this->font->setFilename($value); return $this; } /** * {@inheritdoc} * * @see self::filename() */ public function file(string $value): self { return $this->filename($value); } /** * Set outline stroke effect for the font to be built * * @throws FontException */ public function stroke(mixed $color, int $width = 1): self { $this->font->setStrokeWidth($width); $this->font->setStrokeColor($color); return $this; } /** * Set color for the font to be built */ public function color(mixed $value): self { $this->font->setColor($value); return $this; } /** * Set the size for the font to be built */ public function size(float $value): self { $this->font->setSize($value); return $this; } /** * Set the horizontal alignment of the font to be built */ public function align(string $value): self { $this->font->setAlignment($value); return $this; } /** * Set the vertical alignment of the font to be built */ public function valign(string $value): self { $this->font->setValignment($value); return $this; } /** * Set the line height of the font to be built */ public function lineHeight(float $value): self { $this->font->setLineHeight($value); return $this; } /** * Set the rotation angle of the font to be built */ public function angle(float $value): self { $this->font->setAngle($value); return $this; } /** * Set the maximum width of the text block to be built */ public function wrap(int $width): self { $this->font->setWrapWidth($width); return $this; } /** * Build font */ public function __invoke(): FontInterface { return $this->font; } } image/src/Typography/Font.php000064400000012243152665633640012224 0ustar00filename = $filename; } /** * {@inheritdoc} * * @see FontInterface::setSize() */ public function setSize(float $size): FontInterface { $this->size = $size; return $this; } /** * {@inheritdoc} * * @see FontInterface::size() */ public function size(): float { return $this->size; } /** * {@inheritdoc} * * @see FontInterface::setAngle() */ public function setAngle(float $angle): FontInterface { $this->angle = $angle; return $this; } /** * {@inheritdoc} * * @see FontInterface::angle() */ public function angle(): float { return $this->angle; } /** * {@inheritdoc} * * @see FontInterface::setFilename() * * @throws FontException */ public function setFilename(string $filename): FontInterface { if (!file_exists($filename)) { throw new FontException('Font file ' . $filename . ' does not exist.'); } $this->filename = $filename; return $this; } /** * {@inheritdoc} * * @see FontInterface::filename() */ public function filename(): ?string { return $this->filename; } /** * {@inheritdoc} * * @see FontInterface::hasFilename() */ public function hasFilename(): bool { return !is_null($this->filename) && is_file($this->filename); } /** * {@inheritdoc} * * @see FontInterface::setColor() */ public function setColor(mixed $color): FontInterface { $this->color = $color; return $this; } /** * {@inheritdoc} * * @see FontInterface::color() */ public function color(): mixed { return $this->color; } /** * {@inheritdoc} * * @see FontInterface::setStrokeColor() */ public function setStrokeColor(mixed $color): FontInterface { $this->strokeColor = $color; return $this; } /** * {@inheritdoc} * * @see FontInterface::strokeColor() */ public function strokeColor(): mixed { return $this->strokeColor; } /** * {@inheritdoc} * * @see FontInterface::setStrokeWidth() */ public function setStrokeWidth(int $width): FontInterface { if (!in_array($width, range(0, 10))) { throw new FontException( 'The stroke width must be in the range from 0 to 10.' ); } $this->strokeWidth = $width; return $this; } /** * {@inheritdoc} * * @see FontInterface::strokeWidth() */ public function strokeWidth(): int { return $this->strokeWidth; } /** * {@inheritdoc} * * @see FontInterface::hasStrokeEffect() */ public function hasStrokeEffect(): bool { return $this->strokeWidth > 0; } /** * {@inheritdoc} * * @see FontInterface::alignment() */ public function alignment(): string { return $this->alignment; } /** * {@inheritdoc} * * @see FontInterface::setAlignment() */ public function setAlignment(string $value): FontInterface { $this->alignment = $value; return $this; } /** * {@inheritdoc} * * @see FontInterface::valignment() */ public function valignment(): string { return $this->valignment; } /** * {@inheritdoc} * * @see FontInterface::setValignment() */ public function setValignment(string $value): FontInterface { $this->valignment = $value; return $this; } /** * {@inheritdoc} * * @see FontInterface::setLineHeight() */ public function setLineHeight(float $height): FontInterface { $this->lineHeight = $height; return $this; } /** * {@inheritdoc} * * @see FontInterface::lineHeight() */ public function lineHeight(): float { return $this->lineHeight; } /** * {@inheritdoc} * * @see FontInterface::setWrapWidth() */ public function setWrapWidth(?int $width): FontInterface { $this->wrapWidth = $width; return $this; } /** * {@inheritdoc} * * @see FontInterface::wrapWidth() */ public function wrapWidth(): ?int { return $this->wrapWidth; } } image/src/Typography/TextBlock.php000064400000002562152665633640013220 0ustar00push(new Line($line)); } } /** * Return array of lines in text block * * @return array */ public function lines(): array { return $this->items; } /** * Set lines of the text block * * @param array $lines */ public function setLines(array $lines): self { $this->items = $lines; return $this; } /** * Get line by given key */ public function line(mixed $key): ?Line { if (!array_key_exists($key, $this->lines())) { return null; } return $this->lines()[$key]; } /** * Return line with most characters of text block */ public function longestLine(): Line { $lines = $this->lines(); usort($lines, function (Line $a, Line $b): int { if ($a->length() === $b->length()) { return 0; } return $a->length() > $b->length() ? -1 : 1; }); return $lines[0]; } } image/src/Typography/Line.php000064400000005360152665633640012207 0ustar00 */ class Line implements IteratorAggregate, Countable, Stringable { /** * Segments (usually individual words including punctuation marks) of the line * * @var array */ protected array $segments = []; /** * Create new text line object with given text & position * * @return void */ public function __construct( ?string $text = null, protected PointInterface $position = new Point() ) { if (is_string($text)) { $this->segments = $this->wordsSeperatedBySpaces($text) ? explode(" ", $text) : mb_str_split($text); } } /** * Add word to current line */ public function add(string $word): self { $this->segments[] = $word; return $this; } /** * Returns Iterator * * @return Traversable */ public function getIterator(): Traversable { return new ArrayIterator($this->segments); } /** * Get Position of line */ public function position(): PointInterface { return $this->position; } /** * Set position of current line */ public function setPosition(PointInterface $point): self { $this->position = $point; return $this; } /** * Count segments (individual words including punctuation marks) of line */ public function count(): int { return count($this->segments); } /** * Count characters of line */ public function length(): int { return mb_strlen((string) $this); } /** * Dermine if words are sperarated by spaces in the written language of the given text */ private function wordsSeperatedBySpaces(string $text): bool { return 1 !== preg_match( '/[' . '\x{4E00}-\x{9FFF}' . // CJK Unified Ideographs (chinese) '\x{3400}-\x{4DBF}' . // CJK Unified Ideographs Extension A (chinese) '\x{3040}-\x{309F}' . // hiragana (japanese) '\x{30A0}-\x{30FF}' . // katakana (japanese) '\x{0E00}-\x{0E7F}' . // thai ']/u', $text ); } /** * Cast line to string */ public function __toString(): string { $string = implode("", $this->segments); if ($this->wordsSeperatedBySpaces($string)) { return implode(" ", $this->segments); } return $string; } } image/src/Resolution.php000064400000006021152665633640011310 0ustar00 */ class Resolution implements ResolutionInterface, Stringable, IteratorAggregate { public const PER_INCH = 1; public const PER_CM = 2; /** * Create new instance */ public function __construct( protected float $x, protected float $y, protected int $per_unit = self::PER_INCH ) { // } /** * {@inheritdoc} * * @see IteratorAggregate::getIterator() */ public function getIterator(): Traversable { return new ArrayIterator([$this->x, $this->y]); } /** * {@inheritdoc} * * @see ResolutionInterface::x() */ public function x(): float { return $this->x; } /** * {@inheritdoc} * * @see ResolutionInterface::setX() */ public function setX(float $x): self { $this->x = $x; return $this; } /** * {@inheritdoc} * * @see ResolutionInterface::y() */ public function y(): float { return $this->y; } /** * {@inheritdoc} * * @see ResolutionInterface::setY() */ public function setY(float $y): self { $this->y = $y; return $this; } /** * {@inheritdoc} * * @see ResolutionInterface::setPerUnit() */ protected function setPerUnit(int $per_unit): self { $this->per_unit = $per_unit; return $this; } /** * {@inheritdoc} * * @see ResolutionInterface::unit() */ public function unit(): string { return match ($this->per_unit) { self::PER_CM => 'dpcm', default => 'dpi', }; } /** * {@inheritdoc} * * @see ResolutionInterface::perInch() */ public function perInch(): self { return match ($this->per_unit) { self::PER_CM => $this ->setPerUnit(self::PER_INCH) ->setX($this->x * 2.54) ->setY($this->y * 2.54), default => $this }; } /** * {@inheritdoc} * * @see ResolutionInterface::perCm() */ public function perCm(): self { return match ($this->per_unit) { self::PER_INCH => $this ->setPerUnit(self::PER_CM) ->setX($this->x / 2.54) ->setY($this->y / 2.54), default => $this, }; } /** * {@inheritdoc} * * @see ResolutionInterface::toString() */ public function toString(): string { return sprintf("%1\$.2f x %2\$.2f %3\$s", $this->x, $this->y, $this->unit()); } /** * {@inheritdoc} * * @see ResolutionInterface::__toString() */ public function __toString(): string { return $this->toString(); } } image/src/File.php000064400000005224152665633640010030 0ustar00pointer = $this->buildFilePointer($data); } /** * Create file object from path in file system * * @throws RuntimeException */ public static function fromPath(string $path): self { return new self(fopen($path, 'r')); } /** * {@inheritdoc} * * @see FileInterface::save() */ public function save(string $filepath): void { $dir = pathinfo($filepath, PATHINFO_DIRNAME); if (!is_dir($dir)) { throw new NotWritableException( "Can't write image to path. Directory does not exist." ); } if (!is_writable($dir)) { throw new NotWritableException( "Can't write image to path. Directory is not writable." ); } if (is_file($filepath) && !is_writable($filepath)) { throw new NotWritableException( sprintf("Can't write image. Path (%s) is not writable.", $filepath) ); } // write data $saved = @file_put_contents($filepath, $this->toFilePointer()); if ($saved === false) { throw new NotWritableException( sprintf("Can't write image data to path (%s).", $filepath) ); } } /** * {@inheritdoc} * * @see FileInterface::toString() */ public function toString(): string { return stream_get_contents($this->toFilePointer(), offset: 0); } /** * {@inheritdoc} * * @see FileInterface::toFilePointer() */ public function toFilePointer() { rewind($this->pointer); return $this->pointer; } /** * {@inheritdoc} * * @see FileInterface::size() */ public function size(): int { $info = fstat($this->toFilePointer()); return intval($info['size']); } /** * {@inheritdoc} * * @see FileInterface::__toString() */ public function __toString(): string { return $this->toString(); } } image/src/Image.php000064400000065056152665633640010204 0ustar00origin = new Origin(); } /** * {@inheritdoc} * * @see ImageInterface::driver() */ public function driver(): DriverInterface { return $this->driver; } /** * {@inheritdoc} * * @see ImageInterface::core() */ public function core(): CoreInterface { return $this->core; } /** * {@inheritdoc} * * @see ImageInterface::origin() */ public function origin(): Origin { return $this->origin; } /** * {@inheritdoc} * * @see ImageInterface::setOrigin() */ public function setOrigin(Origin $origin): ImageInterface { $this->origin = $origin; return $this; } /** * {@inheritdoc} * * @see ImageInterface::count() */ public function count(): int { return $this->core->count(); } /** * Implementation of IteratorAggregate * * @return Traversable */ public function getIterator(): Traversable { return $this->core; } /** * {@inheritdoc} * * @see ImageInterface::isAnimated() */ public function isAnimated(): bool { return $this->count() > 1; } /** * {@inheritdoc} * * @see ImageInterface::removeAnimation( */ public function removeAnimation(int|string $position = 0): ImageInterface { return $this->modify(new RemoveAnimationModifier($position)); } /** * {@inheritdoc} * * @see ImageInterface::sliceAnimation() */ public function sliceAnimation(int $offset = 0, ?int $length = null): ImageInterface { return $this->modify(new SliceAnimationModifier($offset, $length)); } /** * {@inheritdoc} * * @see ImageInterface::loops() */ public function loops(): int { return $this->core->loops(); } /** * {@inheritdoc} * * @see ImageInterface::setLoops() */ public function setLoops(int $loops): ImageInterface { $this->core->setLoops($loops); return $this; } /** * {@inheritdoc} * * @see ImageInterface::exif() */ public function exif(?string $query = null): mixed { return is_null($query) ? $this->exif : $this->exif->get($query); } /** * {@inheritdoc} * * @see ImageInterface::setExif() */ public function setExif(CollectionInterface $exif): ImageInterface { $this->exif = $exif; return $this; } /** * {@inheritdoc} * * @see ImageInterface::modify() */ public function modify(ModifierInterface $modifier): ImageInterface { return $this->driver->specialize($modifier)->apply($this); } /** * {@inheritdoc} * * @see ImageInterface::analyze() */ public function analyze(AnalyzerInterface $analyzer): mixed { return $this->driver->specialize($analyzer)->analyze($this); } /** * {@inheritdoc} * * @see ImageInterface::encode() */ public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedImageInterface { return $this->driver->specialize($encoder)->encode($this); } /** * {@inheritdoc} * * @see ImageInterface::save() */ public function save(?string $path = null, mixed ...$options): ImageInterface { $path = is_null($path) ? $this->origin()->filePath() : $path; if (is_null($path)) { throw new EncoderException('Could not determine file path to save.'); } try { // try to determine encoding format by file extension of the path $encoded = $this->encodeByPath($path, ...$options); } catch (EncoderException) { // fallback to encoding format by media type $encoded = $this->encodeByMediaType(null, ...$options); } $encoded->save($path); return $this; } /** * {@inheritdoc} * * @see ImageInterface::width() */ public function width(): int { return $this->analyze(new WidthAnalyzer()); } /** * {@inheritdoc} * * @see ImageInterface::height() */ public function height(): int { return $this->analyze(new HeightAnalyzer()); } /** * {@inheritdoc} * * @see ImageInterface::size() */ public function size(): SizeInterface { return new Rectangle($this->width(), $this->height()); } /** * {@inheritdoc} * * @see ImageInterface::colorspace() */ public function colorspace(): ColorspaceInterface { return $this->analyze(new ColorspaceAnalyzer()); } /** * {@inheritdoc} * * @see ImageInterface::setColorspace() */ public function setColorspace(string|ColorspaceInterface $colorspace): ImageInterface { return $this->modify(new ColorspaceModifier($colorspace)); } /** * {@inheritdoc} * * @see ImageInterface::resolution() */ public function resolution(): ResolutionInterface { return $this->analyze(new ResolutionAnalyzer()); } /** * {@inheritdoc} * * @see ImageInterface::setResolution() */ public function setResolution(float $x, float $y): ImageInterface { return $this->modify(new ResolutionModifier($x, $y)); } /** * {@inheritdoc} * * @see ImageInterface::pickColor() */ public function pickColor(int $x, int $y, int $frame_key = 0): ColorInterface { return $this->analyze(new PixelColorAnalyzer($x, $y, $frame_key)); } /** * {@inheritdoc} * * @see ImageInterface::pickColors() */ public function pickColors(int $x, int $y): CollectionInterface { return $this->analyze(new PixelColorsAnalyzer($x, $y)); } /** * {@inheritdoc} * * @see ImageInterface::blendingColor() */ public function blendingColor(): ColorInterface { return $this->driver()->handleInput( $this->driver()->config()->blendingColor ); } /** * {@inheritdoc} * * @see ImageInterface::setBlendingColor() */ public function setBlendingColor(mixed $color): ImageInterface { $this->driver()->config()->setOptions( blendingColor: $this->driver()->handleInput($color) ); return $this; } /** * {@inheritdoc} * * @see ImageInterface::blendTransparency() */ public function blendTransparency(mixed $color = null): ImageInterface { return $this->modify(new BlendTransparencyModifier($color)); } /** * {@inheritdoc} * * @see ImageInterface::profile() */ public function profile(): ProfileInterface { return $this->analyze(new ProfileAnalyzer()); } /** * {@inheritdoc} * * @see ImageInterface::setProfile() */ public function setProfile(ProfileInterface $profile): ImageInterface { return $this->modify(new ProfileModifier($profile)); } /** * {@inheritdoc} * * @see ImageInterface::removeProfile() */ public function removeProfile(): ImageInterface { return $this->modify(new ProfileRemovalModifier()); } /** * {@inheritdoc} * * @see ImageInterface::reduceColors() */ public function reduceColors(int $limit, mixed $background = 'transparent'): ImageInterface { return $this->modify(new QuantizeColorsModifier($limit, $background)); } /** * {@inheritdoc} * * @see ImageInterface::sharpen() */ public function sharpen(int $amount = 10): ImageInterface { return $this->modify(new SharpenModifier($amount)); } /** * {@inheritdoc} * * @see ImageInterface::invert() */ public function invert(): ImageInterface { return $this->modify(new InvertModifier()); } /** * {@inheritdoc} * * @see ImageInterface::pixelate() */ public function pixelate(int $size): ImageInterface { return $this->modify(new PixelateModifier($size)); } /** * {@inheritdoc} * * @see ImageInterface::greyscale() */ public function greyscale(): ImageInterface { return $this->modify(new GreyscaleModifier()); } /** * {@inheritdoc} * * @see ImageInterface::brightness() */ public function brightness(int $level): ImageInterface { return $this->modify(new BrightnessModifier($level)); } /** * {@inheritdoc} * * @see ImageInterface::contrast() */ public function contrast(int $level): ImageInterface { return $this->modify(new ContrastModifier($level)); } /** * {@inheritdoc} * * @see ImageInterface::gamma() */ public function gamma(float $gamma): ImageInterface { return $this->modify(new GammaModifier($gamma)); } /** * {@inheritdoc} * * @see ImageInterface::colorize() */ public function colorize(int $red = 0, int $green = 0, int $blue = 0): ImageInterface { return $this->modify(new ColorizeModifier($red, $green, $blue)); } /** * {@inheritdoc} * * @see ImageInterface::flip() */ public function flip(): ImageInterface { return $this->modify(new FlipModifier()); } /** * {@inheritdoc} * * @see ImageInterface::flop() */ public function flop(): ImageInterface { return $this->modify(new FlopModifier()); } /** * {@inheritdoc} * * @see ImageInterface::blur() */ public function blur(int $amount = 5): ImageInterface { return $this->modify(new BlurModifier($amount)); } /** * {@inheritdoc} * * @see ImageInterface::rotate() */ public function rotate(float $angle, mixed $background = 'ffffff'): ImageInterface { return $this->modify(new RotateModifier($angle, $background)); } /** * {@inheritdoc} * * @see ImageInterface::orient() */ public function orient(): ImageInterface { return $this->modify(new AlignRotationModifier()); } /** * {@inheritdoc} * * @see ImageInterface::text() */ public function text(string $text, int $x, int $y, callable|Closure|FontInterface $font): ImageInterface { return $this->modify( new TextModifier( $text, new Point($x, $y), call_user_func(new FontFactory($font)), ), ); } /** * {@inheritdoc} * * @see ImageInterface::resize() */ public function resize(?int $width = null, ?int $height = null): ImageInterface { return $this->modify(new ResizeModifier($width, $height)); } /** * {@inheritdoc} * * @see ImageInterface::resizeDown() */ public function resizeDown(?int $width = null, ?int $height = null): ImageInterface { return $this->modify(new ResizeDownModifier($width, $height)); } /** * {@inheritdoc} * * @see ImageInterface::scale() */ public function scale(?int $width = null, ?int $height = null): ImageInterface { return $this->modify(new ScaleModifier($width, $height)); } /** * {@inheritdoc} * * @see ImageInterface::scaleDown() */ public function scaleDown(?int $width = null, ?int $height = null): ImageInterface { return $this->modify(new ScaleDownModifier($width, $height)); } /** * {@inheritdoc} * * @see ImageInterface::cover() */ public function cover(int $width, int $height, string $position = 'center'): ImageInterface { return $this->modify(new CoverModifier($width, $height, $position)); } /** * {@inheritdoc} * * @see ImageInterface::coverDown() */ public function coverDown(int $width, int $height, string $position = 'center'): ImageInterface { return $this->modify(new CoverDownModifier($width, $height, $position)); } /** * {@inheritdoc} * * @see ImageInterface::resizeCanvas() */ public function resizeCanvas( ?int $width = null, ?int $height = null, mixed $background = 'ffffff', string $position = 'center' ): ImageInterface { return $this->modify(new ResizeCanvasModifier($width, $height, $background, $position)); } /** * {@inheritdoc} * * @see ImageInterface::resizeCanvasRelative() */ public function resizeCanvasRelative( ?int $width = null, ?int $height = null, mixed $background = 'ffffff', string $position = 'center' ): ImageInterface { return $this->modify(new ResizeCanvasRelativeModifier($width, $height, $background, $position)); } /** * {@inheritdoc} * * @see ImageInterface::padDown() */ public function pad( int $width, int $height, mixed $background = 'ffffff', string $position = 'center' ): ImageInterface { return $this->modify(new PadModifier($width, $height, $background, $position)); } /** * {@inheritdoc} * * @see ImageInterface::pad() */ public function contain( int $width, int $height, mixed $background = 'ffffff', string $position = 'center' ): ImageInterface { return $this->modify(new ContainModifier($width, $height, $background, $position)); } /** * {@inheritdoc} * * @see ImageInterface::crop() */ public function crop( int $width, int $height, int $offset_x = 0, int $offset_y = 0, mixed $background = 'ffffff', string $position = 'top-left' ): ImageInterface { return $this->modify(new CropModifier($width, $height, $offset_x, $offset_y, $background, $position)); } /** * {@inheritdoc} * * @see ImageInterface::trim() */ public function trim(int $tolerance = 0): ImageInterface { return $this->modify(new TrimModifier($tolerance)); } /** * {@inheritdoc} * * @see ImageInterface::place() */ public function place( mixed $element, string $position = 'top-left', int $offset_x = 0, int $offset_y = 0, int $opacity = 100 ): ImageInterface { return $this->modify(new PlaceModifier($element, $position, $offset_x, $offset_y, $opacity)); } /** * {@inheritdoc} * * @see ImageInterface::fill() */ public function fill(mixed $color, ?int $x = null, ?int $y = null): ImageInterface { return $this->modify( new FillModifier( $color, is_null($x) || is_null($y) ? null : new Point($x, $y), ), ); } /** * {@inheritdoc} * * @see ImageInterface::drawPixel() */ public function drawPixel(int $x, int $y, mixed $color): ImageInterface { return $this->modify(new DrawPixelModifier(new Point($x, $y), $color)); } /** * {@inheritdoc} * * @see ImageInterface::drawRectangle() */ public function drawRectangle(int $x, int $y, callable|Closure|Rectangle $init): ImageInterface { return $this->modify( new DrawRectangleModifier( call_user_func(new RectangleFactory(new Point($x, $y), $init)), ), ); } /** * {@inheritdoc} * * @see ImageInterface::drawEllipse() */ public function drawEllipse(int $x, int $y, callable|Closure|Ellipse $init): ImageInterface { return $this->modify( new DrawEllipseModifier( call_user_func(new EllipseFactory(new Point($x, $y), $init)), ), ); } /** * {@inheritdoc} * * @see ImageInterface::drawCircle() */ public function drawCircle(int $x, int $y, callable|Closure|Circle $init): ImageInterface { return $this->modify( new DrawEllipseModifier( call_user_func(new CircleFactory(new Point($x, $y), $init)), ), ); } /** * {@inheritdoc} * * @see ImageInterface::drawPolygon() */ public function drawPolygon(callable|Closure|Polygon $init): ImageInterface { return $this->modify( new DrawPolygonModifier( call_user_func(new PolygonFactory($init)), ), ); } /** * {@inheritdoc} * * @see ImageInterface::drawLine() */ public function drawLine(callable|Closure|Line $init): ImageInterface { return $this->modify( new DrawLineModifier( call_user_func(new LineFactory($init)), ), ); } /** * {@inheritdoc} * * @see ImageInterface::drawBezier() */ public function drawBezier(callable|Closure|Bezier $init): ImageInterface { return $this->modify( new DrawBezierModifier( call_user_func(new BezierFactory($init)), ), ); } /** * {@inheritdoc} * * @see ImageInterface::encodeByMediaType() */ public function encodeByMediaType(null|string|MediaType $type = null, mixed ...$options): EncodedImageInterface { return $this->encode(new MediaTypeEncoder($type, ...$options)); } /** * {@inheritdoc} * * @see ImageInterface::encodeByExtension() */ public function encodeByExtension( null|string|FileExtension $extension = null, mixed ...$options ): EncodedImageInterface { return $this->encode(new FileExtensionEncoder($extension, ...$options)); } /** * {@inheritdoc} * * @see ImageInterface::encodeByPath() */ public function encodeByPath(?string $path = null, mixed ...$options): EncodedImageInterface { return $this->encode(new FilePathEncoder($path, ...$options)); } /** * {@inheritdoc} * * @see ImageInterface::toJpeg() */ public function toJpeg(mixed ...$options): EncodedImageInterface { return $this->encode(new JpegEncoder(...$options)); } /** * Alias of self::toJpeg() * * @throws RuntimeException */ public function toJpg(mixed ...$options): EncodedImageInterface { return $this->toJpeg(...$options); } /** * {@inheritdoc} * * @see ImageInterface::toJpeg() */ public function toJpeg2000(mixed ...$options): EncodedImageInterface { return $this->encode(new Jpeg2000Encoder(...$options)); } /** * ALias of self::toJpeg2000() * * @throws RuntimeException */ public function toJp2(mixed ...$options): EncodedImageInterface { return $this->toJpeg2000(...$options); } /** * {@inheritdoc} * * @see ImageInterface::toPng() */ public function toPng(mixed ...$options): EncodedImageInterface { return $this->encode(new PngEncoder(...$options)); } /** * {@inheritdoc} * * @see ImageInterface::toGif() */ public function toGif(mixed ...$options): EncodedImageInterface { return $this->encode(new GifEncoder(...$options)); } /** * {@inheritdoc} * * @see ImageInterface::toWebp() */ public function toWebp(mixed ...$options): EncodedImageInterface { return $this->encode(new WebpEncoder(...$options)); } /** * {@inheritdoc} * * @see ImageInterface::toBitmap() */ public function toBitmap(mixed ...$options): EncodedImageInterface { return $this->encode(new BmpEncoder(...$options)); } /** * Alias if self::toBitmap() * * @throws RuntimeException */ public function toBmp(mixed ...$options): EncodedImageInterface { return $this->toBitmap(...$options); } /** * {@inheritdoc} * * @see ImageInterface::toAvif() */ public function toAvif(mixed ...$options): EncodedImageInterface { return $this->encode(new AvifEncoder(...$options)); } /** * {@inheritdoc} * * @see ImageInterface::toTiff() */ public function toTiff(mixed ...$options): EncodedImageInterface { return $this->encode(new TiffEncoder(...$options)); } /** * Alias of self::toTiff() * * @throws RuntimeException */ public function toTif(mixed ...$options): EncodedImageInterface { return $this->toTiff(...$options); } /** * {@inheritdoc} * * @see ImageInterface::toHeic() */ public function toHeic(mixed ...$options): EncodedImageInterface { return $this->encode(new HeicEncoder(...$options)); } /** * Show debug info for the current image * * @return array */ public function __debugInfo(): array { try { return [ 'width' => $this->width(), 'height' => $this->height(), ]; } catch (RuntimeException) { return []; } } /** * Clone image */ public function __clone(): void { $this->driver = clone $this->driver; $this->core = clone $this->core; $this->exif = clone $this->exif; } } image/src/Format.php000064400000011036152665633640010377 0ustar00format(); } 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 NotSupportedException('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 (NotSupportedException) { return null; } } /** * Return the possible media (MIME) types for the current format * * @return array */ 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 */ public function mediaType(): MediaType { $types = $this->mediaTypes(); return reset($types); } /** * Return the possible file extension for the current format * * @return array */ 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 */ public function fileExtension(): FileExtension { $extensions = $this->fileExtensions(); return reset($extensions); } /** * 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::JP2 => Jpeg2000Encoder::class, self::JPEG => JpegEncoder::class, self::PNG => PngEncoder::class, self::TIFF => TiffEncoder::class, self::WEBP => WebpEncoder::class, }; // get parameters of target encoder $parameters = []; $reflectionClass = new ReflectionClass($classname); if ($constructor = $reflectionClass->getConstructor()) { $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); } } image/src/MediaType.php000064400000010302152665633640011023 0ustar00mediaType(); } if ($identifier instanceof FileExtension) { return $identifier->mediaType(); } try { $type = self::from(strtolower($identifier)); } catch (Error) { try { $type = FileExtension::from(strtolower($identifier))->mediaType(); } catch (Error) { throw new NotSupportedException('Unable to create media type from "' . $identifier . '".'); } } return $type; } /** * Try to create media type from given identifier and return null on failure * * @param string|Format|MediaType|FileExtension $identifier * @return MediaType|null */ public static function tryCreate(string|self|Format|FileExtension $identifier): ?self { try { return self::create($identifier); } catch (NotSupportedException) { return null; } } /** * Return the matching format for the current media (MIME) type */ public function format(): Format { return match ($this) { self::IMAGE_JPEG, self::IMAGE_JPG, self::IMAGE_PJPEG, self::IMAGE_X_JPEG => Format::JPEG, self::IMAGE_WEBP, self::IMAGE_X_WEBP => Format::WEBP, self::IMAGE_GIF => Format::GIF, self::IMAGE_PNG, self::IMAGE_X_PNG => Format::PNG, self::IMAGE_AVIF, self::IMAGE_X_AVIF => Format::AVIF, self::IMAGE_BMP, self::IMAGE_MS_BMP, self::IMAGE_X_BITMAP, self::IMAGE_X_BMP, self::IMAGE_X_MS_BMP, self::IMAGE_X_XBITMAP, self::IMAGE_X_WINDOWS_BMP, self::IMAGE_X_BMP3, self::IMAGE_X_WIN_BITMAP => Format::BMP, self::IMAGE_TIFF => Format::TIFF, self::IMAGE_JP2, self::IMAGE_JPX, self::IMAGE_X_JP2_CODESTREAM, self::IMAGE_JPM => Format::JP2, self::IMAGE_HEIF, self::IMAGE_HEIC, self::IMAGE_X_HEIC => Format::HEIC, }; } /** * Return the possible file extension for the current media type * * @return array */ public function fileExtensions(): array { return $this->format()->fileExtensions(); } /** * Return the first file extension for the current media type */ public function fileExtension(): FileExtension { return $this->format()->fileExtension(); } } image/src/Colors/Hsl/Channels/Luminance.php000064400000000741152665633640014605 0ustar00channels = [ new Hue($h), new Saturation($s), new Luminance($l), ]; } /** * {@inheritdoc} * * @see ColorInterface::colorspace() */ public function colorspace(): ColorspaceInterface { return new Colorspace(); } /** * {@inheritdoc} * * @see ColorInterface::create() */ public static function create(mixed $input): ColorInterface { return InputHandler::withDecoders([ Decoders\StringColorDecoder::class, ])->handle($input); } /** * Return the Hue channel */ public function hue(): ColorChannelInterface { /** @throws void */ return $this->channel(Hue::class); } /** * Return the Saturation channel */ public function saturation(): ColorChannelInterface { /** @throws void */ return $this->channel(Saturation::class); } /** * Return the Luminance channel */ public function luminance(): ColorChannelInterface { /** @throws void */ return $this->channel(Luminance::class); } public function toHex(string $prefix = ''): string { return $this->convertTo(RgbColorspace::class)->toHex($prefix); } /** * {@inheritdoc} * * @see ColorInterface::toString() */ public function toString(): string { return sprintf( 'hsl(%d, %d%%, %d%%)', $this->hue()->value(), $this->saturation()->value(), $this->luminance()->value() ); } /** * {@inheritdoc} * * @see ColorInterface::isGreyscale() */ public function isGreyscale(): bool { return $this->saturation()->value() == 0; } /** * {@inheritdoc} * * @see ColorInterface::isTransparent() */ public function isTransparent(): bool { return false; } /** * {@inheritdoc} * * @see ColorInterface::isClear() */ public function isClear(): bool { return false; } } image/src/Colors/Hsl/Decoders/StringColorDecoder.php000064400000002365152665633640016426 0ustar00[0-9\.]+), ?(?P[0-9\.]+%?), ?(?P[0-9\.]+%?)?\)$/i'; if (preg_match($pattern, $input, $matches) != 1) { throw new DecoderException('Unable to decode input'); } $values = array_map(function (string $value): int { return match (strpos($value, '%')) { false => intval(trim($value)), default => intval(trim(str_replace('%', '', $value))), }; }, [$matches['h'], $matches['s'], $matches['l']]); return new Color(...$values); } } image/src/Colors/Hsl/Colorspace.php000064400000007767152665633640013250 0ustar00 */ public static array $channels = [ Channels\Hue::class, Channels\Saturation::class, Channels\Luminance::class ]; /** * {@inheritdoc} * * @see ColorspaceInterface::colorFromNormalized() */ public function colorFromNormalized(array $normalized): ColorInterface { return new Color(...array_map( fn(string $classname, float $value_normalized) => (new $classname(normalized: $value_normalized))->value(), self::$channels, $normalized )); } /** * @throws ColorException */ public function importColor(ColorInterface $color): ColorInterface { return match ($color::class) { CmykColor::class => $this->importRgbColor($color->convertTo(RgbColorspace::class)), RgbColor::class => $this->importRgbColor($color), HsvColor::class => $this->importHsvColor($color), default => $color, }; } /** * @throws ColorException */ protected function importRgbColor(ColorInterface $color): ColorInterface { if (!($color instanceof RgbColor)) { throw new ColorException('Unabled to import color of type ' . $color::class . '.'); } // normalized values of rgb channels $values = array_map( fn(ColorChannelInterface $channel): float => $channel->normalize(), $color->channels(), ); // take only RGB $values = array_slice($values, 0, 3); // calculate Luminance $min = min(...$values); $max = max(...$values); $luminance = ($max + $min) / 2; $delta = $max - $min; // calculate saturation $saturation = match (true) { $delta == 0 => 0, default => $delta / (1 - abs(2 * $luminance - 1)), }; // calculate hue [$r, $g, $b] = $values; $hue = match (true) { ($delta == 0) => 0, ($max == $r) => 60 * fmod((($g - $b) / $delta), 6), ($max == $g) => 60 * ((($b - $r) / $delta) + 2), ($max == $b) => 60 * ((($r - $g) / $delta) + 4), default => 0, }; $hue = ($hue + 360) % 360; // normalize hue return new Color( intval(round($hue)), intval(round($saturation * 100)), intval(round($luminance * 100)), ); } /** * @throws ColorException */ protected function importHsvColor(ColorInterface $color): ColorInterface { if (!($color instanceof HsvColor)) { throw new ColorException('Unabled to import color of type ' . $color::class . '.'); } // normalized values of hsv channels [$h, $s, $v] = array_map( fn(ColorChannelInterface $channel): float => $channel->normalize(), $color->channels(), ); // calculate Luminance $luminance = (2 - $s) * $v / 2; // calculate Saturation $saturation = match (true) { $luminance == 0 => $s, $luminance == 1 => 0, $luminance < .5 => $s * $v / ($luminance * 2), default => $s * $v / (2 - $luminance * 2), }; return new Color( intval(round($h * 360)), intval(round($saturation * 100)), intval(round($luminance * 100)), ); } } image/src/Colors/Profile.php000064400000000745152665633640012015 0ustar00 */ protected array $channels; /** * {@inheritdoc} * * @see ColorInterface::channels() */ public function channels(): array { return $this->channels; } /** * {@inheritdoc} * * @see ColorInterface::channel() */ public function channel(string $classname): ColorChannelInterface { $channels = array_filter( $this->channels(), fn(ColorChannelInterface $channel): bool => $channel::class === $classname, ); if (count($channels) == 0) { throw new ColorException('Color channel ' . $classname . ' could not be found.'); } return reset($channels); } /** * {@inheritdoc} * * @see ColorInterface::normalize() */ public function normalize(): array { return array_map( fn(ColorChannelInterface $channel): float => $channel->normalize(), $this->channels(), ); } /** * {@inheritdoc} * * @see ColorInterface::toArray() */ public function toArray(): array { return array_map( fn(ColorChannelInterface $channel): int => $channel->value(), $this->channels() ); } /** * {@inheritdoc} * * @see ColorInterface::convertTo() */ public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface { $colorspace = match (true) { is_object($colorspace) => $colorspace, default => new $colorspace(), }; return $colorspace->importColor($this); } /** * Show debug info for the current color * * @return array */ public function __debugInfo(): array { return array_reduce($this->channels(), function (array $result, ColorChannelInterface $item) { $key = strtolower((new ReflectionClass($item))->getShortName()); $result[$key] = $item->value(); return $result; }, []); } /** * {@inheritdoc} * * @see ColorInterface::__toString() */ public function __toString(): string { return $this->toString(); } } image/src/Colors/Rgb/Channels/Alpha.php000064400000000462152665633640013703 0ustar00normalize(), 6)); } } image/src/Colors/Rgb/Channels/Red.php000064400000000733152665633640013371 0ustar00channels = [ new Red($r), new Green($g), new Blue($b), new Alpha($a), ]; } /** * {@inheritdoc} * * @see ColorInterface::colorspace() */ public function colorspace(): ColorspaceInterface { return new Colorspace(); } /** * {@inheritdoc} * * @see ColorInterface::create() */ public static function create(mixed $input): ColorInterface { return InputHandler::withDecoders([ Decoders\HexColorDecoder::class, Decoders\StringColorDecoder::class, Decoders\TransparentColorDecoder::class, Decoders\HtmlColornameDecoder::class, ])->handle($input); } /** * Return the RGB red color channel */ public function red(): ColorChannelInterface { /** @throws void */ return $this->channel(Red::class); } /** * Return the RGB green color channel */ public function green(): ColorChannelInterface { /** @throws void */ return $this->channel(Green::class); } /** * Return the RGB blue color channel */ public function blue(): ColorChannelInterface { /** @throws void */ return $this->channel(Blue::class); } /** * Return the colors alpha channel */ public function alpha(): ColorChannelInterface { /** @throws void */ return $this->channel(Alpha::class); } /** * {@inheritdoc} * * @see ColorInterface::toHex() */ public function toHex(string $prefix = ''): string { if ($this->isTransparent()) { return sprintf( '%s%02x%02x%02x%02x', $prefix, $this->red()->value(), $this->green()->value(), $this->blue()->value(), $this->alpha()->value() ); } return sprintf( '%s%02x%02x%02x', $prefix, $this->red()->value(), $this->green()->value(), $this->blue()->value() ); } /** * {@inheritdoc} * * @see ColorInterface::toString() */ public function toString(): string { if ($this->isTransparent()) { return sprintf( 'rgba(%d, %d, %d, %.1F)', $this->red()->value(), $this->green()->value(), $this->blue()->value(), $this->alpha()->normalize(), ); } return sprintf( 'rgb(%d, %d, %d)', $this->red()->value(), $this->green()->value(), $this->blue()->value() ); } /** * {@inheritdoc} * * @see ColorInterface::isGreyscale() */ public function isGreyscale(): bool { $values = [$this->red()->value(), $this->green()->value(), $this->blue()->value()]; return count(array_unique($values, SORT_REGULAR)) === 1; } /** * {@inheritdoc} * * @see ColorInterface::isTransparent() */ public function isTransparent(): bool { return $this->alpha()->value() < $this->alpha()->max(); } /** * {@inheritdoc} * * @see ColorInterface::isClear() */ public function isClear(): bool { return $this->alpha()->value() == 0; } } image/src/Colors/Rgb/Decoders/TransparentColorDecoder.php000064400000001352152665633640017440 0ustar00 */ protected static array $names = [ 'lightsalmon' => '#ffa07a', 'salmon' => '#fa8072', 'darksalmon' => '#e9967a', 'lightcoral' => '#f08080', 'indianred' => '#cd5c5c', 'crimson' => '#dc143c', 'firebrick' => '#b22222', 'red' => '#ff0000', 'darkred' => '#8b0000', 'coral' => '#ff7f50', 'tomato' => '#ff6347', 'orangered' => '#ff4500', 'gold' => '#ffd700', 'orange' => '#ffa500', 'darkorange' => '#ff8c00', 'lightyellow' => '#ffffe0', 'lemonchiffon' => '#fffacd', 'lightgoldenrodyellow' => '#fafad2', 'papayawhip' => '#ffefd5', 'moccasin' => '#ffe4b5', 'peachpuff' => '#ffdab9', 'palegoldenrod' => '#eee8aa', 'khaki' => '#f0e68c', 'darkkhaki' => '#bdb76b', 'yellow' => '#ffff00', 'lawngreen' => '#7cfc00', 'chartreuse' => '#7fff00', 'limegreen' => '#32cd32', 'lime' => '#00ff00', 'forestgreen' => '#228b22', 'green' => '#008000', 'darkgreen' => '#006400', 'greenyellow' => '#adff2f', 'yellowgreen' => '#9acd32', 'springgreen' => '#00ff7f', 'mediumspringgreen' => '#00fa9a', 'lightgreen' => '#90ee90', 'palegreen' => '#98fb98', 'darkseagreen' => '#8fbc8f', 'mediumseagre' => 'en #3cb371', 'seagreen' => '#2e8b57', 'olive' => '#808000', 'darkolivegreen' => '#556b2f', 'olivedrab' => '#6b8e23', 'lightcyan' => '#e0ffff', 'cyan' => '#00ffff', 'aqua' => '#00ffff', 'aquamarine' => '#7fffd4', 'mediumaquamarine' => '#66cdaa', 'paleturquoise' => '#afeeee', 'turquoise' => '#40e0d0', 'mediumturquoise' => '#48d1cc', 'darkturquoise' => '#00ced1', 'lightseagreen' => '#20b2aa', 'cadetblue' => '#5f9ea0', 'darkcyan' => '#008b8b', 'teal' => '#008080', 'powderblue' => '#b0e0e6', 'lightblue' => '#add8e6', 'lightskyblue' => '#87cefa', 'skyblue' => '#87ceeb', 'deepskyblue' => '#00bfff', 'lightsteelblue' => '#b0c4de', 'dodgerblue' => '#1e90ff', 'cornflowerblue' => '#6495ed', 'steelblue' => '#4682b4', 'royalblue' => '#4169e1', 'blue' => '#0000ff', 'mediumblue' => '#0000cd', 'darkblue' => '#00008b', 'navy' => '#000080', 'midnightblue' => '#191970', 'mediumslateblue' => '#7b68ee', 'slateblue' => '#6a5acd', 'darkslateblue' => '#483d8b', 'lavender' => '#e6e6fa', 'thistle' => '#d8bfd8', 'plum' => '#dda0dd', 'violet' => '#ee82ee', 'orchid' => '#da70d6', 'fuchsia' => '#ff00ff', 'magenta' => '#ff00ff', 'mediumorchid' => '#ba55d3', 'mediumpurple' => '#9370db', 'blueviolet' => '#8a2be2', 'darkviolet' => '#9400d3', 'darkorchid' => '#9932cc', 'darkmagenta' => '#8b008b', 'purple' => '#800080', 'indigo' => '#4b0082', 'pink' => '#ffc0cb', 'lightpink' => '#ffb6c1', 'hotpink' => '#ff69b4', 'deeppink' => '#ff1493', 'palevioletred' => '#db7093', 'mediumvioletred' => '#c71585', 'white' => '#ffffff', 'snow' => '#fffafa', 'honeydew' => '#f0fff0', 'mintcream' => '#f5fffa', 'azure' => '#f0ffff', 'aliceblue' => '#f0f8ff', 'ghostwhite' => '#f8f8ff', 'whitesmoke' => '#f5f5f5', 'seashell' => '#fff5ee', 'beige' => '#f5f5dc', 'oldlace' => '#fdf5e6', 'floralwhite' => '#fffaf0', 'ivory' => '#fffff0', 'antiquewhite' => '#faebd7', 'linen' => '#faf0e6', 'lavenderblush' => '#fff0f5', 'mistyrose' => '#ffe4e1', 'gainsboro' => '#dcdcdc', 'lightgray' => '#d3d3d3', 'silver' => '#c0c0c0', 'darkgray' => '#a9a9a9', 'gray' => '#808080', 'dimgray' => '#696969', 'lightslategray' => '#778899', 'slategray' => '#708090', 'darkslategray' => '#2f4f4f', 'black' => '#000000', 'cornsilk' => '#fff8dc', 'blanchedalmond' => '#ffebcd', 'bisque' => '#ffe4c4', 'navajowhite' => '#ffdead', 'wheat' => '#f5deb3', 'burlywood' => '#deb887', 'tan' => '#d2b48c', 'rosybrown' => '#bc8f8f', 'sandybrown' => '#f4a460', 'goldenrod' => '#daa520', 'peru' => '#cd853f', 'chocolate' => '#d2691e', 'saddlebrown' => '#8b4513', 'sienna' => '#a0522d', 'brown' => '#a52a2a', 'maroon' => '#800000', ]; /** * Decode html color names */ public function decode(mixed $input): ImageInterface|ColorInterface { if (!is_string($input)) { throw new DecoderException('Unable to decode input'); } if (!array_key_exists(strtolower($input), static::$names)) { throw new DecoderException('Unable to decode input'); } return parent::decode(static::$names[strtolower($input)]); } } image/src/Colors/Rgb/Decoders/HexColorDecoder.php000064400000003021152665633640015656 0ustar00[a-f\d]{3}(?:[a-f\d]?|(?:[a-f\d]{3}(?:[a-f\d]{2})?)?)\b)$/i'; if (preg_match($pattern, $input, $matches) != 1) { throw new DecoderException('Unable to decode input'); } $values = match (strlen($matches['hex'])) { 3, 4 => str_split($matches['hex']), 6, 8 => str_split($matches['hex'], 2), default => throw new DecoderException('Unable to decode input'), }; $values = array_map(function (string $value): float|int { return match (strlen($value)) { 1 => hexdec($value . $value), 2 => hexdec($value), default => throw new DecoderException('Unable to decode input'), }; }, $values); return new Color(...$values); } } image/src/Colors/Rgb/Decoders/StringColorDecoder.php000064400000003302152665633640016402 0ustar00[0-9\.]+%?), ?(?P[0-9\.]+%?), ?(?P[0-9\.]+%?)' . '(?:, ?(?P(?:1)|(?:1\.0*)|(?:0)|(?:0?\.\d+%?)|(?:\d{1,3}%)))?\)$/i'; if (preg_match($pattern, $input, $matches) != 1) { throw new DecoderException('Unable to decode input'); } // rgb values $values = array_map(function (string $value): int { return match (strpos($value, '%')) { false => intval(trim($value)), default => intval(round(floatval(trim(str_replace('%', '', $value))) / 100 * 255)), }; }, [$matches['r'], $matches['g'], $matches['b']]); // alpha value if (array_key_exists('a', $matches)) { $values[] = match (true) { strpos($matches['a'], '%') => round(intval(trim(str_replace('%', '', $matches['a']))) / 2.55), default => intval(round(floatval(trim($matches['a'])) * 255)), }; } return new Color(...$values); } } image/src/Colors/Rgb/Colorspace.php000064400000010324152665633640013213 0ustar00 */ public static array $channels = [ Channels\Red::class, Channels\Green::class, Channels\Blue::class, Channels\Alpha::class ]; /** * {@inheritdoc} * * @see ColorspaceInterface::colorFromNormalized() */ public function colorFromNormalized(array $normalized): ColorInterface { return new Color(...array_map( fn($classname, float $value_normalized) => (new $classname(normalized: $value_normalized))->value(), self::$channels, $normalized, )); } /** * @throws ColorException */ public function importColor(ColorInterface $color): ColorInterface { return match ($color::class) { CmykColor::class => $this->importCmykColor($color), HsvColor::class => $this->importHsvColor($color), HslColor::class => $this->importHslColor($color), default => $color, }; } /** * @throws ColorException */ protected function importCmykColor(ColorInterface $color): ColorInterface { if (!($color instanceof CmykColor)) { throw new ColorException('Unabled to import color of type ' . $color::class . '.'); } return new Color( (int) (255 * (1 - $color->cyan()->normalize()) * (1 - $color->key()->normalize())), (int) (255 * (1 - $color->magenta()->normalize()) * (1 - $color->key()->normalize())), (int) (255 * (1 - $color->yellow()->normalize()) * (1 - $color->key()->normalize())), ); } /** * @throws ColorException */ protected function importHsvColor(ColorInterface $color): ColorInterface { if (!($color instanceof HsvColor)) { throw new ColorException('Unabled to import color of type ' . $color::class . '.'); } $chroma = $color->value()->normalize() * $color->saturation()->normalize(); $hue = $color->hue()->normalize() * 6; $x = $chroma * (1 - abs(fmod($hue, 2) - 1)); // connect channel values $values = match (true) { $hue < 1 => [$chroma, $x, 0], $hue < 2 => [$x, $chroma, 0], $hue < 3 => [0, $chroma, $x], $hue < 4 => [0, $x, $chroma], $hue < 5 => [$x, 0, $chroma], default => [$chroma, 0, $x], }; // add to each value $values = array_map(fn(float|int $value): float => $value + $color->value()->normalize() - $chroma, $values); $values[] = 1; // append alpha channel value return $this->colorFromNormalized($values); } /** * @throws ColorException */ protected function importHslColor(ColorInterface $color): ColorInterface { if (!($color instanceof HslColor)) { throw new ColorException('Unabled to import color of type ' . $color::class . '.'); } // normalized values of hsl channels [$h, $s, $l] = array_map( fn(ColorChannelInterface $channel): float => $channel->normalize(), $color->channels() ); $c = (1 - abs(2 * $l - 1)) * $s; $x = $c * (1 - abs(fmod($h * 6, 2) - 1)); $m = $l - $c / 2; $values = match (true) { $h < 1 / 6 => [$c, $x, 0], $h < 2 / 6 => [$x, $c, 0], $h < 3 / 6 => [0, $c, $x], $h < 4 / 6 => [0, $x, $c], $h < 5 / 6 => [$x, 0, $c], default => [$c, 0, $x], }; $values = array_map(fn(float|int $value): float => $value + $m, $values); $values[] = 1; // append alpha channel value return $this->colorFromNormalized($values); } } image/src/Colors/AbstractColorChannel.php000064400000004136152665633640014446 0ustar00value = $this->validate( match (true) { is_null($value) && is_numeric($normalized) => intval(round($normalized * $this->max())), is_numeric($value) && is_null($normalized) => $value, default => throw new ColorException('Color channels must either have a value or a normalized value') } ); } /** * Alias of value() */ public function toInt(): int { return $this->value; } /** * {@inheritdoc} * * @see ColorChannelInterface::value() */ public function value(): int { return $this->value; } /** * {@inheritdoc} * * @see ColorChannelInterface::normalize() */ public function normalize(int $precision = 32): float { return round(($this->value() - $this->min()) / ($this->max() - $this->min()), $precision); } /** * {@inheritdoc} * * @see ColorChannelInterface::validate() */ public function validate(mixed $value): mixed { if ($value < $this->min() || $value > $this->max()) { throw new ColorException('Color channel value must be in range ' . $this->min() . ' to ' . $this->max()); } return $value; } /** * {@inheritdoc} * * @see ColorChannelInterface::toString() */ public function toString(): string { return (string) $this->value(); } /** * {@inheritdoc} * * @see ColorChannelInterface::__toString() */ public function __toString(): string { return $this->toString(); } } image/src/Colors/Hsv/Channels/Value.php000064400000000735152665633640013763 0ustar00channels = [ new Hue($h), new Saturation($s), new Value($v), ]; } /** * {@inheritdoc} * * @see ColorInterface::colorspace() */ public function colorspace(): ColorspaceInterface { return new Colorspace(); } /** * {@inheritdoc} * * @see ColorInterface::create() */ public static function create(mixed $input): ColorInterface { return InputHandler::withDecoders([ Decoders\StringColorDecoder::class, ])->handle($input); } /** * Return the Hue channel */ public function hue(): ColorChannelInterface { /** @throws void */ return $this->channel(Hue::class); } /** * Return the Saturation channel */ public function saturation(): ColorChannelInterface { /** @throws void */ return $this->channel(Saturation::class); } /** * Return the Value channel */ public function value(): ColorChannelInterface { /** @throws void */ return $this->channel(Value::class); } public function toHex(string $prefix = ''): string { return $this->convertTo(RgbColorspace::class)->toHex($prefix); } /** * {@inheritdoc} * * @see ColorInterface::toString() */ public function toString(): string { return sprintf( 'hsv(%d, %d%%, %d%%)', $this->hue()->value(), $this->saturation()->value(), $this->value()->value() ); } /** * {@inheritdoc} * * @see ColorInterface::isGreyscale() */ public function isGreyscale(): bool { return $this->saturation()->value() == 0; } /** * {@inheritdoc} * * @see ColorInterface::isTransparent() */ public function isTransparent(): bool { return false; } /** * {@inheritdoc} * * @see ColorInterface::isClear() */ public function isClear(): bool { return false; } } image/src/Colors/Hsv/Decoders/StringColorDecoder.php000064400000002375152665633640016441 0ustar00[0-9\.]+), ?(?P[0-9\.]+%?), ?(?P[0-9\.]+%?)?\)$/i'; if (preg_match($pattern, $input, $matches) != 1) { throw new DecoderException('Unable to decode input'); } $values = array_map(function (string $value): int { return match (strpos($value, '%')) { false => intval(trim($value)), default => intval(trim(str_replace('%', '', $value))), }; }, [$matches['h'], $matches['s'], $matches['v']]); return new Color(...$values); } } image/src/Colors/Hsv/Colorspace.php000064400000006746152665633640013256 0ustar00 */ public static array $channels = [ Channels\Hue::class, Channels\Saturation::class, Channels\Value::class ]; /** * {@inheritdoc} * * @see ColorspaceInterface::colorFromNormalized() */ public function colorFromNormalized(array $normalized): ColorInterface { return new Color(...array_map( fn(string $classname, float $value_normalized) => (new $classname(normalized: $value_normalized))->value(), self::$channels, $normalized )); } /** * @throws ColorException */ public function importColor(ColorInterface $color): ColorInterface { return match ($color::class) { CmykColor::class => $this->importRgbColor($color->convertTo(RgbColorspace::class)), RgbColor::class => $this->importRgbColor($color), HslColor::class => $this->importHslColor($color), default => $color, }; } /** * @throws ColorException */ protected function importRgbColor(ColorInterface $color): ColorInterface { if (!($color instanceof RgbColor)) { throw new ColorException('Unabled to import color of type ' . $color::class . '.'); } // normalized values of rgb channels $values = array_map(fn(ColorChannelInterface $channel): float => $channel->normalize(), $color->channels()); // take only RGB $values = array_slice($values, 0, 3); // calculate chroma $min = min(...$values); $max = max(...$values); $chroma = $max - $min; // calculate value $v = 100 * $max; if ($chroma == 0) { // greyscale color return new Color(0, 0, intval(round($v))); } // calculate saturation $s = 100 * ($chroma / $max); // calculate hue [$r, $g, $b] = $values; $h = match (true) { ($r == $min) => 3 - (($g - $b) / $chroma), ($b == $min) => 1 - (($r - $g) / $chroma), default => 5 - (($b - $r) / $chroma), } * 60; return new Color( intval(round($h)), intval(round($s)), intval(round($v)) ); } /** * @throws ColorException */ protected function importHslColor(ColorInterface $color): ColorInterface { if (!($color instanceof HslColor)) { throw new ColorException('Unabled to import color of type ' . $color::class . '.'); } // normalized values of hsl channels [$h, $s, $l] = array_map( fn(ColorChannelInterface $channel): float => $channel->normalize(), $color->channels() ); $v = $l + $s * min($l, 1 - $l); $s = ($v == 0) ? 0 : 2 * (1 - $l / $v); return $this->colorFromNormalized([$h, $s, $v]); } } image/src/Colors/Cmyk/Channels/Yellow.php000064400000000172152665633640014320 0ustar00channels = [ new Cyan($c), new Magenta($m), new Yellow($y), new Key($k), ]; } /** * {@inheritdoc} * * @see ColorInterface::create() */ public static function create(mixed $input): ColorInterface { return InputHandler::withDecoders([ Decoders\StringColorDecoder::class, ])->handle($input); } /** * {@inheritdoc} * * @see ColorInterface::colorspace() */ public function colorspace(): ColorspaceInterface { return new Colorspace(); } /** * {@inheritdoc} * * @see ColorInterface::toHex() */ public function toHex(string $prefix = ''): string { return $this->convertTo(RgbColorspace::class)->toHex($prefix); } /** * Return the CMYK cyan channel */ public function cyan(): ColorChannelInterface { /** @throws void */ return $this->channel(Cyan::class); } /** * Return the CMYK magenta channel */ public function magenta(): ColorChannelInterface { /** @throws void */ return $this->channel(Magenta::class); } /** * Return the CMYK yellow channel */ public function yellow(): ColorChannelInterface { /** @throws void */ return $this->channel(Yellow::class); } /** * Return the CMYK key channel */ public function key(): ColorChannelInterface { /** @throws void */ return $this->channel(Key::class); } /** * {@inheritdoc} * * @see ColorInterface::toString() */ public function toString(): string { return sprintf( 'cmyk(%d%%, %d%%, %d%%, %d%%)', $this->cyan()->value(), $this->magenta()->value(), $this->yellow()->value(), $this->key()->value() ); } /** * {@inheritdoc} * * @see ColorInterface::isGreyscale() */ public function isGreyscale(): bool { return 0 === array_sum([ $this->cyan()->value(), $this->magenta()->value(), $this->yellow()->value(), ]); } /** * {@inheritdoc} * * @see ColorInterface::isTransparent() */ public function isTransparent(): bool { return false; } /** * {@inheritdoc} * * @see ColorInterface::isClear() */ public function isClear(): bool { return false; } } image/src/Colors/Cmyk/Decoders/StringColorDecoder.php000064400000002267152665633650016605 0ustar00[0-9\.]+%?), ?(?P[0-9\.]+%?), ?(?P[0-9\.]+%?), ?(?P[0-9\.]+%?)\)$/i'; if (preg_match($pattern, $input, $matches) != 1) { throw new DecoderException('Unable to decode input'); } $values = array_map(function (string $value): int { return intval(round(floatval(trim(str_replace('%', '', $value))))); }, [$matches['c'], $matches['m'], $matches['y'], $matches['k']]); return new Color(...$values); } } image/src/Colors/Cmyk/Colorspace.php000064400000004463152665633650013414 0ustar00 */ public static array $channels = [ Channels\Cyan::class, Channels\Magenta::class, Channels\Yellow::class, Channels\Key::class ]; /** * {@inheritdoc} * * @see ColorspaceInterface::createColor() */ public function colorFromNormalized(array $normalized): ColorInterface { return new Color(...array_map( fn(string $classname, float $value_normalized) => (new $classname(normalized: $value_normalized))->value(), self::$channels, $normalized, )); } /** * @throws ColorException */ public function importColor(ColorInterface $color): ColorInterface { return match ($color::class) { RgbColor::class => $this->importRgbColor($color), HsvColor::class => $this->importRgbColor($color->convertTo(RgbColorspace::class)), HslColor::class => $this->importRgbColor($color->convertTo(RgbColorspace::class)), default => $color, }; } /** * @throws ColorException */ protected function importRgbColor(ColorInterface $color): CmykColor { if (!($color instanceof RgbColor)) { throw new ColorException('Unabled to import color of type ' . $color::class . '.'); } $c = (255 - $color->red()->value()) / 255.0 * 100; $m = (255 - $color->green()->value()) / 255.0 * 100; $y = (255 - $color->blue()->value()) / 255.0 * 100; $k = intval(round(min([$c, $m, $y]))); $c = intval(round($c - $k)); $m = intval(round($m - $k)); $y = intval(round($y - $k)); return new CmykColor($c, $m, $y, $k); } } image/src/Drivers/AbstractDriver.php000064400000007577152665633650013524 0ustar00config = new Config(); $this->checkHealth(); } /** * {@inheritdoc} * * @see DriverInterface::config() */ public function config(): Config { return $this->config; } /** * {@inheritdoc} * * @see DriverInterface::handleInput() */ public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface { return InputHandler::withDecoders($decoders, $this)->handle($input); } /** * {@inheritdoc} * * @see DriverInterface::specialize() */ public function specialize( ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface $object ): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface { // return object directly if no specializing is possible if (!($object instanceof SpecializableInterface)) { return $object; } // return directly and only attach driver if object is already specialized if ($object instanceof SpecializedInterface) { $object->setDriver($this); return $object; } // resolve classname for specializable object $specialized_classname = implode("\\", [ (new ReflectionClass($this))->getNamespaceName(), // driver's namespace match (true) { $object instanceof ModifierInterface => 'Modifiers', $object instanceof AnalyzerInterface => 'Analyzers', $object instanceof EncoderInterface => 'Encoders', $object instanceof DecoderInterface => 'Decoders', }, $object_shortname = (new ReflectionClass($object))->getShortName(), ]); // fail if driver specialized classname does not exists if (!class_exists($specialized_classname)) { throw new NotSupportedException( "Class '" . $object_shortname . "' is not supported by " . $this->id() . " driver." ); } // create a driver specialized object with the specializable properties of generic object $specialized = new $specialized_classname(...$object->specializable()); // attach driver return $specialized->setDriver($this); } /** * {@inheritdoc} * * @see DriverInterface::specializeMultiple() * * @throws NotSupportedException * @throws DriverException */ public function specializeMultiple(array $objects): array { return array_map( function (string|object $object): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface { return $this->specialize( match (true) { is_string($object) => new $object(), is_object($object) => $object, } ); }, $objects ); } } image/src/Drivers/SpecializableModifier.php000064400000000722152665633650015014 0ustar00modify($this); } } image/src/Drivers/AbstractFontProcessor.php000064400000012224152665633650015060 0ustar00wrapTextBlock(new TextBlock($text), $font); $pivot = $this->buildPivot($lines, $font, $position); $leading = $this->leading($font); $blockWidth = $this->boxSize((string) $lines->longestLine(), $font)->width(); $x = $pivot->x(); $y = $font->hasFilename() ? $pivot->y() + $this->capHeight($font) : $pivot->y(); $xAdjustment = 0; // adjust line positions according to alignment foreach ($lines as $line) { $lineBoxSize = $this->boxSize((string) $line, $font); $lineWidth = $lineBoxSize->width() + $lineBoxSize->pivot()->x(); $xAdjustment = $font->alignment() === 'left' ? 0 : $blockWidth - $lineWidth; $xAdjustment = $font->alignment() === 'right' ? intval(round($xAdjustment)) : $xAdjustment; $xAdjustment = $font->alignment() === 'center' ? intval(round($xAdjustment / 2)) : $xAdjustment; $position = new Point($x + $xAdjustment, $y); $position->rotate($font->angle(), $pivot); $line->setPosition($position); $y += $leading; } return $lines; } /** * {@inheritdoc} * * @see FontProcessorInterface::nativeFontSize() */ public function nativeFontSize(FontInterface $font): float { return $font->size(); } /** * {@inheritdoc} * * @see FontProcessorInterface::typographicalSize() */ public function typographicalSize(FontInterface $font): int { return $this->boxSize('Hy', $font)->height(); } /** * {@inheritdoc} * * @see FontProcessorInterface::capHeight() */ public function capHeight(FontInterface $font): int { return $this->boxSize('T', $font)->height(); } /** * {@inheritdoc} * * @see FontProcessorInterface::leading() */ public function leading(FontInterface $font): int { return intval(round($this->typographicalSize($font) * $font->lineHeight())); } /** * Reformat a text block by wrapping each line before the given maximum width * * @throws FontException */ protected function wrapTextBlock(TextBlock $block, FontInterface $font): TextBlock { $newLines = []; foreach ($block as $line) { foreach ($this->wrapLine($line, $font) as $newLine) { $newLines[] = $newLine; } } return $block->setLines($newLines); } /** * Check if a line exceeds the given maximum width and wrap it if necessary. * The output will be an array of formatted lines that are all within the * maximum width. * * @throws FontException * @return array */ protected function wrapLine(Line $line, FontInterface $font): array { // no wrap width - no wrapping if (is_null($font->wrapWidth())) { return [$line]; } $wrapped = []; $formattedLine = new Line(); foreach ($line as $word) { // calculate width of newly formatted line $lineWidth = $this->boxSize(match ($formattedLine->count()) { 0 => $word, default => $formattedLine . ' ' . $word, }, $font)->width(); // decide if word fits on current line or a new line must be created if ($line->count() === 1 || $lineWidth <= $font->wrapWidth()) { $formattedLine->add($word); } else { if ($formattedLine->count() !== 0) { $wrapped[] = $formattedLine; } $formattedLine = new Line($word); } } $wrapped[] = $formattedLine; return $wrapped; } /** * Build pivot point of textblock according to the font settings and based on given position * * @throws FontException */ protected function buildPivot(TextBlock $block, FontInterface $font, PointInterface $position): PointInterface { // bounding box $box = new Rectangle( $this->boxSize((string) $block->longestLine(), $font)->width(), $this->leading($font) * ($block->count() - 1) + $this->capHeight($font) ); // set position $box->setPivot($position); // alignment $box->align($font->alignment()); $box->valign($font->valignment()); $box->rotate($font->angle()); return $box->last(); } } image/src/Drivers/SpecializableAnalyzer.php000064400000000716152665633650015046 0ustar00analyze($this); } } image/src/Drivers/SpecializableEncoder.php000064400000000473152665633650014640 0ustar00colorspace(); foreach ($image as $frame) { $colors->push( parent::colorAt($colorspace, $frame->native()) ); } return $colors; } } image/src/Drivers/Imagick/Analyzers/PixelColorAnalyzer.php000064400000002101152665633650017661 0ustar00colorAt( $image->colorspace(), $image->core()->frame($this->frame_key)->native() ); } /** * @throws ColorException */ protected function colorAt(ColorspaceInterface $colorspace, Imagick $imagick): ColorInterface { return $this->driver() ->colorProcessor($colorspace) ->nativeToColor( $imagick->getImagePixelColor($this->x, $this->y) ); } } image/src/Drivers/Imagick/Analyzers/ResolutionAnalyzer.php000064400000001337152665633650017756 0ustar00core()->native(); $imageResolution = $imagick->getImageResolution(); return new Resolution( $imageResolution['x'], $imageResolution['y'], $imagick->getImageUnits(), ); } } image/src/Drivers/Imagick/Analyzers/ColorspaceAnalyzer.php000064400000001406152665633650017702 0ustar00core()->native()->getImageColorspace()) { Imagick::COLORSPACE_CMYK => new CmykColorspace(), default => new RgbColorspace(), }; } } image/src/Drivers/Imagick/Analyzers/WidthAnalyzer.php000064400000000742152665633650016671 0ustar00core()->native()->getImageWidth(); } } image/src/Drivers/Imagick/Analyzers/HeightAnalyzer.php000064400000000747152665633650017027 0ustar00core()->native()->getImageHeight(); } } image/src/Drivers/Imagick/Analyzers/ProfileAnalyzer.php000064400000001405152665633650017207 0ustar00core()->native()->getImageProfiles('icc'); if (!array_key_exists('icc', $profiles)) { throw new ColorException('No ICC profile found in image.'); } return new Profile($profiles['icc']); } } image/src/Drivers/Imagick/Core.php000064400000014601152665633650013023 0ustar00 */ class Core implements CoreInterface, Iterator { protected int $iteratorIndex = 0; /** * Create new core instance * * @return void */ public function __construct(protected Imagick $imagick) { // } /** * {@inheritdoc} * * @see CollectionInterface::has() */ public function has(int|string $key): bool { try { $result = $this->imagick->setIteratorIndex($key); } catch (ImagickException) { return false; } return $result; } /** * {@inheritdoc} * * @see CollectionInterface::push() */ public function push(mixed $item): CollectionInterface { return $this->add($item); } /** * {@inheritdoc} * * @see CollectionInterface::get() */ public function get(int|string $key, mixed $default = null): mixed { try { $this->imagick->setIteratorIndex($key); } catch (ImagickException) { return $default; } return new Frame($this->imagick->current()); } /** * {@inheritdoc} * * @see CollectionInterface::getAtPosition() */ public function getAtPosition(int $key = 0, mixed $default = null): mixed { return $this->get($key, $default); } /** * {@inheritdoc} * * @see CollectionInterface::empty() */ public function empty(): CollectionInterface { $this->imagick->clear(); return $this; } /** * {@inheritdoc} * * @see CollectionInterface::slice() */ public function slice(int $offset, ?int $length = null): CollectionInterface { $allowed_indexes = []; $length = is_null($length) ? $this->count() : $length; for ($i = $offset; $i < $offset + $length; $i++) { $allowed_indexes[] = $i; } $sliced = new Imagick(); foreach ($this->imagick as $key => $native) { if (in_array($key, $allowed_indexes)) { $sliced->addImage($native->getImage()); } } $sliced = $sliced->coalesceImages(); $sliced->setImageIterations($this->imagick->getImageIterations()); $this->imagick = $sliced; return $this; } /** * {@inheritdoc} * * @see CoreInterface::add() */ public function add(FrameInterface $frame): CoreInterface { $imagick = $frame->native(); $imagick->setImageDelay( (int) round($frame->delay() * 100) ); $imagick->setImageDispose($frame->dispose()); $size = $frame->size(); $imagick->setImagePage( $size->width(), $size->height(), $frame->offsetLeft(), $frame->offsetTop() ); $this->imagick->addImage($imagick); return $this; } /** * {@inheritdoc} * * @see CoreInterface::count() */ public function count(): int { return $this->imagick->getNumberImages(); } /** * {@inheritdoc} * * @see Iterator::rewind() */ public function current(): mixed { $this->imagick->setIteratorIndex($this->iteratorIndex); return new Frame($this->imagick->current()); } /** * {@inheritdoc} * * @see Iterator::rewind() */ public function next(): void { $this->iteratorIndex += 1; } /** * {@inheritdoc} * * @see Iterator::rewind() */ public function key(): mixed { return $this->iteratorIndex; } /** * {@inheritdoc} * * @see Iterator::rewind() */ public function valid(): bool { try { $result = $this->imagick->setIteratorIndex($this->iteratorIndex); } catch (ImagickException) { return false; } return $result; } /** * {@inheritdoc} * * @see Iterator::rewind() */ public function rewind(): void { $this->iteratorIndex = 0; } /** * {@inheritdoc} * * @see CoreInterface::native() */ public function native(): mixed { return $this->imagick; } /** * {@inheritdoc} * * @see CoreInterface::setNative() */ public function setNative(mixed $native): CoreInterface { $this->imagick = $native; return $this; } /** * {@inheritdoc} * * @see CoreInterface::frame() */ public function frame(int $position): FrameInterface { foreach ($this->imagick as $core) { if ($core->getIteratorIndex() === $position) { return new Frame($core); } } throw new AnimationException('Frame #' . $position . ' could not be found in the image.'); } /** * {@inheritdoc} * * @see CoreInterface::loops() */ public function loops(): int { return $this->imagick->getImageIterations(); } /** * {@inheritdoc} * * @see CoreInterface::setLoops() */ public function setLoops(int $loops): CoreInterface { $this->imagick = $this->imagick->coalesceImages(); $this->imagick->setImageIterations($loops); return $this; } /** * {@inheritdoc} * * @see CollectionInterface::first() */ public function first(): FrameInterface { return $this->frame(0); } /** * {@inheritdoc} * * @see CollectableInterface::last() */ public function last(): FrameInterface { return $this->frame($this->count() - 1); } /** * {@inheritdoc} * * @see CollectionInterface::toArray() */ public function toArray(): array { $frames = []; foreach ($this as $frame) { $frames[] = $frame; } return $frames; } /** * Clone instance */ public function __clone(): void { $this->imagick = clone $this->imagick; } } image/src/Drivers/Imagick/Modifiers/PixelateModifier.php000064400000001705152665633650017307 0ustar00pixelateFrame($frame); } return $image; } protected function pixelateFrame(FrameInterface $frame): void { $size = $frame->size(); $frame->native()->scaleImage( (int) round(max(1, $size->width() / $this->size)), (int) round(max(1, $size->height() / $this->size)) ); $frame->native()->scaleImage($size->width(), $size->height()); } } image/src/Drivers/Imagick/Modifiers/AlignRotationModifier.php000064400000003530152665633650020304 0ustar00core()->native()->getImageOrientation()) { case Imagick::ORIENTATION_TOPRIGHT: // 2 $image->core()->native()->flopImage(); break; case Imagick::ORIENTATION_BOTTOMRIGHT: // 3 $image->core()->native()->rotateImage("#000", 180); break; case Imagick::ORIENTATION_BOTTOMLEFT: // 4 $image->core()->native()->rotateImage("#000", 180); $image->core()->native()->flopImage(); break; case Imagick::ORIENTATION_LEFTTOP: // 5 $image->core()->native()->rotateImage("#000", -270); $image->core()->native()->flopImage(); break; case Imagick::ORIENTATION_RIGHTTOP: // 6 $image->core()->native()->rotateImage("#000", -270); break; case Imagick::ORIENTATION_RIGHTBOTTOM: // 7 $image->core()->native()->rotateImage("#000", -90); $image->core()->native()->flopImage(); break; case Imagick::ORIENTATION_LEFTBOTTOM: // 8 $image->core()->native()->rotateImage("#000", -90); break; } // set new orientation in image $image->core()->native()->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); return $image; } } image/src/Drivers/Imagick/Modifiers/ContainModifier.php000064400000005430152665633650017126 0ustar00getCropSize($image); $resize = $this->getResizeSize($image); $transparent = new ImagickPixel('transparent'); $background = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->background) ); foreach ($image as $frame) { $frame->native()->scaleImage( $crop->width(), $crop->height(), ); $frame->native()->setBackgroundColor($transparent); $frame->native()->setImageBackgroundColor($transparent); $frame->native()->extentImage( $resize->width(), $resize->height(), $crop->pivot()->x() * -1, $crop->pivot()->y() * -1 ); if ($resize->width() > $crop->width()) { // fill new emerged background $draw = new ImagickDraw(); $draw->setFillColor($background); $delta = abs($crop->pivot()->x()); if ($delta > 0) { $draw->rectangle( 0, 0, $delta - 1, $resize->height() ); } $draw->rectangle( $crop->width() + $delta, 0, $resize->width(), $resize->height() ); $frame->native()->drawImage($draw); } if ($resize->height() > $crop->height()) { // fill new emerged background $draw = new ImagickDraw(); $draw->setFillColor($background); $delta = abs($crop->pivot()->y()); if ($delta > 0) { $draw->rectangle( 0, 0, $resize->width(), $delta - 1 ); } $draw->rectangle( 0, $crop->height() + $delta, $resize->width(), $resize->height() ); $frame->native()->drawImage($draw); } } return $image; } } image/src/Drivers/Imagick/Modifiers/SliceAnimationModifier.php000064400000001354152665633650020433 0ustar00offset >= $image->count()) { throw new AnimationException('Offset is not in the range of frames.'); } $image->core()->slice($this->offset, $this->length); return $image; } } image/src/Drivers/Imagick/Modifiers/ResolutionModifier.php000064400000001102152665633650017666 0ustar00core()->native(); $imagick->setImageResolution($this->x, $this->y); return $image; } } image/src/Drivers/Imagick/Modifiers/CoverDownModifier.php000064400000000665152665633650017446 0ustar00resizeDown($this->width, $this->height); } } image/src/Drivers/Imagick/Modifiers/GreyscaleModifier.php000064400000001100152665633650017437 0ustar00native()->modulateImage(100, 0, 100); } return $image; } } image/src/Drivers/Imagick/Modifiers/FlipModifier.php000064400000001035152665633650016422 0ustar00native()->flipImage(); } return $image; } } image/src/Drivers/Imagick/Modifiers/ResizeCanvasModifier.php000064400000001360152665633650020126 0ustar00cropSize($image); $image->modify(new CropModifier( $cropSize->width(), $cropSize->height(), $cropSize->pivot()->x(), $cropSize->pivot()->y(), $this->background, )); return $image; } } image/src/Drivers/Imagick/Modifiers/BlurModifier.php000064400000001077152665633650016442 0ustar00native()->blurImage($this->amount, 0.5 * $this->amount); } return $image; } } image/src/Drivers/Imagick/Modifiers/ContrastModifier.php000064400000001144152665633650017326 0ustar00native()->sigmoidalContrastImage($this->level > 0, abs($this->level / 4), 0); } return $image; } } image/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php000064400000001560152665633650017432 0ustar00driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->color) ); $pixel = new ImagickDraw(); $pixel->setFillColor($color); $pixel->point($this->position->x(), $this->position->y()); foreach ($image as $frame) { $frame->native()->drawImage($pixel); } return $image; } } image/src/Drivers/Imagick/Modifiers/RemoveAnimationModifier.php000064400000001413152665633650020625 0ustar00selectedFrame($image); $imagick->addImage($frame->native()->getImage()); // set new imagick to image $image->core()->setNative($imagick); return $image; } } image/src/Drivers/Imagick/Modifiers/ScaleDownModifier.php000064400000000621152665633650017407 0ustar00size()->scaleDown($this->width, $this->height); } } image/src/Drivers/Imagick/Modifiers/ScaleModifier.php000064400000000611152665633650016556 0ustar00size()->scale($this->width, $this->height); } } image/src/Drivers/Imagick/Modifiers/ProfileRemovalModifier.php000064400000001376152665633650020466 0ustar00core()->native(); $result = $imagick->profileImage('icc', null); if ($result === false) { throw new ColorException('ICC color profile could not be removed.'); } return $image; } } image/src/Drivers/Imagick/Modifiers/PadModifier.php000064400000001071152665633650016234 0ustar00size() ->containMax( $this->width, $this->height ) ->alignPivotTo( $this->getResizeSize($image), $this->position ); } } image/src/Drivers/Imagick/Modifiers/RotateModifier.php000064400000001441152665633650016767 0ustar00driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->background) ); foreach ($image as $frame) { $frame->native()->rotateImage( $background, $this->rotationAngle() * -1 ); } return $image; } } image/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php000064400000003437152665633650020005 0ustar00setFillColor(new ImagickPixel('transparent')); // defaults to no backgroundColor if ($this->drawable->hasBackgroundColor()) { $background_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ); $drawing->setFillColor($background_color); } if ($this->drawable->hasBorder()) { $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->borderColor() ); $drawing->setStrokeColor($border_color); $drawing->setStrokeWidth($this->drawable->borderSize()); } $drawing->polygon($this->points()); foreach ($image as $frame) { $frame->native()->drawImage($drawing); } return $image; } /** * Return points of drawable in processable form for ImagickDraw * * @return array> */ private function points(): array { $points = []; foreach ($this->drawable as $point) { $points[] = ['x' => $point->x(), 'y' => $point->y()]; } return $points; } } image/src/Drivers/Imagick/Modifiers/FlopModifier.php000064400000001041152665633650016425 0ustar00native()->flopImage(); } return $image; } } image/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php000064400000002102152665633650021142 0ustar00blendingColor($this->driver()); // get imagickpixel from blending color $pixel = $this->driver() ->colorProcessor($image->colorspace()) ->colorToNative($blendingColor); // merge transparent areas with the background color foreach ($image as $frame) { $frame->native()->setImageBackgroundColor($pixel); $frame->native()->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); $frame->native()->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); } return $image; } } image/src/Drivers/Imagick/Modifiers/TrimModifier.php000064400000001476152665633650016454 0ustar00isAnimated()) { throw new NotSupportedException('Trim modifier cannot be applied to animated images.'); } $imagick = $image->core()->native(); $imagick->trimImage(($this->tolerance / 100 * $imagick->getQuantum()) / 1.5); $imagick->setImagePage(0, 0, 0, 0); return $image; } } image/src/Drivers/Imagick/Modifiers/TextModifier.php000064400000010475152665633650016464 0ustar00processor()->textBlock($this->text, $this->font, $this->position); $drawText = $this->imagickDrawText($image, $this->font); $drawStroke = $this->imagickDrawStroke($image, $this->font); foreach ($image as $frame) { foreach ($lines as $line) { foreach ($this->strokeOffsets($this->font) as $offset) { // Draw the stroke outline under the actual text $this->maybeDrawTextline($frame, $line, $drawStroke, $offset); } // Draw the actual text $this->maybeDrawTextline($frame, $line, $drawText); } } return $image; } /** * Create an ImagickDraw object to draw text on the image * * @throws RuntimeException * @throws ColorException * @throws FontException * @throws ImagickDrawException * @throws ImagickException */ private function imagickDrawText(ImageInterface $image, FontInterface $font): ImagickDraw { $color = $this->driver()->handleInput($font->color()); if ($font->hasStrokeEffect() && $color->isTransparent()) { throw new ColorException( 'The text color must be fully opaque when using the stroke effect.' ); } $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative($color); return $this->processor()->toImagickDraw($font, $color); } /** * Create a ImagickDraw object to draw the outline stroke effect on the Image * * @throws RuntimeException * @throws ColorException * @throws FontException * @throws ImagickDrawException * @throws ImagickException */ private function imagickDrawStroke(ImageInterface $image, FontInterface $font): ?ImagickDraw { if (!$font->hasStrokeEffect()) { return null; } $color = $this->driver()->handleInput($font->strokeColor()); if ($color->isTransparent()) { throw new ColorException( 'The stroke color must be fully opaque.' ); } $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative($color); return $this->processor()->toImagickDraw($font, $color); } /** * Maybe draw given line of text on frame instance depending on given * ImageDraw instance. Optionally move line position by given offset. */ private function maybeDrawTextline( FrameInterface $frame, Line $textline, ?ImagickDraw $draw = null, PointInterface $offset = new Point(), ): void { if ($draw instanceof ImagickDraw) { $frame->native()->annotateImage( $draw, $textline->position()->x() + $offset->x(), $textline->position()->y() + $offset->y(), $this->font->angle(), (string) $textline ); } } /** * Return imagick font processor * * @throws FontException */ private function processor(): FontProcessor { $processor = $this->driver()->fontProcessor(); if (!($processor instanceof FontProcessor)) { throw new FontException('Font processor does not match the driver.'); } return $processor; } } image/src/Drivers/Imagick/Modifiers/DrawEllipseModifier.php000064400000002760152665633650017751 0ustar00driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ); $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->borderColor() ); foreach ($image as $frame) { $drawing = new ImagickDraw(); $drawing->setFillColor($background_color); if ($this->drawable->hasBorder()) { $drawing->setStrokeWidth($this->drawable->borderSize()); $drawing->setStrokeColor($border_color); } $drawing->ellipse( $this->drawable->position()->x(), $this->drawable->position()->y(), $this->drawable->width() / 2, $this->drawable->height() / 2, 0, 360 ); $frame->native()->drawImage($drawing); } return $image; } } image/src/Drivers/Imagick/Modifiers/ColorspaceModifier.php000064400000003040152665633650017620 0ustar00 */ protected static array $mapping = [ RgbColorspace::class => Imagick::COLORSPACE_SRGB, CmykColorspace::class => Imagick::COLORSPACE_CMYK, ]; public function apply(ImageInterface $image): ImageInterface { $colorspace = $this->targetColorspace(); $imagick = $image->core()->native(); $imagick->transformImageColorspace( $this->getImagickColorspace($colorspace) ); return $image; } /** * @throws NotSupportedException */ private function getImagickColorspace(ColorspaceInterface $colorspace): int { if (!array_key_exists($colorspace::class, self::$mapping)) { throw new NotSupportedException('Given colorspace is not supported.'); } return self::$mapping[$colorspace::class]; } } image/src/Drivers/Imagick/Modifiers/ResizeModifier.php000064400000001743152665633650016777 0ustar00getAdjustedSize($image); foreach ($image as $frame) { $frame->native()->scaleImage( $resizeTo->width(), $resizeTo->height() ); } return $image; } /** * @throws RuntimeException */ protected function getAdjustedSize(ImageInterface $image): SizeInterface { return $image->size()->resize($this->width, $this->height); } } image/src/Drivers/Imagick/Modifiers/DrawBezierModifier.php000064400000005037152665633650017574 0ustar00drawable->count() !== 3 && $this->drawable->count() !== 4) { throw new GeometryException('You must specify either 3 or 4 points to create a bezier curve'); } $drawing = new ImagickDraw(); if ($this->drawable->hasBackgroundColor()) { $background_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ); } else { $background_color = 'transparent'; } $drawing->setFillColor($background_color); if ($this->drawable->hasBorder() && $this->drawable->borderSize() > 0) { $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->borderColor() ); $drawing->setStrokeColor($border_color); $drawing->setStrokeWidth($this->drawable->borderSize()); } $drawing->pathStart(); $drawing->pathMoveToAbsolute( $this->drawable->first()->x(), $this->drawable->first()->y() ); if ($this->drawable->count() === 3) { $drawing->pathCurveToQuadraticBezierAbsolute( $this->drawable->second()->x(), $this->drawable->second()->y(), $this->drawable->last()->x(), $this->drawable->last()->y() ); } elseif ($this->drawable->count() === 4) { $drawing->pathCurveToAbsolute( $this->drawable->second()->x(), $this->drawable->second()->y(), $this->drawable->third()->x(), $this->drawable->third()->y(), $this->drawable->last()->x(), $this->drawable->last()->y() ); } $drawing->pathFinish(); foreach ($image as $frame) { $frame->native()->drawImage($drawing); } return $image; } } image/src/Drivers/Imagick/Modifiers/ResizeDownModifier.php000064400000000623152665633650017623 0ustar00size()->resizeDown($this->width, $this->height); } } image/src/Drivers/Imagick/Modifiers/DrawLineModifier.php000064400000002226152665633650017240 0ustar00setStrokeWidth($this->drawable->width()); $drawing->setFillOpacity(0); $drawing->setStrokeColor( $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ) ); $drawing->line( $this->drawable->start()->x(), $this->drawable->start()->y(), $this->drawable->end()->x(), $this->drawable->end()->y(), ); foreach ($image as $frame) { $frame->native()->drawImage($drawing); } return $image; } } image/src/Drivers/Imagick/Modifiers/InvertModifier.php000064400000001054152665633650017000 0ustar00native()->negateImage(false); } return $image; } } image/src/Drivers/Imagick/Modifiers/StripMetaModifier.php000064400000001676152665633650017453 0ustar00core()->native()->getImageProfiles('icc'); // remove meta data $image->core()->native()->stripImage(); $image->setExif(new Collection()); if ($profiles !== []) { // re-apply icc profiles $image->core()->native()->profileImage("icc", $profiles['icc']); } return $image; } } image/src/Drivers/Imagick/Modifiers/QuantizeColorsModifier.php000064400000002135152665633650020514 0ustar00limit <= 0) { throw new InputException('Quantization limit must be greater than 0.'); } // no color reduction if the limit is higher than the colors in the img if ($this->limit > $image->core()->native()->getImageColors()) { return $image; } foreach ($image as $frame) { $frame->native()->quantizeImage( $this->limit, $frame->native()->getImageColorspace(), 0, false, false ); } return $image; } } image/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php000064400000002773152665633650020264 0ustar00driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ); $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->borderColor() ); $drawing->setFillColor($background_color); if ($this->drawable->hasBorder()) { $drawing->setStrokeColor($border_color); $drawing->setStrokeWidth($this->drawable->borderSize()); } // build rectangle $drawing->rectangle( $this->drawable->position()->x(), $this->drawable->position()->y(), $this->drawable->position()->x() + $this->drawable->width(), $this->drawable->position()->y() + $this->drawable->height() ); foreach ($image as $frame) { $frame->native()->drawImage($drawing); } return $image; } } image/src/Drivers/Imagick/Modifiers/CoverModifier.php000064400000001713152665633650016611 0ustar00getCropSize($image); $resize = $this->getResizeSize($crop); foreach ($image as $frame) { $frame->native()->cropImage( $crop->width(), $crop->height(), $crop->pivot()->x(), $crop->pivot()->y() ); $frame->native()->scaleImage( $resize->width(), $resize->height() ); $frame->native()->setImagePage(0, 0, 0, 0); } return $image; } } image/src/Drivers/Imagick/Modifiers/ResizeCanvasRelativeModifier.php000064400000000633152665633650021624 0ustar00driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->color) ); foreach ($image->core()->native() as $frame) { if ($this->hasPosition()) { $this->floodFillWithColor($frame, $pixel); } else { $this->fillAllWithColor($frame, $pixel); } } return $image; } private function floodFillWithColor(Imagick $frame, ImagickPixel $pixel): void { $target = $frame->getImagePixelColor( $this->position->x(), $this->position->y() ); $frame->floodfillPaintImage( $pixel, 100, $target, $this->position->x(), $this->position->y(), false, Imagick::CHANNEL_ALL ); } private function fillAllWithColor(Imagick $frame, ImagickPixel $pixel): void { $draw = new ImagickDraw(); $draw->setFillColor($pixel); $draw->rectangle(0, 0, $frame->getImageWidth(), $frame->getImageHeight()); $frame->drawImage($draw); // deactive alpha channel when image was filled with opaque color if ($pixel->getColorValue(Imagick::COLOR_ALPHA) == 1) { $frame->setImageAlphaChannel(Imagick::ALPHACHANNEL_DEACTIVATE); } } } image/src/Drivers/Imagick/Modifiers/BrightnessModifier.php000064400000001125152665633650017640 0ustar00native()->modulateImage(100 + $this->level, 100, 100); } return $image; } } image/src/Drivers/Imagick/Modifiers/SharpenModifier.php000064400000001115152665633650017127 0ustar00native()->unsharpMaskImage(1, 1, $this->amount / 6.25, 0); } return $image; } } image/src/Drivers/Imagick/Modifiers/ColorizeModifier.php000064400000002276152665633650017326 0ustar00normalizeLevel($this->red); $green = $this->normalizeLevel($this->green); $blue = $this->normalizeLevel($this->blue); foreach ($image as $frame) { $qrange = $frame->native()->getQuantumRange(); $frame->native()->levelImage(0, $red, $qrange['quantumRangeLong'], Imagick::CHANNEL_RED); $frame->native()->levelImage(0, $green, $qrange['quantumRangeLong'], Imagick::CHANNEL_GREEN); $frame->native()->levelImage(0, $blue, $qrange['quantumRangeLong'], Imagick::CHANNEL_BLUE); } return $image; } private function normalizeLevel(int $level): int { return $level > 0 ? intval(round($level / 5)) : intval(round(($level + 100) / 100)); } } image/src/Drivers/Imagick/Modifiers/CropModifier.php000064400000005627152665633650016446 0ustar00driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->background) ); // create empty container imagick to rebuild core $imagick = new Imagick(); // save resolution to add it later $resolution = $image->resolution()->perInch(); // define position of the image on the new canvas $crop = $this->crop($image); $position = [ ($crop->pivot()->x() + $this->offset_x) * -1, ($crop->pivot()->y() + $this->offset_y) * -1, ]; foreach ($image as $frame) { // create new frame canvas with modifiers background $canvas = new Imagick(); $canvas->newImage($crop->width(), $crop->height(), $background, 'png'); $canvas->setImageResolution($resolution->x(), $resolution->y()); $canvas->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET); // or ALPHACHANNEL_ACTIVATE? // set animation details if ($image->isAnimated()) { $canvas->setImageDelay($frame->native()->getImageDelay()); $canvas->setImageIterations($frame->native()->getImageIterations()); $canvas->setImageDispose($frame->native()->getImageDispose()); } // make the rectangular position of the original image transparent // so that we can later place the original on top. this preserves // the transparency of the original and shows the background color // of the modifier in the other areas. if the original image has no // transparent area the rectangular transparency will be covered by // the original. $clearer = new Imagick(); $clearer->newImage( $frame->native()->getImageWidth(), $frame->native()->getImageHeight(), new ImagickPixel('black'), ); $canvas->compositeImage($clearer, Imagick::COMPOSITE_DSTOUT, ...$position); // place original frame content onto prepared frame canvas $canvas->compositeImage($frame->native(), Imagick::COMPOSITE_DEFAULT, ...$position); // add newly built frame to container imagick $imagick->addImage($canvas); } // replace imagick in the original image $image->core()->setNative($imagick); return $image; } } image/src/Drivers/Imagick/Modifiers/PlaceModifier.php000064400000002343152665633650016557 0ustar00driver()->handleInput($this->element); $position = $this->getPosition($image, $watermark); // set opacity of watermark if ($this->opacity < 100) { $watermark->core()->native()->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET); $watermark->core()->native()->evaluateImage( Imagick::EVALUATE_DIVIDE, $this->opacity > 0 ? 100 / $this->opacity : 1000, Imagick::CHANNEL_ALPHA, ); } foreach ($image as $frame) { $frame->native()->compositeImage( $watermark->core()->native(), Imagick::COMPOSITE_DEFAULT, $position->x(), $position->y() ); } return $image; } } image/src/Drivers/Imagick/Modifiers/ProfileModifier.php000064400000001361152665633650017132 0ustar00core()->native(); $result = $imagick->profileImage('icc', (string) $this->profile); if ($result === false) { throw new ColorException('ICC color profile could not be set.'); } return $image; } } image/src/Drivers/Imagick/Modifiers/GammaModifier.php000064400000001056152665633650016555 0ustar00native()->gammaImage($this->gamma); } return $image; } } image/src/Drivers/Imagick/Decoders/EncodedImageObjectDecoder.php000064400000001257152665633650020607 0ustar00toString()); } } image/src/Drivers/Imagick/Decoders/SplFileInfoImageDecoder.php000064400000001235152665633650020265 0ustar00getRealPath()); } } image/src/Drivers/Imagick/Decoders/FilePathImageDecoder.php000064400000002265152665633650017613 0ustar00isFile($input)) { throw new DecoderException('Unable to decode input'); } try { $imagick = new Imagick(); $imagick->readImage($input); } catch (ImagickException) { throw new DecoderException('Unable to decode input'); } // decode image $image = parent::decode($imagick); // set file path on origin $image->origin()->setFilePath($input); // extract exif data for the appropriate formats if (in_array($imagick->getImageFormat(), ['JPEG', 'TIFF', 'TIF'])) { $image->setExif($this->extractExifData($input)); } return $image; } } image/src/Drivers/Imagick/Decoders/Base64ImageDecoder.php000064400000001212152665633650017132 0ustar00isValidBase64($input)) { throw new DecoderException('Unable to decode input'); } return parent::decode(base64_decode((string) $input)); } } image/src/Drivers/Imagick/Decoders/DataUriImageDecoder.php000064400000001605152665633650017445 0ustar00parseDataUri($input); if (!$uri->isValid()) { throw new DecoderException('Unable to decode input'); } if ($uri->isBase64Encoded()) { return parent::decode(base64_decode($uri->data())); } return parent::decode(urldecode($uri->data())); } } image/src/Drivers/Imagick/Decoders/BinaryImageDecoder.php000064400000002350152665633650017336 0ustar00readImageBlob($input); } catch (ImagickException) { throw new DecoderException('Unable to decode input'); } // decode image $image = parent::decode($imagick); // get media type enum from string media type $format = Format::tryCreate($image->origin()->mediaType()); // extract exif data for appropriate formats if (in_array($format, [Format::JPEG, Format::TIFF])) { $image->setExif($this->extractExifData($input)); } return $image; } } image/src/Drivers/Imagick/Decoders/FilePointerImageDecoder.php000064400000001467152665633650020342 0ustar00getImageFormat() !== 'JPEG') { $input = $input->coalesceImages(); } // turn images with colorspace 'GRAY' into 'SRGB' to avoid working on // greyscale colorspace images as this results images loosing color // information when placed into this image. if ($input->getImageColorspace() == Imagick::COLORSPACE_GRAY) { $input->setImageColorspace(Imagick::COLORSPACE_SRGB); } // create image object $image = new Image( $this->driver(), new Core($input) ); // discard animation depending on config if (!$this->driver()->config()->decodeAnimation) { $image->modify(new RemoveAnimationModifier()); } // adjust image rotatation if ($this->driver()->config()->autoOrientation) { $image->modify(new AlignRotationModifier()); } // set media type on origin $image->origin()->setMediaType($input->getImageMimeType()); return $image; } } image/src/Drivers/Imagick/Driver.php000064400000011214152665633650013363 0ustar00newImage($width, $height, $background, 'png'); $imagick->setType(Imagick::IMGTYPE_UNDEFINED); $imagick->setImageType(Imagick::IMGTYPE_UNDEFINED); $imagick->setColorspace(Imagick::COLORSPACE_SRGB); $imagick->setImageResolution(96, 96); $imagick->setImageBackgroundColor($background); return new Image($this, new Core($imagick)); } /** * {@inheritdoc} * * @see DriverInterface::createAnimation() * * @throws RuntimeException */ public function createAnimation(callable $init): ImageInterface { $imagick = new Imagick(); $imagick->setFormat('gif'); $animation = new class ($this, $imagick) { public function __construct( protected DriverInterface $driver, public Imagick $imagick ) { // } /** * @throws RuntimeException */ public function add(mixed $source, float $delay = 1): self { $native = $this->driver->handleInput($source)->core()->native(); $native->setImageDelay(intval(round($delay * 100))); $this->imagick->addImage($native); return $this; } /** * @throws RuntimeException */ public function __invoke(): ImageInterface { return new Image( $this->driver, new Core($this->imagick) ); } }; $init($animation); return call_user_func($animation); } /** * {@inheritdoc} * * @see DriverInterface::colorProcessor() */ public function colorProcessor(ColorspaceInterface $colorspace): ColorProcessorInterface { return new ColorProcessor($colorspace); } /** * {@inheritdoc} * * @see DriverInterface::fontProcessor() */ public function fontProcessor(): FontProcessorInterface { return new FontProcessor(); } /** * {@inheritdoc} * * @see DriverInterface::supports() */ public function supports(string|Format|FileExtension|MediaType $identifier): bool { try { $format = Format::create($identifier); } catch (NotSupportedException) { return false; } return count(Imagick::queryFormats($format->name)) >= 1; } /** * Return version of ImageMagick library * * @throws DriverException */ public static function version(): string { $pattern = '/^ImageMagick (?P(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)' . '(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?' . '(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)/'; if (preg_match($pattern, Imagick::getVersion()['versionString'], $matches) !== 1) { throw new DriverException('Unable to read ImageMagick version number.'); } return $matches['version']; } } image/src/Drivers/Imagick/FontProcessor.php000064400000003703152665633650014742 0ustar00toImagickDraw($font); $dimensions = (new Imagick())->queryFontMetrics($draw, $text); return new Rectangle( intval(round($dimensions['textWidth'])), intval(round($dimensions['ascender'] + $dimensions['descender'])), ); } /** * Imagick::annotateImage() needs an ImagickDraw object - this method takes * the font object as the base and adds an optional passed color to the new * ImagickDraw object. * * @throws FontException * @throws ImagickDrawException * @throws ImagickException */ public function toImagickDraw(FontInterface $font, ?ImagickPixel $color = null): ImagickDraw { if (!$font->hasFilename()) { throw new FontException('No font file specified.'); } $draw = new ImagickDraw(); $draw->setStrokeAntialias(true); $draw->setTextAntialias(true); $draw->setFont($font->filename()); $draw->setFontSize($this->nativeFontSize($font)); $draw->setTextAlignment(Imagick::ALIGN_LEFT); if ($color instanceof ImagickPixel) { $draw->setFillColor($color); } return $draw; } } image/src/Drivers/Imagick/ColorProcessor.php000064400000002725152665633650015115 0ustar00convertTo($this->colorspace) ); } public function nativeToColor(mixed $native): ColorInterface { return match ($this->colorspace::class) { CmykColorspace::class => $this->colorspace->colorFromNormalized([ $native->getColorValue(Imagick::COLOR_CYAN), $native->getColorValue(Imagick::COLOR_MAGENTA), $native->getColorValue(Imagick::COLOR_YELLOW), $native->getColorValue(Imagick::COLOR_BLACK), ]), default => $this->colorspace->colorFromNormalized([ $native->getColorValue(Imagick::COLOR_RED), $native->getColorValue(Imagick::COLOR_GREEN), $native->getColorValue(Imagick::COLOR_BLUE), $native->getColorValue(Imagick::COLOR_ALPHA), ]), }; } } image/src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php000064400000002565152665633650016372 0ustar00strip || (is_null($this->strip) && $this->driver()->config()->strip)) { $image->modify(new StripMetaModifier()); } $imagick = $image->core()->native(); $imagick->setImageBackgroundColor('white'); $imagick->setBackgroundColor('white'); $imagick->setFormat($format); $imagick->setImageFormat($format); $imagick->setCompression($compression); $imagick->setImageCompression($compression); $imagick->setCompressionQuality($this->quality); $imagick->setImageCompressionQuality($this->quality); return new EncodedImage($imagick->getImagesBlob(), 'image/jp2'); } } image/src/Drivers/Imagick/Encoders/BmpEncoder.php000064400000001613152665633650015712 0ustar00core()->native(); $imagick->setFormat($format); $imagick->setImageFormat($format); $imagick->setCompression($compression); $imagick->setImageCompression($compression); return new EncodedImage($imagick->getImagesBlob(), 'image/bmp'); } } image/src/Drivers/Imagick/Encoders/AvifEncoder.php000064400000002403152665633650016057 0ustar00strip || (is_null($this->strip) && $this->driver()->config()->strip)) { $image->modify(new StripMetaModifier()); } $imagick = $image->core()->native(); $imagick->setFormat($format); $imagick->setImageFormat($format); $imagick->setCompression($compression); $imagick->setImageCompression($compression); $imagick->setCompressionQuality($this->quality); $imagick->setImageCompressionQuality($this->quality); return new EncodedImage($imagick->getImagesBlob(), 'image/avif'); } } image/src/Drivers/Imagick/Encoders/GifEncoder.php000064400000001774152665633650015711 0ustar00core()->native(); $imagick->setFormat($format); $imagick->setImageFormat($format); $imagick->setCompression($compression); $imagick->setImageCompression($compression); if ($this->interlaced) { $imagick->setInterlaceScheme(Imagick::INTERLACE_LINE); } return new EncodedImage($imagick->getImagesBlob(), 'image/gif'); } } image/src/Drivers/Imagick/Encoders/PngEncoder.php000064400000002516152665633650015723 0ustar00indexed) { // reduce colors $output = clone $image; $output->reduceColors(256); $output = $output->core()->native(); $output->setFormat('PNG'); $output->setImageFormat('PNG'); } else { $output = clone $image->core()->native(); $output->setFormat('PNG32'); $output->setImageFormat('PNG32'); } $output->setCompression(Imagick::COMPRESSION_ZIP); $output->setImageCompression(Imagick::COMPRESSION_ZIP); if ($this->interlaced) { $output->setInterlaceScheme(Imagick::INTERLACE_LINE); } return new EncodedImage($output->getImagesBlob(), 'image/png'); } } image/src/Drivers/Imagick/Encoders/TiffEncoder.php000064400000002353152665633650016066 0ustar00strip || (is_null($this->strip) && $this->driver()->config()->strip)) { $image->modify(new StripMetaModifier()); } $imagick = $image->core()->native(); $imagick->setFormat($format); $imagick->setImageFormat($format); $imagick->setCompression($imagick->getImageCompression()); $imagick->setImageCompression($imagick->getImageCompression()); $imagick->setCompressionQuality($this->quality); $imagick->setImageCompressionQuality($this->quality); return new EncodedImage($imagick->getImagesBlob(), 'image/tiff'); } } image/src/Drivers/Imagick/Encoders/JpegEncoder.php000064400000004067152665633650016067 0ustar00driver()->handleInput( $this->driver()->config()->blendingColor ); // resolve blending color because jpeg has no transparency $background = $this->driver() ->colorProcessor($image->colorspace()) ->colorToNative($blendingColor); // set alpha value to 1 because Imagick renders // possible full transparent colors as black $background->setColorValue(Imagick::COLOR_ALPHA, 1); // strip meta data if ($this->strip || (is_null($this->strip) && $this->driver()->config()->strip)) { $image->modify(new StripMetaModifier()); } /** @var Imagick $imagick */ $imagick = $image->core()->native(); $imagick->setImageBackgroundColor($background); $imagick->setBackgroundColor($background); $imagick->setFormat($format); $imagick->setImageFormat($format); $imagick->setCompression($compression); $imagick->setImageCompression($compression); $imagick->setCompressionQuality($this->quality); $imagick->setImageCompressionQuality($this->quality); $imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); if ($this->progressive) { $imagick->setInterlaceScheme(Imagick::INTERLACE_PLANE); } return new EncodedImage($imagick->getImagesBlob(), 'image/jpeg'); } } image/src/Drivers/Imagick/Encoders/WebpEncoder.php000064400000003023152665633650016066 0ustar00strip || (is_null($this->strip) && $this->driver()->config()->strip)) { $image->modify(new StripMetaModifier()); } $imagick = $image->core()->native(); $imagick->setImageBackgroundColor(new ImagickPixel('transparent')); if (!$image->isAnimated()) { $imagick = $imagick->mergeImageLayers(Imagick::LAYERMETHOD_MERGE); } $imagick->setFormat($format); $imagick->setImageFormat($format); $imagick->setCompression($compression); $imagick->setImageCompression($compression); $imagick->setImageCompressionQuality($this->quality); if ($this->quality === 100) { $imagick->setOption('webp:lossless', 'true'); } return new EncodedImage($imagick->getImagesBlob(), 'image/webp'); } } image/src/Drivers/Imagick/Encoders/HeicEncoder.php000064400000002140152665633650016040 0ustar00strip || (is_null($this->strip) && $this->driver()->config()->strip)) { $image->modify(new StripMetaModifier()); } $imagick = $image->core()->native(); $imagick->setFormat($format); $imagick->setImageFormat($format); $imagick->setCompressionQuality($this->quality); $imagick->setImageCompressionQuality($this->quality); return new EncodedImage($imagick->getImagesBlob(), 'image/heic'); } } image/src/Drivers/Imagick/Frame.php000064400000007655152665633650013200 0ustar00native->setImageBackgroundColor($background); $this->native->setBackgroundColor($background); } /** * {@inheritdoc} * * @see DriverInterface::toImage() */ public function toImage(DriverInterface $driver): ImageInterface { return new Image($driver, new Core($this->native())); } /** * {@inheritdoc} * * @see DriverInterface::setNative() */ public function setNative(mixed $native): FrameInterface { $this->native = $native; return $this; } /** * {@inheritdoc} * * @see DriverInterface::native() */ public function native(): Imagick { return $this->native; } /** * {@inheritdoc} * * @see DriverInterface::size() */ public function size(): SizeInterface { return new Rectangle( $this->native->getImageWidth(), $this->native->getImageHeight() ); } /** * {@inheritdoc} * * @see DriverInterface::delay() */ public function delay(): float { return $this->native->getImageDelay() / 100; } /** * {@inheritdoc} * * @see DriverInterface::setDelay() */ public function setDelay(float $delay): FrameInterface { $this->native->setImageDelay(intval(round($delay * 100))); return $this; } /** * {@inheritdoc} * * @see DriverInterface::dispose() */ public function dispose(): int { return $this->native->getImageDispose(); } /** * {@inheritdoc} * * @see DriverInterface::setDispose() * * @throws InputException */ public function setDispose(int $dispose): FrameInterface { if (!in_array($dispose, [0, 1, 2, 3])) { throw new InputException('Value for argument $dispose must be 0, 1, 2 or 3.'); } $this->native->setImageDispose($dispose); return $this; } /** * {@inheritdoc} * * @see DriverInterface::setOffset() */ public function setOffset(int $left, int $top): FrameInterface { $this->native->setImagePage( $this->native->getImageWidth(), $this->native->getImageHeight(), $left, $top ); return $this; } /** * {@inheritdoc} * * @see DriverInterface::offsetLeft() */ public function offsetLeft(): int { return $this->native->getImagePage()['x']; } /** * {@inheritdoc} * * @see DriverInterface::setOffsetLeft() */ public function setOffsetLeft(int $offset): FrameInterface { return $this->setOffset($offset, $this->offsetTop()); } /** * {@inheritdoc} * * @see DriverInterface::offsetTop() */ public function offsetTop(): int { return $this->native->getImagePage()['y']; } /** * {@inheritdoc} * * @see DriverInterface::setOffsetTop() */ public function setOffsetTop(int $offset): FrameInterface { return $this->setOffset($this->offsetLeft(), $offset); } } image/src/Drivers/Specializable.php000064400000000434152665633650013335 0ustar00 PHP_MAXPATHLEN) { return false; } try { if (!@is_file($input)) { return false; } } catch (Exception) { return false; } return true; } /** * Extract and return EXIF data from given input which can be binary image * data or a file path. * * @return CollectionInterface */ protected function extractExifData(string $path_or_data): CollectionInterface { if (!function_exists('exif_read_data')) { return new Collection(); } try { $source = match (true) { $this->isFile($path_or_data) => $path_or_data, // path default => $this->buildFilePointer($path_or_data), // data }; // extract exif data $data = @exif_read_data($source, null, true); if (is_resource($source)) { fclose($source); } } catch (Exception) { $data = []; } return new Collection(is_array($data) ? $data : []); } /** * Determine if given input is base64 encoded data */ protected function isValidBase64(mixed $input): bool { if (!is_string($input)) { return false; } return base64_encode(base64_decode($input)) === str_replace(["\n", "\r"], '', $input); } /** * Parse data uri */ protected function parseDataUri(mixed $input): object { $pattern = "/^data:(?P\w+\/[-+.\w]+)?" . "(?P(;[-\w]+=[-\w]+)*)(?P;base64)?,(?P.*)/"; $result = preg_match($pattern, (string) $input, $matches); return new class ($matches, $result) { /** * @param array $matches * @return void */ public function __construct(private array $matches, private int|false $result) { // } public function isValid(): bool { return (bool) $this->result; } public function mediaType(): ?string { if (isset($this->matches['mediatype']) && !empty($this->matches['mediatype'])) { return $this->matches['mediatype']; } return null; } public function hasMediaType(): bool { return !empty($this->mediaType()); } public function isBase64Encoded(): bool { return isset($this->matches['base64']) && $this->matches['base64'] === ';base64'; } public function data(): ?string { if (isset($this->matches['data']) && !empty($this->matches['data'])) { return $this->matches['data']; } return null; } }; } } image/src/Drivers/SpecializableDecoder.php000064400000001323152665633650014621 0ustar00encode($this); } /** * Build new file pointer, run callback with it and return result as encoded image * * @throws RuntimeException */ protected function createEncodedImage(callable $callback, ?string $mediaType = null): EncodedImage { $pointer = $this->buildFilePointer(); $callback($pointer); return is_string($mediaType) ? new EncodedImage($pointer, $mediaType) : new EncodedImage($pointer); } } image/src/Drivers/Gd/Analyzers/PixelColorsAnalyzer.php000064400000001201152665633650017032 0ustar00colorspace(); foreach ($image as $frame) { $colors->push( parent::colorAt($colorspace, $frame->native()) ); } return $colors; } } image/src/Drivers/Gd/Analyzers/PixelColorAnalyzer.php000064400000002716152665633650016663 0ustar00colorAt( $image->colorspace(), $image->core()->frame($this->frame_key)->native() ); } /** * @throws GeometryException * @throws ColorException */ protected function colorAt(ColorspaceInterface $colorspace, GdImage $gd): ColorInterface { $index = @imagecolorat($gd, $this->x, $this->y); if (!imageistruecolor($gd)) { $index = imagecolorsforindex($gd, $index); } if ($index === false) { throw new GeometryException( 'The specified position is not in the valid image area.' ); } return $this->driver()->colorProcessor($colorspace)->nativeToColor($index); } } image/src/Drivers/Gd/Analyzers/ResolutionAnalyzer.php000064400000001174152665633650016743 0ustar00core()->native())); } } image/src/Drivers/Gd/Analyzers/ColorspaceAnalyzer.php000064400000001133152665633650016665 0ustar00core()->native()); } } image/src/Drivers/Gd/Analyzers/HeightAnalyzer.php000064400000001056152665633650016007 0ustar00core()->native()); } } image/src/Drivers/Gd/Core.php000064400000004261152665633650012012 0ustar00push($frame); return $this; } /** * {@inheritdoc} * * @see CoreInterface::native() */ public function native(): mixed { return $this->first()->native(); } /** * {@inheritdoc} * * @see CoreInterface::setNative() */ public function setNative(mixed $native): self { $this->empty()->push(new Frame($native)); return $this; } /** * {@inheritdoc} * * @see CoreInterface::frame() */ public function frame(int $position): FrameInterface { $frame = $this->getAtPosition($position); if (!($frame instanceof FrameInterface)) { throw new AnimationException('Frame #' . $position . ' could not be found in the image.'); } return $frame; } /** * {@inheritdoc} * * @see CoreInterface::loops() */ public function loops(): int { return $this->loops; } /** * {@inheritdoc} * * @see CoreInterface::setLoops() */ public function setLoops(int $loops): self { $this->loops = $loops; return $this; } /** * {@inheritdoc} * * @see CollectionInterface::first() */ public function first(): FrameInterface { return parent::first(); } /** * {@inheritdoc} * * @see CollectionInterface::last() */ public function last(): FrameInterface { return parent::last(); } /** * Clone instance */ public function __clone(): void { foreach ($this->items as $key => $frame) { $this->items[$key] = clone $frame; } } } image/src/Drivers/Gd/Cloner.php000064400000005536152665633650012352 0ustar00width(), $size->height()); // copy resolution to clone $resolution = imageresolution($gd); if (is_array($resolution) && array_key_exists(0, $resolution) && array_key_exists(1, $resolution)) { imageresolution($clone, $resolution[0], $resolution[1]); } // fill with background $processor = new ColorProcessor(); imagefill($clone, 0, 0, $processor->colorToNative($background)); imagealphablending($clone, true); imagesavealpha($clone, true); // set background image as transparent if alpha channel value if color is below .5 // comes into effect when the end format only supports binary transparency (like GIF) if ($background->channel(Alpha::class)->value() < 128) { imagecolortransparent($clone, $processor->colorToNative($background)); } return $clone; } /** * Create a clone of an GdImage that is positioned on the specified background color. * Possible transparent areas are mixed with this color. * * @throws ColorException */ public static function cloneBlended(GdImage $gd, ColorInterface $background): GdImage { // create empty canvas with same size $clone = static::cloneEmpty($gd, background: $background); // transfer actual image to clone imagecopy($clone, $gd, 0, 0, 0, 0, imagesx($gd), imagesy($gd)); return $clone; } } image/src/Drivers/Gd/Modifiers/PixelateModifier.php000064400000001243152665633650016272 0ustar00native(), IMG_FILTER_PIXELATE, $this->size, true); } return $image; } } image/src/Drivers/Gd/Modifiers/AlignRotationModifier.php000064400000003001152665633650017263 0ustar00exif('IFD0.Orientation')) { 2 => $image->flop(), 3 => $image->rotate(180), 4 => $image->rotate(180)->flop(), 5 => $image->rotate(270)->flop(), 6 => $image->rotate(270), 7 => $image->rotate(90)->flop(), 8 => $image->rotate(90), default => $image }; return $this->markAligned($image); } /** * Set exif data of image to top-left orientation, marking the image as * aligned and making sure the rotation correction process is not * performed again. */ private function markAligned(ImageInterface $image): ImageInterface { $exif = $image->exif()->map(function ($item) { if (is_array($item) && array_key_exists('Orientation', $item)) { $item['Orientation'] = 1; return $item; } return $item; }); return $image->setExif($exif); } } image/src/Drivers/Gd/Modifiers/ContainModifier.php000064400000005657152665633650016127 0ustar00getCropSize($image); $resize = $this->getResizeSize($image); $background = $this->driver()->handleInput($this->background); $blendingColor = $this->driver()->handleInput( $this->driver()->config()->blendingColor ); foreach ($image as $frame) { $this->modify($frame, $crop, $resize, $background, $blendingColor); } return $image; } /** * @throws ColorException */ protected function modify( FrameInterface $frame, SizeInterface $crop, SizeInterface $resize, ColorInterface $background, ColorInterface $blendingColor ): void { // create new gd image $modified = Cloner::cloneEmpty($frame->native(), $resize, $background); // make image area transparent to keep transparency // even if background-color is set $transparent = imagecolorallocatealpha( $modified, $blendingColor->channel(Red::class)->value(), $blendingColor->channel(Green::class)->value(), $blendingColor->channel(Blue::class)->value(), 127, ); imagealphablending($modified, false); // do not blend / just overwrite imagecolortransparent($modified, $transparent); imagefilledrectangle( $modified, $crop->pivot()->x(), $crop->pivot()->y(), $crop->pivot()->x() + $crop->width() - 1, $crop->pivot()->y() + $crop->height() - 1, $transparent ); // copy image from original with blending alpha imagealphablending($modified, true); imagecopyresampled( $modified, $frame->native(), $crop->pivot()->x(), $crop->pivot()->y(), 0, 0, $crop->width(), $crop->height(), $frame->size()->width(), $frame->size()->height() ); // set new content as resource $frame->setNative($modified); } } image/src/Drivers/Gd/Modifiers/SliceAnimationModifier.php000064400000001472152665633650017422 0ustar00offset >= $image->count()) { throw new AnimationException('Offset is not in the range of frames.'); } $image->core()->slice($this->offset, $this->length); return $image; } } image/src/Drivers/Gd/Modifiers/ResolutionModifier.php000064400000001334152665633650016663 0ustar00x)); $y = intval(round($this->y)); foreach ($image as $frame) { imageresolution($frame->native(), $x, $y); } return $image; } } image/src/Drivers/Gd/Modifiers/CoverDownModifier.php000064400000000660152665633650016427 0ustar00resizeDown($this->width, $this->height); } } image/src/Drivers/Gd/Modifiers/GreyscaleModifier.php000064400000001225152665633650016435 0ustar00native(), IMG_FILTER_GRAYSCALE); } return $image; } } image/src/Drivers/Gd/Modifiers/FlipModifier.php000064400000001174152665633650015414 0ustar00native(), IMG_FLIP_VERTICAL); } return $image; } } image/src/Drivers/Gd/Modifiers/ResizeCanvasModifier.php000064400000001476152665633650017124 0ustar00cropSize($image); $image->modify(new CropModifier( $cropSize->width(), $cropSize->height(), $cropSize->pivot()->x(), $cropSize->pivot()->y(), $this->background, )); return $image; } } image/src/Drivers/Gd/Modifiers/BlurModifier.php000064400000001314152665633650015422 0ustar00amount; $i++) { imagefilter($frame->native(), IMG_FILTER_GAUSSIAN_BLUR); } } return $image; } } image/src/Drivers/Gd/Modifiers/ContrastModifier.php000064400000001245152665633650016316 0ustar00native(), IMG_FILTER_CONTRAST, ($this->level * -1)); } return $image; } } image/src/Drivers/Gd/Modifiers/DrawPixelModifier.php000064400000001725152665633650016423 0ustar00driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->color) ); foreach ($image as $frame) { imagealphablending($frame->native(), true); imagesetpixel( $frame->native(), $this->position->x(), $this->position->y(), $color ); } return $image; } } image/src/Drivers/Gd/Modifiers/RemoveAnimationModifier.php000064400000001236152665633650017616 0ustar00core()->setNative( $this->selectedFrame($image)->native() ); return $image; } } image/src/Drivers/Gd/Modifiers/ScaleDownModifier.php000064400000000746152665633650016405 0ustar00size()->scaleDown($this->width, $this->height); } } image/src/Drivers/Gd/Modifiers/ScaleModifier.php000064400000000604152665633650015546 0ustar00size()->scale($this->width, $this->height); } } image/src/Drivers/Gd/Modifiers/ProfileRemovalModifier.php000064400000001257152665633650017452 0ustar00size() ->containMax( $this->width, $this->height ) ->alignPivotTo( $this->getResizeSize($image), $this->position ); } } image/src/Drivers/Gd/Modifiers/RotateModifier.php000064400000006335152665633650015764 0ustar00driver()->handleInput($this->background); foreach ($image as $frame) { $this->modifyFrame($frame, $background); } return $image; } /** * Apply rotation modification on given frame, given background * color is used for newly create image areas * * @throws ColorException */ protected function modifyFrame(FrameInterface $frame, ColorInterface $background): void { // get transparent color from frame core $transparent = match ($transparent = imagecolortransparent($frame->native())) { -1 => imagecolorallocatealpha( $frame->native(), $background->channel(Red::class)->value(), $background->channel(Green::class)->value(), $background->channel(Blue::class)->value(), 127 ), default => $transparent, }; // rotate original image against transparent background $rotated = imagerotate( $frame->native(), $this->rotationAngle(), $transparent ); // create size from original after rotation $container = (new Rectangle( imagesx($rotated), imagesy($rotated), ))->movePivot('center'); // create size from original and rotate points $cutout = (new Rectangle( imagesx($frame->native()), imagesy($frame->native()), $container->pivot() ))->align('center') ->valign('center') ->rotate($this->rotationAngle() * -1); // create new gd image $modified = Cloner::cloneEmpty($frame->native(), $container, $background); // draw the cutout on new gd image to have a transparent // background where the rotated image will be placed imagealphablending($modified, false); imagefilledpolygon( $modified, $cutout->toArray(), imagecolortransparent($modified) ); // place rotated image on new gd image imagealphablending($modified, true); imagecopy( $modified, $rotated, 0, 0, 0, 0, imagesx($rotated), imagesy($rotated) ); $frame->setNative($modified); } } image/src/Drivers/Gd/Modifiers/DrawPolygonModifier.php000064400000003171152665633650016766 0ustar00drawable->hasBackgroundColor()) { imagealphablending($frame->native(), true); imagesetthickness($frame->native(), 0); imagefilledpolygon( $frame->native(), $this->drawable->toArray(), $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ) ); } if ($this->drawable->hasBorder()) { imagealphablending($frame->native(), true); imagesetthickness($frame->native(), $this->drawable->borderSize()); imagepolygon( $frame->native(), $this->drawable->toArray(), $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->borderColor() ) ); } } return $image; } } image/src/Drivers/Gd/Modifiers/FlopModifier.php000064400000001176152665633650015424 0ustar00native(), IMG_FLIP_HORIZONTAL); } return $image; } } image/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php000064400000001767152665633650020150 0ustar00blendingColor($this->driver()); foreach ($image as $frame) { // create new canvas with blending color as background $modified = Cloner::cloneBlended( $frame->native(), background: $blendingColor ); // set new gd image $frame->setNative($modified); } return $image; } } image/src/Drivers/Gd/Modifiers/TrimModifier.php000064400000005500152665633650015432 0ustar00isAnimated()) { throw new NotSupportedException('Trim modifier cannot be applied to animated images.'); } // apply tolerance with a min. value of .5 because the default tolerance of '0' should // already trim away similar colors which is not the case with imagecropauto. $trimmed = imagecropauto( $image->core()->native(), IMG_CROP_THRESHOLD, max([.5, $this->tolerance / 10]), $this->trimColor($image) ); // if the tolerance is very high, it is possible that no image is left. // imagick returns a 1x1 pixel image in this case. this does the same. if ($trimmed === false) { $trimmed = $this->driver()->createImage(1, 1)->core()->native(); } $image->core()->setNative($trimmed); return $image; } /** * Create an average color from the colors of the four corner points of the given image * * @throws RuntimeException * @throws AnimationException */ private function trimColor(ImageInterface $image): int { // trim color base $red = 0; $green = 0; $blue = 0; // corner coordinates $size = $image->size(); $cornerPoints = [ new Point(0, 0), new Point($size->width() - 1, 0), new Point(0, $size->height() - 1), new Point($size->width() - 1, $size->height() - 1), ]; // create an average color to be used in trim operation foreach ($cornerPoints as $pos) { $cornerColor = imagecolorat($image->core()->native(), $pos->x(), $pos->y()); $rgb = imagecolorsforindex($image->core()->native(), $cornerColor); $red += round(round(($rgb['red'] / 51)) * 51); $green += round(round(($rgb['green'] / 51)) * 51); $blue += round(round(($rgb['blue'] / 51)) * 51); } $red = (int) round($red / 4); $green = (int) round($green / 4); $blue = (int) round($blue / 4); return imagecolorallocate($image->core()->native(), $red, $green, $blue); } } image/src/Drivers/Gd/Modifiers/TextModifier.php000064400000010332152665633650015442 0ustar00driver()->fontProcessor(); $lines = $fontProcessor->textBlock($this->text, $this->font, $this->position); // decode text colors $textColor = $this->gdTextColor($image); $strokeColor = $this->gdStrokeColor($image); foreach ($image as $frame) { imagealphablending($frame->native(), true); if ($this->font->hasFilename()) { foreach ($lines as $line) { foreach ($this->strokeOffsets($this->font) as $offset) { imagettftext( $frame->native(), $fontProcessor->nativeFontSize($this->font), $this->font->angle() * -1, $line->position()->x() + $offset->x(), $line->position()->y() + $offset->y(), $strokeColor, $this->font->filename(), (string) $line ); } imagettftext( $frame->native(), $fontProcessor->nativeFontSize($this->font), $this->font->angle() * -1, $line->position()->x(), $line->position()->y(), $textColor, $this->font->filename(), (string) $line ); } } else { foreach ($lines as $line) { foreach ($this->strokeOffsets($this->font) as $offset) { imagestring( $frame->native(), $this->gdFont(), $line->position()->x() + $offset->x(), $line->position()->y() + $offset->y(), (string) $line, $strokeColor ); } imagestring( $frame->native(), $this->gdFont(), $line->position()->x(), $line->position()->y(), (string) $line, $textColor ); } } } return $image; } /** * Decode text color in GD compatible format * * @throws RuntimeException * @throws ColorException */ protected function gdTextColor(ImageInterface $image): int { return $this ->driver() ->colorProcessor($image->colorspace()) ->colorToNative(parent::textColor()); } /** * Decode color for stroke (outline) effect in GD compatible format * * @throws RuntimeException * @throws ColorException */ protected function gdStrokeColor(ImageInterface $image): int { if (!$this->font->hasStrokeEffect()) { return 0; } $color = parent::strokeColor(); if ($color->isTransparent()) { throw new ColorException( 'The stroke color must be fully opaque.' ); } return $this ->driver() ->colorProcessor($image->colorspace()) ->colorToNative($color); } /** * Return GD's internal font size (if no ttf file is set) */ private function gdFont(): int { if (is_numeric($this->font->filename())) { return intval($this->font->filename()); } return 1; } } image/src/Drivers/Gd/Modifiers/DrawEllipseModifier.php000064400000005371152665633650016740 0ustar00drawable->hasBorder()) { imagealphablending($frame->native(), true); // slightly smaller ellipse to keep 1px bordered edges clean if ($this->drawable->hasBackgroundColor()) { imagefilledellipse( $frame->native(), $this->drawable()->position()->x(), $this->drawable->position()->y(), $this->drawable->width() - 1, $this->drawable->height() - 1, $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ) ); } // gd's imageellipse ignores imagesetthickness // so i use imagearc with 360 degrees instead. imagesetthickness( $frame->native(), $this->drawable->borderSize(), ); imagearc( $frame->native(), $this->drawable()->position()->x(), $this->drawable()->position()->y(), $this->drawable->width(), $this->drawable->height(), 0, 360, $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->borderColor() ) ); } else { imagealphablending($frame->native(), true); imagesetthickness($frame->native(), 0); imagefilledellipse( $frame->native(), $this->drawable()->position()->x(), $this->drawable()->position()->y(), $this->drawable->width(), $this->drawable->height(), $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ) ); } } return $image; } } image/src/Drivers/Gd/Modifiers/ColorspaceModifier.php000064400000001555152665633650016617 0ustar00targetColorspace() instanceof RgbColorspace)) { throw new NotSupportedException( 'Only RGB colorspace is supported by GD driver.' ); } return $image; } } image/src/Drivers/Gd/Modifiers/ResizeModifier.php000064400000003711152665633650015762 0ustar00getAdjustedSize($image); foreach ($image as $frame) { $this->resizeFrame($frame, $resizeTo); } return $image; } /** * @throws ColorException */ private function resizeFrame(FrameInterface $frame, SizeInterface $resizeTo): void { // create empty canvas in target size $modified = Cloner::cloneEmpty($frame->native(), $resizeTo); // copy content from resource imagecopyresampled( $modified, $frame->native(), $resizeTo->pivot()->x(), $resizeTo->pivot()->y(), 0, 0, $resizeTo->width(), $resizeTo->height(), $frame->size()->width(), $frame->size()->height() ); // set new content as resource $frame->setNative($modified); } /** * Return the size the modifier will resize to * * @throws RuntimeException * @throws GeometryException */ protected function getAdjustedSize(ImageInterface $image): SizeInterface { return $image->size()->resize($this->width, $this->height); } } image/src/Drivers/Gd/Modifiers/DrawBezierModifier.php000064400000022027152665633650016560 0ustar00drawable->count() !== 3 && $this->drawable->count() !== 4) { throw new GeometryException('You must specify either 3 or 4 points to create a bezier curve'); } [$polygon, $polygon_border_segments] = $this->calculateBezierPoints(); if ($this->drawable->hasBackgroundColor() || $this->drawable->hasBorder()) { imagealphablending($frame->native(), true); imageantialias($frame->native(), true); } if ($this->drawable->hasBackgroundColor()) { $background_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ); imagesetthickness($frame->native(), 0); imagefilledpolygon( $frame->native(), $polygon, $background_color ); } if ($this->drawable->hasBorder() && $this->drawable->borderSize() > 0) { $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->borderColor() ); if ($this->drawable->borderSize() === 1) { imagesetthickness($frame->native(), $this->drawable->borderSize()); $count = count($polygon); for ($i = 0; $i < $count; $i += 2) { if (array_key_exists($i + 2, $polygon) && array_key_exists($i + 3, $polygon)) { imageline( $frame->native(), $polygon[$i], $polygon[$i + 1], $polygon[$i + 2], $polygon[$i + 3], $border_color ); } } } else { $polygon_border_segments_total = count($polygon_border_segments); for ($i = 0; $i < $polygon_border_segments_total; $i += 1) { imagefilledpolygon( $frame->native(), $polygon_border_segments[$i], $border_color ); } } } } return $image; } /** * Calculate interpolation points for quadratic beziers using the Bernstein polynomial form * * @return array{'x': float, 'y': float} */ private function calculateQuadraticBezierInterpolationPoint(float $t = 0.05): array { $remainder = 1 - $t; $control_point_1_multiplier = $remainder * $remainder; $control_point_2_multiplier = $remainder * $t * 2; $control_point_3_multiplier = $t * $t; $x = ( $this->drawable->first()->x() * $control_point_1_multiplier + $this->drawable->second()->x() * $control_point_2_multiplier + $this->drawable->last()->x() * $control_point_3_multiplier ); $y = ( $this->drawable->first()->y() * $control_point_1_multiplier + $this->drawable->second()->y() * $control_point_2_multiplier + $this->drawable->last()->y() * $control_point_3_multiplier ); return ['x' => $x, 'y' => $y]; } /** * Calculate interpolation points for cubic beziers using the Bernstein polynomial form * * @return array{'x': float, 'y': float} */ private function calculateCubicBezierInterpolationPoint(float $t = 0.05): array { $remainder = 1 - $t; $t_squared = $t * $t; $remainder_squared = $remainder * $remainder; $control_point_1_multiplier = $remainder_squared * $remainder; $control_point_2_multiplier = $remainder_squared * $t * 3; $control_point_3_multiplier = $t_squared * $remainder * 3; $control_point_4_multiplier = $t_squared * $t; $x = ( $this->drawable->first()->x() * $control_point_1_multiplier + $this->drawable->second()->x() * $control_point_2_multiplier + $this->drawable->third()->x() * $control_point_3_multiplier + $this->drawable->last()->x() * $control_point_4_multiplier ); $y = ( $this->drawable->first()->y() * $control_point_1_multiplier + $this->drawable->second()->y() * $control_point_2_multiplier + $this->drawable->third()->y() * $control_point_3_multiplier + $this->drawable->last()->y() * $control_point_4_multiplier ); return ['x' => $x, 'y' => $y]; } /** * Calculate the points needed to draw a quadratic or cubic bezier with optional border/stroke * * @throws GeometryException * @return array{0: array, 1: array} */ private function calculateBezierPoints(): array { if ($this->drawable->count() !== 3 && $this->drawable->count() !== 4) { throw new GeometryException('You must specify either 3 or 4 points to create a bezier curve'); } $polygon = []; $inner_polygon = []; $outer_polygon = []; $polygon_border_segments = []; // define ratio t; equivalent to 5 percent distance along edge $t = 0.05; $polygon[] = $this->drawable->first()->x(); $polygon[] = $this->drawable->first()->y(); for ($i = $t; $i < 1; $i += $t) { if ($this->drawable->count() === 3) { $ip = $this->calculateQuadraticBezierInterpolationPoint($i); } elseif ($this->drawable->count() === 4) { $ip = $this->calculateCubicBezierInterpolationPoint($i); } $polygon[] = (int) $ip['x']; $polygon[] = (int) $ip['y']; } $polygon[] = $this->drawable->last()->x(); $polygon[] = $this->drawable->last()->y(); if ($this->drawable->hasBorder() && $this->drawable->borderSize() > 1) { // create the border/stroke effect by calculating two new curves with offset positions // from the main polygon and then connecting the inner/outer curves to create separate // 4-point polygon segments $polygon_total_points = count($polygon); $offset = ($this->drawable->borderSize() / 2); for ($i = 0; $i < $polygon_total_points; $i += 2) { if (array_key_exists($i + 2, $polygon) && array_key_exists($i + 3, $polygon)) { $dx = $polygon[$i + 2] - $polygon[$i]; $dy = $polygon[$i + 3] - $polygon[$i + 1]; $dxy_sqrt = ($dx * $dx + $dy * $dy) ** 0.5; // inner polygon $scale = $offset / $dxy_sqrt; $ox = -$dy * $scale; $oy = $dx * $scale; $inner_polygon[] = $ox + $polygon[$i]; $inner_polygon[] = $oy + $polygon[$i + 1]; $inner_polygon[] = $ox + $polygon[$i + 2]; $inner_polygon[] = $oy + $polygon[$i + 3]; // outer polygon $scale = -$offset / $dxy_sqrt; $ox = -$dy * $scale; $oy = $dx * $scale; $outer_polygon[] = $ox + $polygon[$i]; $outer_polygon[] = $oy + $polygon[$i + 1]; $outer_polygon[] = $ox + $polygon[$i + 2]; $outer_polygon[] = $oy + $polygon[$i + 3]; } } $inner_polygon_total_points = count($inner_polygon); for ($i = 0; $i < $inner_polygon_total_points; $i += 2) { if (array_key_exists($i + 2, $inner_polygon) && array_key_exists($i + 3, $inner_polygon)) { $polygon_border_segments[] = [ $inner_polygon[$i], $inner_polygon[$i + 1], $outer_polygon[$i], $outer_polygon[$i + 1], $outer_polygon[$i + 2], $outer_polygon[$i + 3], $inner_polygon[$i + 2], $inner_polygon[$i + 3], ]; } } } return [$polygon, $polygon_border_segments]; } } image/src/Drivers/Gd/Modifiers/ResizeDownModifier.php000064400000000616152665633650016613 0ustar00size()->resizeDown($this->width, $this->height); } } image/src/Drivers/Gd/Modifiers/DrawLineModifier.php000064400000002231152665633650016222 0ustar00driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ); foreach ($image as $frame) { imagealphablending($frame->native(), true); imageantialias($frame->native(), true); imagesetthickness($frame->native(), $this->drawable->width()); imageline( $frame->native(), $this->drawable->start()->x(), $this->drawable->start()->y(), $this->drawable->end()->x(), $this->drawable->end()->y(), $color ); } return $image; } } image/src/Drivers/Gd/Modifiers/InvertModifier.php000064400000001206152665633650015765 0ustar00native(), IMG_FILTER_NEGATE); } return $image; } } image/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php000064400000004240152665633650017501 0ustar00limit <= 0) { throw new InputException('Quantization limit must be greater than 0.'); } // no color reduction if the limit is higher than the colors in the img $colorCount = imagecolorstotal($image->core()->native()); if ($colorCount > 0 && $this->limit > $colorCount) { return $image; } $width = $image->width(); $height = $image->height(); $background = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->background) ); $blendingColor = $this->driver()->handleInput( $this->driver()->config()->blendingColor ); foreach ($image as $frame) { // create new image for color quantization $reduced = Cloner::cloneEmpty($frame->native(), background: $blendingColor); // fill with background imagefill($reduced, 0, 0, $background); // set transparency imagecolortransparent($reduced, $background); // copy original image (colors are limited automatically in the copy process) imagecopy($reduced, $frame->native(), 0, 0, 0, 0, $width, $height); // gd library does not support color quantization directly therefore the // colors are decrease by transforming the image to a palette version imagetruecolortopalette($reduced, true, $this->limit); $frame->setNative($reduced); } return $image; } } image/src/Drivers/Gd/Modifiers/DrawRectangleModifier.php000064400000004033152665633650017241 0ustar00drawable->position(); foreach ($image as $frame) { // draw background if ($this->drawable->hasBackgroundColor()) { imagealphablending($frame->native(), true); imagesetthickness($frame->native(), 0); imagefilledrectangle( $frame->native(), $position->x(), $position->y(), $position->x() + $this->drawable->width(), $position->y() + $this->drawable->height(), $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->backgroundColor() ) ); } // draw border if ($this->drawable->hasBorder()) { imagealphablending($frame->native(), true); imagesetthickness($frame->native(), $this->drawable->borderSize()); imagerectangle( $frame->native(), $position->x(), $position->y(), $position->x() + $this->drawable->width(), $position->y() + $this->drawable->height(), $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->borderColor() ) ); } } return $image; } } image/src/Drivers/Gd/Modifiers/CoverModifier.php000064400000003103152665633650015572 0ustar00getCropSize($image); $resize = $this->getResizeSize($crop); foreach ($image as $frame) { $this->modifyFrame($frame, $crop, $resize); } return $image; } /** * @throws ColorException */ protected function modifyFrame(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void { // create new image $modified = Cloner::cloneEmpty($frame->native(), $resize); // copy content from resource imagecopyresampled( $modified, $frame->native(), 0, 0, $crop->pivot()->x(), $crop->pivot()->y(), $resize->width(), $resize->height(), $crop->width(), $crop->height() ); // set new content as resource $frame->setNative($modified); } } image/src/Drivers/Gd/Modifiers/ResizeCanvasRelativeModifier.php000064400000000626152665633650020614 0ustar00color($image); foreach ($image as $frame) { if ($this->hasPosition()) { $this->floodFillWithColor($frame, $color); } else { $this->fillAllWithColor($frame, $color); } } return $image; } /** * @throws RuntimeException */ private function color(ImageInterface $image): int { return $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->color) ); } private function floodFillWithColor(FrameInterface $frame, int $color): void { imagefill( $frame->native(), $this->position->x(), $this->position->y(), $color ); } private function fillAllWithColor(FrameInterface $frame, int $color): void { imagealphablending($frame->native(), true); imagefilledrectangle( $frame->native(), 0, 0, $frame->size()->width() - 1, $frame->size()->height() - 1, $color ); } } image/src/Drivers/Gd/Modifiers/BrightnessModifier.php000064400000001267152665633650016635 0ustar00native(), IMG_FILTER_BRIGHTNESS, intval($this->level * 2.55)); } return $image; } } image/src/Drivers/Gd/Modifiers/SharpenModifier.php000064400000002144152665633650016120 0ustar00matrix(); foreach ($image as $frame) { imageconvolution($frame->native(), $matrix, 1, 0); } return $image; } /** * Create matrix to be used by imageconvolution() * * @return array> */ private function matrix(): array { $min = $this->amount >= 10 ? $this->amount * -0.01 : 0; $max = $this->amount * -0.025; $abs = ((4 * $min + 4 * $max) * -1) + 1; return [ [$min, $max, $min], [$max, $abs, $max], [$min, $max, $min] ]; } } image/src/Drivers/Gd/Modifiers/ColorizeModifier.php000064400000001536152665633650016312 0ustar00red * 2.55); $green = (int) round($this->green * 2.55); $blue = (int) round($this->blue * 2.55); foreach ($image as $frame) { imagefilter($frame->native(), IMG_FILTER_COLORIZE, $red, $green, $blue); } return $image; } } image/src/Drivers/Gd/Modifiers/CropModifier.php000064400000004712152665633650015426 0ustar00size(); $crop = $this->crop($image); $background = $this->driver()->handleInput($this->background); foreach ($image as $frame) { $this->cropFrame($frame, $originalSize, $crop, $background); } return $image; } /** * @throws ColorException */ protected function cropFrame( FrameInterface $frame, SizeInterface $originalSize, SizeInterface $resizeTo, ColorInterface $background ): void { // create new image with transparent background $modified = Cloner::cloneEmpty($frame->native(), $resizeTo, $background); // define offset $offset_x = $resizeTo->pivot()->x() + $this->offset_x; $offset_y = $resizeTo->pivot()->y() + $this->offset_y; // define target width & height $targetWidth = min($resizeTo->width(), $originalSize->width()); $targetHeight = min($resizeTo->height(), $originalSize->height()); $targetWidth = $targetWidth < $originalSize->width() ? $targetWidth + $offset_x : $targetWidth; $targetHeight = $targetHeight < $originalSize->height() ? $targetHeight + $offset_y : $targetHeight; // don't alpha blend for copy operation to keep transparent areas of original image imagealphablending($modified, false); // copy content from resource imagecopyresampled( $modified, $frame->native(), $offset_x * -1, $offset_y * -1, 0, 0, $targetWidth, $targetHeight, $targetWidth, $targetHeight ); // set new content as resource $frame->setNative($modified); } } image/src/Drivers/Gd/Modifiers/PlaceModifier.php000064400000006330152665633650015545 0ustar00driver()->handleInput($this->element); $position = $this->getPosition($image, $watermark); foreach ($image as $frame) { imagealphablending($frame->native(), true); if ($this->opacity === 100) { $this->placeOpaque($frame, $watermark, $position); } else { $this->placeTransparent($frame, $watermark, $position); } } return $image; } /** * Insert watermark with 100% opacity * * @throws RuntimeException */ private function placeOpaque(FrameInterface $frame, ImageInterface $watermark, PointInterface $position): void { imagecopy( $frame->native(), $watermark->core()->native(), $position->x(), $position->y(), 0, 0, $watermark->width(), $watermark->height() ); } /** * Insert watermark transparent with current opacity * * Unfortunately, the original PHP function imagecopymerge does not work reliably. * For example, any transparency of the image to be inserted is not applied correctly. * For this reason, a new GDImage is created into which the original image is inserted * in the first step and the watermark is inserted with 100% opacity in the second * step. This combination is then transferred to the original image again with the * respective opacity. * * Please note: Unfortunately, there is still an edge case, when a transparent image * is placed on a transparent background, the "double" transparent areas appear opaque! * * @throws RuntimeException */ private function placeTransparent(FrameInterface $frame, ImageInterface $watermark, PointInterface $position): void { $cut = imagecreatetruecolor($watermark->width(), $watermark->height()); imagecopy( $cut, $frame->native(), 0, 0, $position->x(), $position->y(), imagesx($cut), imagesy($cut) ); imagecopy( $cut, $watermark->core()->native(), 0, 0, 0, 0, imagesx($cut), imagesy($cut) ); imagecopymerge( $frame->native(), $cut, $position->x(), $position->y(), 0, 0, $watermark->width(), $watermark->height(), $this->opacity ); } } image/src/Drivers/Gd/Modifiers/ProfileModifier.php000064400000001257152665633650016124 0ustar00native(), 1, $this->gamma); } return $image; } } image/src/Drivers/Gd/Decoders/EncodedImageObjectDecoder.php000064400000001252152665633650017570 0ustar00toString()); } } image/src/Drivers/Gd/Decoders/SplFileInfoImageDecoder.php000064400000001350152665633650017251 0ustar00getRealPath()); } } image/src/Drivers/Gd/Decoders/FilePathImageDecoder.php000064400000004006152665633650016574 0ustar00isFile($input)) { throw new DecoderException('Unable to decode input'); } // detect media (mime) type $mediaType = $this->getMediaTypeByFilePath($input); $image = match ($mediaType->format()) { // gif files might be animated and therefore cannot // be handled by the standard GD decoder. Format::GIF => $this->decodeGif($input), default => parent::decode(match ($mediaType->format()) { Format::JPEG => @imagecreatefromjpeg($input), Format::WEBP => @imagecreatefromwebp($input), Format::PNG => @imagecreatefrompng($input), Format::AVIF => @imagecreatefromavif($input), Format::BMP => @imagecreatefrombmp($input), default => throw new DecoderException('Unable to decode input'), }), }; // set file path & mediaType on origin $image->origin()->setFilePath($input); $image->origin()->setMediaType($mediaType); // extract exif for the appropriate formats if ($mediaType->format() === Format::JPEG) { $image->setExif($this->extractExifData($input)); } // adjust image orientation if ($this->driver()->config()->autoOrientation) { $image->modify(new AlignRotationModifier()); } return $image; } } image/src/Drivers/Gd/Decoders/Base64ImageDecoder.php000064400000001325152665633650016125 0ustar00isValidBase64($input)) { throw new DecoderException('Unable to decode input'); } return parent::decode(base64_decode((string) $input)); } } image/src/Drivers/Gd/Decoders/DataUriImageDecoder.php000064400000001720152665633650016431 0ustar00parseDataUri($input); if (!$uri->isValid()) { throw new DecoderException('Unable to decode input'); } if ($uri->isBase64Encoded()) { return parent::decode(base64_decode($uri->data())); } return parent::decode(urldecode($uri->data())); } } image/src/Drivers/Gd/Decoders/AbstractDecoder.php000064400000002322152665633650015677 0ustar00isGifFormat($input)) { true => $this->decodeGif($input), default => $this->decodeBinary($input), }; } /** * Decode image from given binary data * * @throws RuntimeException */ private function decodeBinary(string $input): ImageInterface { $gd = @imagecreatefromstring($input); if ($gd === false) { throw new DecoderException('Unable to decode input'); } // create image instance $image = parent::decode($gd); // get media type $mediaType = $this->getMediaTypeByBinary($input); // extract & set exif data for appropriate formats if (in_array($mediaType->format(), [Format::JPEG, Format::TIFF])) { $image->setExif($this->extractExifData($input)); } // set mediaType on origin $image->origin()->setMediaType($mediaType); // adjust image orientation if ($this->driver()->config()->autoOrientation) { $image->modify(new AlignRotationModifier()); } return $image; } } image/src/Drivers/Gd/Decoders/FilePointerImageDecoder.php000064400000001462152665633650017323 0ustar00driver(), new Core([ new Frame($input) ]) ); } /** * Decode image from given GIF source which can be either a file path or binary data * * Depending on the configuration, this is taken over by the native GD function * or, if animations are required, by our own extended decoder. * * @throws RuntimeException */ protected function decodeGif(mixed $input): ImageInterface { // create non-animated image depending on config if (!$this->driver()->config()->decodeAnimation) { $native = match (true) { $this->isGifFormat($input) => @imagecreatefromstring($input), default => @imagecreatefromgif($input), }; if ($native === false) { throw new DecoderException('Unable to decode input.'); } $image = self::decode($native); $image->origin()->setMediaType('image/gif'); return $image; } try { // create empty core $core = new Core(); $gif = GifDecoder::decode($input); $splitter = GifSplitter::create($gif)->split(); $delays = $splitter->getDelays(); // set loops on core if ($loops = $gif->getMainApplicationExtension()?->getLoops()) { $core->setLoops($loops); } // add GDImage instances to core foreach ($splitter->coalesceToResources() as $key => $native) { $core->push( new Frame($native, $delays[$key] / 100) ); } } catch (Exception $e) { throw new DecoderException($e->getMessage(), $e->getCode(), $e); } // create (possibly) animated image $image = new Image($this->driver(), $core); // set media type $image->origin()->setMediaType('image/gif'); return $image; } } image/src/Drivers/Gd/Driver.php000064400000010213152665633650012347 0ustar00core->add( $this->driver->handleInput($source)->core()->first()->setDelay($delay) ); return $this; } /** * @throws RuntimeException */ public function __invoke(): ImageInterface { return new Image( $this->driver, $this->core ); } }; $init($animation); return call_user_func($animation); } /** * {@inheritdoc} * * @see DriverInterface::colorProcessor() */ public function colorProcessor(ColorspaceInterface $colorspace): ColorProcessorInterface { return new ColorProcessor($colorspace); } /** * {@inheritdoc} * * @see DriverInterface::fontProcessor() */ public function fontProcessor(): FontProcessorInterface { return new FontProcessor(); } /** * {@inheritdoc} * * @see DriverInterface::supports() */ public function supports(string|Format|FileExtension|MediaType $identifier): bool { return match (Format::tryCreate($identifier)) { Format::JPEG => boolval(imagetypes() & IMG_JPEG), Format::WEBP => boolval(imagetypes() & IMG_WEBP), Format::GIF => boolval(imagetypes() & IMG_GIF), Format::PNG => boolval(imagetypes() & IMG_PNG), Format::AVIF => boolval(imagetypes() & IMG_AVIF), Format::BMP => boolval(imagetypes() & IMG_BMP), default => false, }; } /** * Return version of GD library */ public static function version(): string { return gd_info()['GD Version']; } } image/src/Drivers/Gd/FontProcessor.php000064400000005072152665633650013731 0ustar00hasFilename()) { // calculate box size from gd font $box = new Rectangle(0, 0); $chars = mb_strlen($text); if ($chars > 0) { $box->setWidth( $chars * $this->gdCharacterWidth((int) $font->filename()) ); $box->setHeight( $this->gdCharacterHeight((int) $font->filename()) ); } return $box; } // calculate box size from ttf font file with angle 0 $box = imageftbbox( size: $this->nativeFontSize($font), angle: 0, font_filename: $font->filename(), string: $text ); // build size from points return new Rectangle( width: intval(abs($box[6] - $box[4])), // difference of upper-left-x and upper-right-x height: intval(abs($box[7] - $box[1])), // difference if upper-left-y and lower-left-y pivot: new Point($box[6], $box[7]), // position of upper-left corner ); } /** * {@inheritdoc} * * @see FontProcessorInterface::nativeFontSize() */ public function nativeFontSize(FontInterface $font): float { return floatval(round($font->size() * .76, 6)); } /** * {@inheritdoc} * * @see FontProcessorInterface::leading() */ public function leading(FontInterface $font): int { return (int) round(parent::leading($font) * .8); } /** * Return width of a single character */ protected function gdCharacterWidth(int $gdfont): int { return $gdfont + 4; } /** * Return height of a single character */ protected function gdCharacterHeight(int $gdfont): int { return match ($gdfont) { 2, 3 => 14, 4, 5 => 16, default => 8, }; } } image/src/Drivers/Gd/ColorProcessor.php000064400000010224152665633650014074 0ustar00convertTo($this->colorspace); // gd only supports rgb so the channels can be accessed directly $r = $color->channel(Red::class)->value(); $g = $color->channel(Green::class)->value(); $b = $color->channel(Blue::class)->value(); $a = $color->channel(Alpha::class)->value(); // convert alpha value to gd alpha // ([opaque]255-0[transparent]) to ([opaque]0-127[transparent]) $a = (int) $this->convertRange($a, 0, 255, 127, 0); return ($a << 24) + ($r << 16) + ($g << 8) + $b; } /** * {@inheritdoc} * * @see ColorProcessorInterface::nativeToColor() */ public function nativeToColor(mixed $value): ColorInterface { if (!is_int($value) && !is_array($value)) { throw new ColorException('GD driver can only decode colors in integer and array format.'); } if (is_array($value)) { // array conversion if (!$this->isValidArrayColor($value)) { throw new ColorException( 'GD driver can only decode array color format array{red: int, green: int, blue: int, alpha: int}.', ); } $r = $value['red']; $g = $value['green']; $b = $value['blue']; $a = $value['alpha']; } else { // integer conversion $a = ($value >> 24) & 0xFF; $r = ($value >> 16) & 0xFF; $g = ($value >> 8) & 0xFF; $b = $value & 0xFF; } // convert gd apha integer to intervention alpha integer // ([opaque]0-127[transparent]) to ([opaque]255-0[transparent]) $a = (int) static::convertRange($a, 127, 0, 0, 255); return new Color($r, $g, $b, $a); } /** * Convert input in range (min) to (max) to the corresponding value * in target range (targetMin) to (targetMax). */ protected function convertRange( float|int $input, float|int $min, float|int $max, float|int $targetMin, float|int $targetMax ): float|int { return ceil(((($input - $min) * ($targetMax - $targetMin)) / ($max - $min)) + $targetMin); } /** * Check if given array is valid color format * array{red: int, green: int, blue: int, alpha: int} * i.e. result of imagecolorsforindex() * * @param array $color */ private function isValidArrayColor(array $color): bool { if (!array_key_exists('red', $color)) { return false; } if (!array_key_exists('green', $color)) { return false; } if (!array_key_exists('blue', $color)) { return false; } if (!array_key_exists('alpha', $color)) { return false; } if (!is_int($color['red'])) { return false; } if (!is_int($color['green'])) { return false; } if (!is_int($color['blue'])) { return false; } if (!is_int($color['alpha'])) { return false; } return true; } } image/src/Drivers/Gd/Encoders/BmpEncoder.php000064400000001277152665633650014706 0ustar00createEncodedImage(function ($pointer) use ($image): void { imagebmp($image->core()->native(), $pointer, false); }, 'image/bmp'); } } image/src/Drivers/Gd/Encoders/AvifEncoder.php000064400000001316152665633650015047 0ustar00createEncodedImage(function ($pointer) use ($image): void { imageavif($image->core()->native(), $pointer, $this->quality); }, 'image/avif'); } } image/src/Drivers/Gd/Encoders/GifEncoder.php000064400000003551152665633650014672 0ustar00isAnimated()) { return $this->encodeAnimated($image); } $gd = Cloner::clone($image->core()->native()); return $this->createEncodedImage(function ($pointer) use ($gd): void { imageinterlace($gd, $this->interlaced); imagegif($gd, $pointer); }, 'image/gif'); } /** * @throws RuntimeException */ protected function encodeAnimated(ImageInterface $image): EncodedImage { try { $builder = GifBuilder::canvas( $image->width(), $image->height() ); foreach ($image as $frame) { $builder->addFrame( source: $this->encode($frame->toImage($image->driver()))->toFilePointer(), delay: $frame->delay(), interlaced: $this->interlaced ); } $builder->setLoops($image->loops()); return new EncodedImage($builder->encode(), 'image/gif'); } catch (Exception $e) { throw new EncoderException($e->getMessage(), $e->getCode(), $e); } } } image/src/Drivers/Gd/Encoders/PngEncoder.php000064400000002743152665633650014713 0ustar00prepareOutput($image); return $this->createEncodedImage(function ($pointer) use ($output): void { imageinterlace($output, $this->interlaced); imagepng($output, $pointer, -1); }, 'image/png'); } /** * Prepare given image instance for PNG format output according to encoder settings * * @throws RuntimeException * @throws ColorException * @throws AnimationException */ private function prepareOutput(ImageInterface $image): GdImage { if ($this->indexed) { $output = clone $image; $output->reduceColors(255); return $output->core()->native(); } return Cloner::clone($image->core()->native()); } } image/src/Drivers/Gd/Encoders/JpegEncoder.php000064400000002032152665633650015043 0ustar00driver()->handleInput( $this->driver()->config()->blendingColor ); $output = Cloner::cloneBlended( $image->core()->native(), background: $blendingColor ); return $this->createEncodedImage(function ($pointer) use ($output): void { imageinterlace($output, $this->progressive); imagejpeg($output, $pointer, $this->quality); }, 'image/jpeg'); } } image/src/Drivers/Gd/Encoders/WebpEncoder.php000064400000001503152665633650015055 0ustar00quality === 100 && defined('IMG_WEBP_LOSSLESS') ? IMG_WEBP_LOSSLESS : $this->quality; return $this->createEncodedImage(function ($pointer) use ($image, $quality): void { imagewebp($image->core()->native(), $pointer, $quality); }, 'image/webp'); } } image/src/Drivers/Gd/Frame.php000064400000007503152665633650012156 0ustar00native = $native; return $this; } /** * {@inheritdoc} * * @see FrameInterface::native() */ public function native(): GdImage { return $this->native; } /** * {@inheritdoc} * * @see FrameInterface::size() */ public function size(): SizeInterface { return new Rectangle(imagesx($this->native), imagesy($this->native)); } /** * {@inheritdoc} * * @see FrameInterface::delay() */ public function delay(): float { return $this->delay; } /** * {@inheritdoc} * * @see FrameInterface::setDelay() */ public function setDelay(float $delay): FrameInterface { $this->delay = $delay; return $this; } /** * {@inheritdoc} * * @see FrameInterface::dispose() */ public function dispose(): int { return $this->dispose; } /** * {@inheritdoc} * * @see FrameInterface::setDispose() * * @throws InputException */ public function setDispose(int $dispose): FrameInterface { if (!in_array($dispose, [0, 1, 2, 3])) { throw new InputException('Value for argument $dispose must be 0, 1, 2 or 3.'); } $this->dispose = $dispose; return $this; } /** * {@inheritdoc} * * @see FrameInterface::setOffset() */ public function setOffset(int $left, int $top): FrameInterface { $this->offset_left = $left; $this->offset_top = $top; return $this; } /** * {@inheritdoc} * * @see FrameInterface::offsetLeft() */ public function offsetLeft(): int { return $this->offset_left; } /** * {@inheritdoc} * * @see FrameInterface::setOffsetLeft() */ public function setOffsetLeft(int $offset): FrameInterface { $this->offset_left = $offset; return $this; } /** * {@inheritdoc} * * @see FrameInterface::offsetTop() */ public function offsetTop(): int { return $this->offset_top; } /** * {@inheritdoc} * * @see FrameInterface::setOffsetTop() */ public function setOffsetTop(int $offset): FrameInterface { $this->offset_top = $offset; return $this; } /** * This workaround helps cloning GdImages which is currently not possible. * * @throws ColorException */ public function __clone(): void { $this->native = Cloner::clone($this->native); } } image/src/Drivers/AbstractFrame.php000064400000001025152665633650013301 0ustar00 */ public function __debugInfo(): array { return [ 'delay' => $this->delay(), 'left' => $this->offsetLeft(), 'top' => $this->offsetTop(), 'dispose' => $this->dispose(), ]; } } image/src/Origin.php000064400000003231152665633650010375 0ustar00mediaType; } /** * Alias of self::mediaType() */ public function mimetype(): string { return $this->mediaType(); } /** * Set media type of current instance */ public function setMediaType(string|MediaType $type): self { $this->mediaType = match (true) { is_string($type) => $type, default => $type->value, }; return $this; } /** * Return file path of origin */ public function filePath(): ?string { return $this->filePath; } /** * Set file path for origin */ public function setFilePath(string $path): self { $this->filePath = $path; return $this; } /** * Return file extension if origin was created from file path */ public function fileExtension(): ?string { return pathinfo($this->filePath ?: '', PATHINFO_EXTENSION) ?: null; } /** * Show debug info for the current image * * @return array */ public function __debugInfo(): array { return [ 'mediaType' => $this->mediaType(), 'filePath' => $this->filePath(), ]; } } image/src/Encoders/AutoEncoder.php000064400000001031152665633650013114 0ustar00encode( $this->encoderByMediaType( $image->origin()->mediaType() ) ); } } image/src/Encoders/Jpeg2000Encoder.php000064400000000707152665633650013404 0ustar00 */ protected array $options = []; /** * Create new encoder instance * * @param null|string|MediaType $mediaType Target media type for example "image/jpeg" * @return void */ public function __construct(public null|string|MediaType $mediaType = null, mixed ...$options) { $this->options = $options; } /** * {@inheritdoc} * * @see EncoderInterface::encode() */ public function encode(ImageInterface $image): EncodedImageInterface { $mediaType = is_null($this->mediaType) ? $image->origin()->mediaType() : $this->mediaType; return $image->encode( $this->encoderByMediaType($mediaType) ); } /** * Return new encoder by given media (MIME) type * * @throws EncoderException */ protected function encoderByMediaType(string|MediaType $mediaType): EncoderInterface { try { $mediaType = is_string($mediaType) ? MediaType::from($mediaType) : $mediaType; } catch (Error) { throw new EncoderException('No encoder found for media type (' . $mediaType . ').'); } return $mediaType->format()->encoder(...$this->options); } } image/src/Encoders/FileExtensionEncoder.php000064400000003626152665633650014774 0ustar00 */ protected array $options = []; /** * Create new encoder instance to encode to format of given file extension * * @param null|string|FileExtension $extension Target file extension for example "png" * @return void */ public function __construct(public null|string|FileExtension $extension = null, mixed ...$options) { $this->options = $options; } /** * {@inheritdoc} * * @see EncoderInterface::encode() */ public function encode(ImageInterface $image): EncodedImageInterface { $extension = is_null($this->extension) ? $image->origin()->fileExtension() : $this->extension; return $image->encode( $this->encoderByFileExtension( $extension ) ); } /** * Create matching encoder for given file extension * * @throws EncoderException */ protected function encoderByFileExtension(null|string|FileExtension $extension): EncoderInterface { if (empty($extension)) { throw new EncoderException('No encoder found for empty file extension.'); } try { $extension = is_string($extension) ? FileExtension::from(strtolower($extension)) : $extension; } catch (Error) { throw new EncoderException('No encoder found for file extension (' . $extension . ').'); } return $extension->format()->encoder(...$this->options); } } image/src/Encoders/PngEncoder.php000064400000000563152665633650012741 0ustar00encode( $this->encoderByFileExtension( is_null($this->path) ? $image->origin()->fileExtension() : pathinfo($this->path, PATHINFO_EXTENSION) ) ); } } image/src/Encoders/WebpEncoder.php000064400000000703152665633650013106 0ustar00driver = $this->resolveDriver($driver, ...$options); } /** * Create image manager with given driver * * @link https://image.intervention.io/v3/basics/configuration-drivers#static-constructor * * @throws DriverException * @throws InputException */ public static function withDriver(string|DriverInterface $driver, mixed ...$options): self { return new self(self::resolveDriver($driver, ...$options)); } /** * Create image manager with GD driver * * @link https://image.intervention.io/v3/basics/configuration-drivers#static-gd-driver-constructor * * @throws DriverException * @throws InputException */ public static function gd(mixed ...$options): self { return self::withDriver(new GdDriver(), ...$options); } /** * Create image manager with Imagick driver * * @link https://image.intervention.io/v3/basics/configuration-drivers#static-imagick-driver-constructor * * @throws DriverException * @throws InputException */ public static function imagick(mixed ...$options): self { return self::withDriver(new ImagickDriver(), ...$options); } /** * {@inheritdoc} * * @see ImageManagerInterface::create() */ public function create(int $width, int $height): ImageInterface { return $this->driver->createImage($width, $height); } /** * {@inheritdoc} * * @see ImageManagerInterface::read() */ public function read(mixed $input, string|array|DecoderInterface $decoders = []): ImageInterface { return $this->driver->handleInput( $input, match (true) { is_string($decoders), is_a($decoders, DecoderInterface::class) => [$decoders], default => $decoders, } ); } /** * {@inheritdoc} * * @see ImageManagerInterface::animate() */ public function animate(callable $init): ImageInterface { return $this->driver->createAnimation($init); } /** * {@inheritdoc} * * @see ImageManagerInterface::driver() */ public function driver(): DriverInterface { return $this->driver; } /** * Return driver object from given input which might be driver classname or instance of DriverInterface * * @throws DriverException * @throws InputException */ private static function resolveDriver(string|DriverInterface $driver, mixed ...$options): DriverInterface { $driver = match (true) { $driver instanceof DriverInterface => $driver, class_exists($driver) => new $driver(), default => throw new DriverException( 'Unable to resolve driver. Argment must be either an instance of ' . DriverInterface::class . '::class or a qualified namespaced name of the driver class.', ), }; if (!$driver instanceof DriverInterface) { throw new DriverException( 'Unable to resolve driver. Driver object must implement ' . DriverInterface::class . '.', ); } $driver->config()->setOptions(...$options); return $driver; } } image/composer.json000064400000002340152665633650010370 0ustar00{ "name": "intervention/image", "description": "PHP image manipulation", "homepage": "https://image.intervention.io/", "keywords": [ "image", "gd", "imagick", "watermark", "thumbnail", "resize" ], "license": "MIT", "authors": [ { "name": "Oliver Vogel", "email": "oliver@intervention.io", "homepage": "https://intervention.io/" } ], "require": { "php": "^8.1", "ext-mbstring": "*", "intervention/gif": "^4.2" }, "require-dev": { "phpunit/phpunit": "^10.0 || ^11.0 || ^12.0", "mockery/mockery": "^1.6", "phpstan/phpstan": "^2.1", "squizlabs/php_codesniffer": "^3.8", "slevomat/coding-standard": "~8.0" }, "suggest": { "ext-exif": "Recommended to be able to read EXIF data properly." }, "autoload": { "psr-4": { "Intervention\\Image\\": "src" } }, "autoload-dev": { "psr-4": { "Intervention\\Image\\Tests\\": "tests" } }, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } } }