Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Analyzers/PixelColorAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@ class PixelColorAnalyzer extends GenericPixelColorAnalyzer implements Specialize
*
* @throws StateException
* @throws AnalyzerException
* @throws DriverException
*/
public function analyze(ImageInterface $image): mixed
{
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Analyzers/PixelColorsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +20,7 @@ class PixelColorsAnalyzer extends PixelColorAnalyzer implements SpecializedInter
*
* @throws StateException
* @throws AnalyzerException
* @throws DriverException
*/
public function analyze(ImageInterface $image): mixed
{
Expand Down
40 changes: 40 additions & 0 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -146,6 +173,7 @@ public function native(): mixed
* @see CoreInterface::setNative()
*
* @throws InvalidArgumentException
* @throws DriverException
*/
public function setNative(mixed $native): CoreInterface
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/BlurModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/BrightnessModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/ColorizeModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/ColorspaceModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +22,7 @@ class ColorspaceModifier extends GenericColorspaceModifier implements Specialize
*
* @throws ModifierException
* @throws NotSupportedException
* @throws DriverException
*/
public function apply(ImageInterface $image): ImageInterface
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/ContrastModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/FillModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,7 @@ class FillModifier extends GenericFillModifier implements SpecializedInterface
*
* @throws StateException
* @throws ModifierException
* @throws DriverException
*/
public function apply(ImageInterface $image): ImageInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/Modifiers/FillTransparentAreasModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function apply(ImageInterface $image): ImageInterface
* @param array<float> $color
* @throws StateException
* @throws ModifierException
* @throws DriverException
*/
private function canvas(ImageInterface $image, array $color): ImageInterface
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/FlipModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/GammaModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
3 changes: 3 additions & 0 deletions src/Modifiers/GrayscaleModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
11 changes: 11 additions & 0 deletions src/Modifiers/InsertModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions src/Modifiers/InvertModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/OrientModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/ProfileModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/ResizeModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,7 @@ class ResizeModifier extends GenericResizeModifier implements SpecializedInterfa
*
* @throws InvalidArgumentException
* @throws VipsException
* @throws DriverException
*/
public function apply(ImageInterface $image): ImageInterface
{
Expand Down
3 changes: 3 additions & 0 deletions src/Modifiers/ResolutionModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/SharpenModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Modifiers/StripMetaModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,7 @@ class StripMetaModifier implements ModifierInterface, SpecializedInterface
* @see ModifierInterface::apply()
*
* @throws ModifierException
* @throws DriverException
*/
public function apply(ImageInterface $image): ImageInterface
{
Expand Down
Loading