From cb8d804b23bd010a97c18cb64ae205ae942e39db Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Tue, 9 Dec 2025 16:03:25 +0100 Subject: [PATCH] Don't call imagedestroy() on PHP 8.5+ See https://www.php.net/manual/en/migration85.deprecated.php#migration85.deprecated.gd --- src/Gd/Image.php | 34 +++++++++++++++++++++++----------- src/Gd/Imagine.php | 4 +++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Gd/Image.php b/src/Gd/Image.php index 16f0e273..e9d69ffe 100644 --- a/src/Gd/Image.php +++ b/src/Gd/Image.php @@ -71,7 +71,7 @@ public function __destruct() { if ($this->resource) { if (is_resource($this->resource) && get_resource_type($this->resource) === 'gd' || $this->resource instanceof \GdImage) { - imagedestroy($this->resource); + self::destroyImage($this->resource); } $this->resource = null; } @@ -88,7 +88,7 @@ public function __clone() $size = $this->getSize(); $copy = $this->createImage($size, 'copy'); if (imagecopy($copy, $this->resource, 0, 0, 0, 0, $size->getWidth(), $size->getHeight()) === false) { - imagedestroy($copy); + self::destroyImage($copy); throw new RuntimeException('Image copy operation failed'); } $this->resource = $copy; @@ -146,11 +146,11 @@ final public function crop(PointInterface $start, BoxInterface $size) $dest = $this->createImage($size, 'crop'); if (imagecopy($dest, $this->resource, 0, 0, $start->getX(), $start->getY(), $width, $height) === false) { - imagedestroy($dest); + self::destroyImage($dest); throw new RuntimeException('Image crop operation failed'); } - imagedestroy($this->resource); + self::destroyImage($this->resource); $this->resource = $dest; @@ -223,11 +223,11 @@ final public function resize(BoxInterface $size, $filter = ImageInterface::FILTE imagealphablending($dest, false); if ($success === false) { - imagedestroy($dest); + self::destroyImage($dest); throw new RuntimeException('Image resize operation failed'); } - imagedestroy($this->resource); + self::destroyImage($this->resource); $this->resource = $dest; @@ -251,7 +251,7 @@ final public function rotate($angle, ?ColorInterface $background = null) throw new RuntimeException('Image rotate operation failed'); } - imagedestroy($this->resource); + self::destroyImage($this->resource); $this->resource = $resource; return $this; @@ -360,12 +360,12 @@ final public function flipHorizontally() for ($i = 0; $i < $width; $i++) { if (imagecopy($dest, $this->resource, $i, 0, ($width - 1) - $i, 0, 1, $height) === false) { - imagedestroy($dest); + self::destroyImage($dest); throw new RuntimeException('Horizontal flip operation failed'); } } - imagedestroy($this->resource); + self::destroyImage($this->resource); $this->resource = $dest; } @@ -390,12 +390,12 @@ final public function flipVertically() for ($i = 0; $i < $height; $i++) { if (imagecopy($dest, $this->resource, 0, $i, 0, ($height - 1) - $i, $width, 1) === false) { - imagedestroy($dest); + self::destroyImage($dest); throw new RuntimeException('Vertical flip operation failed'); } } - imagedestroy($this->resource); + self::destroyImage($this->resource); $this->resource = $dest; } @@ -822,4 +822,16 @@ private function getColor(ColorInterface $color) return $index; } + + /** + * @param resource|\GdImage $resource + * + * @return void + */ + private static function destroyImage($resource) + { + if (PHP_VERSION_ID < 80500) { + imagedestroy($resource); + } + } } diff --git a/src/Gd/Imagine.php b/src/Gd/Imagine.php index bae32743..e07a74bb 100644 --- a/src/Gd/Imagine.php +++ b/src/Gd/Imagine.php @@ -181,7 +181,9 @@ private function wrap($resource, PaletteInterface $palette, MetadataBag $metadat imagecopy($truecolor, $resource, 0, 0, 0, 0, $width, $height); - imagedestroy($resource); + if (PHP_VERSION_ID < 80500) { + imagedestroy($resource); + } $resource = $truecolor; } }