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
34 changes: 23 additions & 11 deletions src/Gd/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
}
4 changes: 3 additions & 1 deletion src/Gd/Imagine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Loading