diff --git a/src/Analyzers/PixelColorAnalyzer.php b/src/Analyzers/PixelColorAnalyzer.php index 5af33f6..6737c6a 100644 --- a/src/Analyzers/PixelColorAnalyzer.php +++ b/src/Analyzers/PixelColorAnalyzer.php @@ -7,6 +7,7 @@ use Intervention\Image\Analyzers\PixelColorAnalyzer as GenericPixelColorAnalyzer; use Intervention\Image\Drivers\Vips\Core; use Intervention\Image\Exceptions\AnalyzerException; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\StateException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\CoreInterface; @@ -23,6 +24,7 @@ class PixelColorAnalyzer extends GenericPixelColorAnalyzer implements Specialize * * @throws StateException * @throws AnalyzerException + * @throws DriverException */ public function analyze(ImageInterface $image): mixed { @@ -39,6 +41,7 @@ public function analyze(ImageInterface $image): mixed * * @throws StateException * @throws AnalyzerException + * @throws DriverException */ protected function colorAt(ImageInterface $image, CoreInterface $core, int $x, int $y): ColorInterface { diff --git a/src/Analyzers/PixelColorsAnalyzer.php b/src/Analyzers/PixelColorsAnalyzer.php index 1714602..494d5d5 100644 --- a/src/Analyzers/PixelColorsAnalyzer.php +++ b/src/Analyzers/PixelColorsAnalyzer.php @@ -6,6 +6,7 @@ use Intervention\Image\Collection; use Intervention\Image\Exceptions\AnalyzerException; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\StateException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -19,6 +20,7 @@ class PixelColorsAnalyzer extends PixelColorAnalyzer implements SpecializedInter * * @throws StateException * @throws AnalyzerException + * @throws DriverException */ public function analyze(ImageInterface $image): mixed { diff --git a/src/Core.php b/src/Core.php index a54aeb3..6a830d9 100644 --- a/src/Core.php +++ b/src/Core.php @@ -25,7 +25,34 @@ */ class Core implements CoreInterface, Iterator { + /** + * Number of operations that may be chained onto the image before its + * pipeline is rendered into memory. + * + * libvips evaluates a pipeline by recursing once per node, on a worker + * thread whose stack is fixed when the thread is created. A long enough + * chain overruns that stack and takes the process down with SIGBUS or + * SIGSEGV, with no catchable error and nothing on stderr. The ceiling is + * the platform's default thread stack size, measured on a chain of + * composites at 198 nodes on macOS arm64, 1843 on musl and 2009 on glibc. + * Callers that build an image in a loop, tiling a watermark or drawing + * many shapes, reach it with ordinary inputs. + * + * Rendering to memory every N operations keeps the depth well under the + * lowest of those ceilings. Chains shorter than N, which is every normal + * use of the driver, never pay for it. Past N there is a cost: a rendered + * image is a whole image, so an operation that later narrows the region of + * interest, a crop after the chain say, no longer gets to narrow what the + * chain computes. + * + * This counts calls, not libvips nodes, so it is a bound rather than a + * measurement. A modifier that chains several nodes per call spends the + * budget faster than one node per call. + */ + public const MAX_CHAINED_OPERATIONS = 32; + protected int $iteratorIndex = 0; + protected int $chainedOperations = 0; protected CollectionInterface $meta; protected null|PathSource|BufferSource $stashedSource = null; @@ -146,6 +173,7 @@ public function native(): mixed * @see CoreInterface::setNative() * * @throws InvalidArgumentException + * @throws DriverException */ public function setNative(mixed $native): CoreInterface { @@ -158,6 +186,16 @@ public function setNative(mixed $native): CoreInterface $this->vipsImage = $native; $this->stashedSource = null; + if (++$this->chainedOperations >= static::MAX_CHAINED_OPERATIONS) { + try { + $this->vipsImage = $this->vipsImage->copyMemory(); + } catch (VipsException $e) { + throw new DriverException('Failed to render image pipeline into memory', previous: $e); + } + + $this->chainedOperations = 0; + } + return $this; } @@ -185,6 +223,8 @@ public function setStashedSource(PathSource|BufferSource $source): self /** * Renders vips image of given core into memory and serves any downstream * requests from the memory area + * + * @throws DriverException */ public static function ensureInMemory(CoreInterface $core): CoreInterface { diff --git a/src/Modifiers/BlurModifier.php b/src/Modifiers/BlurModifier.php index 5a8f0ba..e3c60bb 100644 --- a/src/Modifiers/BlurModifier.php +++ b/src/Modifiers/BlurModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -18,6 +19,7 @@ class BlurModifier extends GenericBlurModifier implements SpecializedInterface * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/BrightnessModifier.php b/src/Modifiers/BrightnessModifier.php index 633007b..1a5c53e 100644 --- a/src/Modifiers/BrightnessModifier.php +++ b/src/Modifiers/BrightnessModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -18,6 +19,7 @@ class BrightnessModifier extends GenericBrightnessModifier implements Specialize * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/ColorizeModifier.php b/src/Modifiers/ColorizeModifier.php index a7ba451..f66dc1e 100644 --- a/src/Modifiers/ColorizeModifier.php +++ b/src/Modifiers/ColorizeModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -18,6 +19,7 @@ class ColorizeModifier extends GenericColorizeModifier implements SpecializedInt * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/ColorspaceModifier.php b/src/Modifiers/ColorspaceModifier.php index f050bcb..9fe778f 100644 --- a/src/Modifiers/ColorspaceModifier.php +++ b/src/Modifiers/ColorspaceModifier.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; use Intervention\Image\Drivers\Vips\ColorProcessor; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Exceptions\NotSupportedException; use Intervention\Image\Interfaces\ImageInterface; @@ -21,6 +22,7 @@ class ColorspaceModifier extends GenericColorspaceModifier implements Specialize * * @throws ModifierException * @throws NotSupportedException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/ContrastModifier.php b/src/Modifiers/ContrastModifier.php index eb47a4c..6eb2c8b 100644 --- a/src/Modifiers/ContrastModifier.php +++ b/src/Modifiers/ContrastModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -18,6 +19,7 @@ class ContrastModifier extends GenericContrastModifier implements SpecializedInt * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/FillModifier.php b/src/Modifiers/FillModifier.php index cb07e20..225e731 100644 --- a/src/Modifiers/FillModifier.php +++ b/src/Modifiers/FillModifier.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; use Intervention\Image\Exceptions\ColorDecoderException; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Exceptions\StateException; use Intervention\Image\Interfaces\ImageInterface; @@ -24,6 +25,7 @@ class FillModifier extends GenericFillModifier implements SpecializedInterface * * @throws StateException * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/FillTransparentAreasModifier.php b/src/Modifiers/FillTransparentAreasModifier.php index 490da5e..6f6a08e 100644 --- a/src/Modifiers/FillTransparentAreasModifier.php +++ b/src/Modifiers/FillTransparentAreasModifier.php @@ -72,6 +72,7 @@ public function apply(ImageInterface $image): ImageInterface * @param array $color * @throws StateException * @throws ModifierException + * @throws DriverException */ private function canvas(ImageInterface $image, array $color): ImageInterface { diff --git a/src/Modifiers/FlipModifier.php b/src/Modifiers/FlipModifier.php index 2299d81..099881c 100644 --- a/src/Modifiers/FlipModifier.php +++ b/src/Modifiers/FlipModifier.php @@ -6,6 +6,7 @@ use Intervention\Image\Direction; use Intervention\Image\Drivers\Vips\Core; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -21,6 +22,7 @@ class FlipModifier extends GenericFlipModifier implements SpecializedInterface * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/GammaModifier.php b/src/Modifiers/GammaModifier.php index f57bcd7..92bba33 100644 --- a/src/Modifiers/GammaModifier.php +++ b/src/Modifiers/GammaModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -18,6 +19,7 @@ class GammaModifier extends GenericGammaModifier implements SpecializedInterface * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/GrayscaleModifier.php b/src/Modifiers/GrayscaleModifier.php index 24939ba..d795f9a 100644 --- a/src/Modifiers/GrayscaleModifier.php +++ b/src/Modifiers/GrayscaleModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; use Intervention\Image\Modifiers\GrayscaleModifier as GenericGrayscaleModifier; @@ -15,6 +16,8 @@ class GrayscaleModifier extends GenericGrayscaleModifier implements SpecializedI * {@inheritdoc} * * @see Intervention\Image\Interfaces\ModifierInterface::apply() + * + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/InsertModifier.php b/src/Modifiers/InsertModifier.php index 5c15577..c0a7a70 100644 --- a/src/Modifiers/InsertModifier.php +++ b/src/Modifiers/InsertModifier.php @@ -29,6 +29,17 @@ class InsertModifier extends GenericInsertModifier implements SpecializedInterfa public function apply(ImageInterface $image): ImageInterface { $watermark = $this->driver()->decodeImage($this->image); + + // A libvips sequential source may only be read once, in order. The + // element is folded into the target's pipeline by reference, so it is + // read again every time that pipeline is evaluated: inserting the same + // decoded image twice, or encoding the result twice, fails with "out + // of order read". Materialising the element on its own core spends + // that single read here, and leaves every later evaluation reading + // from memory. Decoded images are shared by reference, so an image + // inserted many times only pays for this once. + Core::ensureInMemory($watermark->core()); + $elementNative = $watermark->core()->native(); $position = $this->position($image, $watermark); diff --git a/src/Modifiers/InvertModifier.php b/src/Modifiers/InvertModifier.php index 2825d95..8f53c90 100644 --- a/src/Modifiers/InvertModifier.php +++ b/src/Modifiers/InvertModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; use Intervention\Image\Modifiers\InvertModifier as GenericInvertModifier; @@ -14,6 +15,8 @@ class InvertModifier extends GenericInvertModifier implements SpecializedInterfa * {@inheritdoc} * * @see Intervention\Image\Interfaces\ModifierInterface::apply() + * + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/OrientModifier.php b/src/Modifiers/OrientModifier.php index 39cf306..93ae6b4 100644 --- a/src/Modifiers/OrientModifier.php +++ b/src/Modifiers/OrientModifier.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; use Intervention\Image\Drivers\Vips\Core; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -19,6 +20,7 @@ class OrientModifier extends GenericOrientModifier implements SpecializedInterfa * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/ProfileModifier.php b/src/Modifiers/ProfileModifier.php index 2d6cc03..787d9de 100644 --- a/src/Modifiers/ProfileModifier.php +++ b/src/Modifiers/ProfileModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -18,6 +19,7 @@ class ProfileModifier extends GenericProfileModifier implements SpecializedInter * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/ResizeModifier.php b/src/Modifiers/ResizeModifier.php index 3ab24e4..84411d5 100644 --- a/src/Modifiers/ResizeModifier.php +++ b/src/Modifiers/ResizeModifier.php @@ -9,6 +9,7 @@ use Intervention\Image\Drivers\Vips\Source\BufferSource; use Intervention\Image\Drivers\Vips\Source\PathSource; use Intervention\Image\Drivers\Vips\Traits\CanNormalizeBands; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\InvalidArgumentException; use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\ImageInterface; @@ -30,6 +31,7 @@ class ResizeModifier extends GenericResizeModifier implements SpecializedInterfa * * @throws InvalidArgumentException * @throws VipsException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/ResolutionModifier.php b/src/Modifiers/ResolutionModifier.php index 8f5e670..1a95ab5 100644 --- a/src/Modifiers/ResolutionModifier.php +++ b/src/Modifiers/ResolutionModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; use Intervention\Image\Modifiers\ResolutionModifier as GenericResolutionModifier; @@ -14,6 +15,8 @@ class ResolutionModifier extends GenericResolutionModifier implements Specialize * {@inheritdoc} * * @see Intervention\Image\Interfaces\ModifierInterface::apply() + * + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/SharpenModifier.php b/src/Modifiers/SharpenModifier.php index 89fbfeb..ca0f943 100644 --- a/src/Modifiers/SharpenModifier.php +++ b/src/Modifiers/SharpenModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -19,6 +20,7 @@ class SharpenModifier extends GenericSharpenModifier implements SpecializedInter * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/StripMetaModifier.php b/src/Modifiers/StripMetaModifier.php index 9e1281d..6ec9800 100644 --- a/src/Modifiers/StripMetaModifier.php +++ b/src/Modifiers/StripMetaModifier.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; use Intervention\Image\Collection; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; @@ -22,6 +23,7 @@ class StripMetaModifier implements ModifierInterface, SpecializedInterface * @see ModifierInterface::apply() * * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Modifiers/TextModifier.php b/src/Modifiers/TextModifier.php index f1545cf..cbb770d 100644 --- a/src/Modifiers/TextModifier.php +++ b/src/Modifiers/TextModifier.php @@ -124,14 +124,7 @@ public function apply(ImageInterface $image): ImageInterface if (isset($stroke)) { // draw stroke effect with offsets - foreach ($this->strokeOffsets($this->font) as $offset) { - $modified = $this->placeTextOnFrame( - $stroke, - $modified, - $blockPosition->x() - $offset->x(), - $blockPosition->y() - $offset->y(), - ); - } + $modified = $this->placeStrokeOnFrame($stroke, $modified, $blockPosition); } // place text image on original image @@ -150,14 +143,7 @@ public function apply(ImageInterface $image): ImageInterface if (isset($stroke)) { // draw stroke effect with offsets - foreach ($this->strokeOffsets($this->font) as $offset) { - $modifiedFrame = $this->placeTextOnFrame( - $stroke, - $modifiedFrame, - $blockPosition->x() - $offset->x(), - $blockPosition->y() - $offset->y(), - ); - } + $modifiedFrame = $this->placeStrokeOnFrame($stroke, $modifiedFrame, $blockPosition); } // place text image on original image @@ -179,6 +165,44 @@ public function apply(ImageInterface $image): ImageInterface return $image; } + /** + * Stamp the stroke image around the given position on the given frame. + * + * A stroke of width w is (2w + 1)^2 stamps. Chaining one composite per + * stamp builds that many libvips pipeline nodes, and libvips recurses once + * per node when it evaluates, so a stroke of 6 or more overruns the worker + * thread stack on macOS and kills the process. libvips composites any + * number of overlays in a single operation, which is one node whatever the + * width, and a good deal faster. + */ + private function placeStrokeOnFrame( + VipsImage $stroke, + FrameInterface $frame, + PointInterface $blockPosition, + ): FrameInterface { + $x = []; + $y = []; + + foreach ($this->strokeOffsets($this->font) as $offset) { + $x[] = $blockPosition->x() - $offset->x(); + $y[] = $blockPosition->y() - $offset->y(); + } + + if ($x === []) { + return $frame; + } + + $frame->setNative( + $frame->native()->composite( + array_fill(0, count($x), $stroke), + array_fill(0, count($x), BlendMode::OVER), + ['x' => $x, 'y' => $y], + ), + ); + + return $frame; + } + /** * Place given text image at given position on given frame */ diff --git a/src/Modifiers/TrimModifier.php b/src/Modifiers/TrimModifier.php index a9c6530..46ffc13 100644 --- a/src/Modifiers/TrimModifier.php +++ b/src/Modifiers/TrimModifier.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; use Intervention\Image\Drivers\Vips\Core; +use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Exceptions\NotSupportedException; use Intervention\Image\Interfaces\ImageInterface; @@ -28,6 +29,7 @@ public function __construct(public int $tolerance = 40) * * @throws NotSupportedException * @throws ModifierException + * @throws DriverException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/tests/Unit/CoreTest.php b/tests/Unit/CoreTest.php index afee646..beee127 100644 --- a/tests/Unit/CoreTest.php +++ b/tests/Unit/CoreTest.php @@ -181,4 +181,70 @@ public function testSetNativeClearsStashedSource(): void $this->assertNull($core->stashedSource()); } + + public function testSetNativeLeavesThePipelineLazyBelowTheOperationLimit(): void + { + $core = new Core($this->vipsImage(10, 10, [255, 0, 0])); + + $native = null; + for ($i = 1; $i < Core::MAX_CHAINED_OPERATIONS; $i++) { + $native = $this->vipsImage(10, 10, [$i, 0, 0]); + $core->setNative($native); + } + + // untouched: the image handed over is the one that comes back + $this->assertSame($native, $core->native()); + } + + public function testSetNativeRendersThePipelineToMemoryAtTheOperationLimit(): void + { + $core = new Core($this->vipsImage(10, 10, [255, 0, 0])); + + $native = null; + for ($i = 1; $i <= Core::MAX_CHAINED_OPERATIONS; $i++) { + $native = $this->vipsImage(10, 10, [$i, 0, 0]); + $core->setNative($native); + } + + // rendered: a different instance, holding the last image handed over + $this->assertNotSame($native, $core->native()); + $this->assertSame($native->writeToBuffer('.png'), $core->native()->writeToBuffer('.png')); + } + + public function testSetNativeStartsANewBudgetAfterRenderingToMemory(): void + { + $core = new Core($this->vipsImage(10, 10, [255, 0, 0])); + + // spend the whole budget, which renders on the last call + for ($i = 1; $i <= Core::MAX_CHAINED_OPERATIONS; $i++) { + $core->setNative($this->vipsImage(10, 10, [$i, 0, 0])); + } + + // the budget is a period, not a threshold: the next call is lazy again + $native = $this->vipsImage(10, 10, [1, 0, 0]); + $core->setNative($native); + + $this->assertSame($native, $core->native()); + } + + public function testChainedModificationsPastTheOperationLimitStillRender(): void + { + // Without the render-to-memory guard this chain overruns the libvips + // worker thread stack and kills the process. Where that happens + // depends on the platform's thread stack size: 1000 inserts is well + // past the ceiling measured on macOS, between 150 and 200, and well + // short of the one on glibc, over 6000. So on Linux CI this asserts + // only that rendering mid-chain leaves the result alone. + $manager = ImageManager::usingDriver(Driver::class); + + $image = $manager->createImage(16, 16)->fill('0000ff'); + $watermark = $manager->createImage(4, 4)->fill('ff0000'); + + for ($i = 0; $i < 1000; $i++) { + $image->insert($watermark); + } + + $this->assertColor(255, 0, 0, 255, $image->colorAt(2, 2)); + $this->assertColor(0, 0, 255, 255, $image->colorAt(10, 10)); + } } diff --git a/tests/Unit/Modifiers/InsertModifierTest.php b/tests/Unit/Modifiers/InsertModifierTest.php index 4b4e876..2dfa1d1 100644 --- a/tests/Unit/Modifiers/InsertModifierTest.php +++ b/tests/Unit/Modifiers/InsertModifierTest.php @@ -4,8 +4,10 @@ namespace Intervention\Image\Drivers\Vips\Tests\Unit\Modifiers; +use Intervention\Image\Drivers\Vips\Core; use Intervention\Image\Drivers\Vips\Driver; use Intervention\Image\Drivers\Vips\Tests\BaseTestCase; +use Intervention\Image\Encoders\PngEncoder; use Intervention\Image\Encoders\WebpEncoder; use Intervention\Image\ImageManager; use Intervention\Image\Modifiers\InsertModifier; @@ -73,4 +75,40 @@ public function testApplyAnimatedWithBinaryWatermarkEncodes(): void $encoded = $image->encode(new WebpEncoder(quality: 80)); $this->assertMediaType('image/webp', $encoded); } + + public function testApplyThenEncodeTwiceKeepsTheSameResult(): void + { + // Regression for: a watermark decoded from a file opens a sequential + // libvips source, which may only be read once, in order. It stays in + // the target's pipeline by reference, so encoding the result a second + // time read that source a second time and failed with "pngload: out + // of order read". + $manager = ImageManager::usingDriver(Driver::class); + + $image = $manager->createImage(64, 64)->fill('0000ff'); + $image->insert($manager->decodePath($this->getTestResourcePath('circle.png'))); + + $first = (string) $image->encode(new PngEncoder()); + $second = (string) $image->encode(new PngEncoder()); + + $this->assertSame($first, $second); + } + + public function testApplyDecodedWatermarkPastThePipelineRenderLimitEncodes(): void + { + // Rendering the pipeline to memory mid-chain is another evaluation of + // the watermark's sequential source, on top of the one at encode time. + // Regression for the render limit and the element materialisation + // pulling against each other. + $manager = ImageManager::usingDriver(Driver::class); + + $image = $manager->createImage(64, 64)->fill('0000ff'); + $watermark = $manager->decodePath($this->getTestResourcePath('circle.png')); + + for ($i = 0; $i <= Core::MAX_CHAINED_OPERATIONS; $i++) { + $image->insert($watermark); + } + + $this->assertMediaType('image/png', $image->encode(new PngEncoder())); + } } diff --git a/tests/Unit/Modifiers/TextModifierTest.php b/tests/Unit/Modifiers/TextModifierTest.php index 1047771..98253c8 100644 --- a/tests/Unit/Modifiers/TextModifierTest.php +++ b/tests/Unit/Modifiers/TextModifierTest.php @@ -6,6 +6,7 @@ use Intervention\Image\Drivers\Vips\Modifiers\TextModifier; use Intervention\Image\Drivers\Vips\Tests\BaseTestCase; +use Intervention\Image\Encoders\PngEncoder; use Intervention\Image\Geometry\Point; use Intervention\Image\Typography\Font; @@ -22,4 +23,21 @@ public function testApply(): void $font->setLineHeight(2); $image->modify(new TextModifier('ABC & D', new Point(150, 150), $font)); } + + public function testApplyWideStroke(): void + { + // A stroke of width w is (2w + 1)^2 stamps. Chaining one composite per + // stamp overran the libvips worker thread stack and killed the process + // from width 6 up; they are composited in a single operation now. + $image = $this->readTestImage('blocks.png'); + $font = new Font($this->getTestResourcePath('test.ttf')); + $font->setColor('b53517'); + $font->setSize(32); + $font->setStrokeColor('ffffff'); + $font->setStrokeWidth(10); + + $image->modify(new TextModifier('ABC', new Point(150, 150), $font)); + + $this->assertMediaType('image/png', $image->encode(new PngEncoder())); + } }