Skip to content

Commit 218a0ae

Browse files
authored
1 parent 43856ce commit 218a0ae

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/Gd/Image.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __destruct()
7171
{
7272
if ($this->resource) {
7373
if (is_resource($this->resource) && get_resource_type($this->resource) === 'gd' || $this->resource instanceof \GdImage) {
74-
imagedestroy($this->resource);
74+
self::destroyImage($this->resource);
7575
}
7676
$this->resource = null;
7777
}
@@ -88,7 +88,7 @@ public function __clone()
8888
$size = $this->getSize();
8989
$copy = $this->createImage($size, 'copy');
9090
if (imagecopy($copy, $this->resource, 0, 0, 0, 0, $size->getWidth(), $size->getHeight()) === false) {
91-
imagedestroy($copy);
91+
self::destroyImage($copy);
9292
throw new RuntimeException('Image copy operation failed');
9393
}
9494
$this->resource = $copy;
@@ -146,11 +146,11 @@ final public function crop(PointInterface $start, BoxInterface $size)
146146
$dest = $this->createImage($size, 'crop');
147147

148148
if (imagecopy($dest, $this->resource, 0, 0, $start->getX(), $start->getY(), $width, $height) === false) {
149-
imagedestroy($dest);
149+
self::destroyImage($dest);
150150
throw new RuntimeException('Image crop operation failed');
151151
}
152152

153-
imagedestroy($this->resource);
153+
self::destroyImage($this->resource);
154154

155155
$this->resource = $dest;
156156

@@ -223,11 +223,11 @@ final public function resize(BoxInterface $size, $filter = ImageInterface::FILTE
223223
imagealphablending($dest, false);
224224

225225
if ($success === false) {
226-
imagedestroy($dest);
226+
self::destroyImage($dest);
227227
throw new RuntimeException('Image resize operation failed');
228228
}
229229

230-
imagedestroy($this->resource);
230+
self::destroyImage($this->resource);
231231

232232
$this->resource = $dest;
233233

@@ -251,7 +251,7 @@ final public function rotate($angle, ?ColorInterface $background = null)
251251
throw new RuntimeException('Image rotate operation failed');
252252
}
253253

254-
imagedestroy($this->resource);
254+
self::destroyImage($this->resource);
255255
$this->resource = $resource;
256256

257257
return $this;
@@ -360,12 +360,12 @@ final public function flipHorizontally()
360360

361361
for ($i = 0; $i < $width; $i++) {
362362
if (imagecopy($dest, $this->resource, $i, 0, ($width - 1) - $i, 0, 1, $height) === false) {
363-
imagedestroy($dest);
363+
self::destroyImage($dest);
364364
throw new RuntimeException('Horizontal flip operation failed');
365365
}
366366
}
367367

368-
imagedestroy($this->resource);
368+
self::destroyImage($this->resource);
369369

370370
$this->resource = $dest;
371371
}
@@ -390,12 +390,12 @@ final public function flipVertically()
390390

391391
for ($i = 0; $i < $height; $i++) {
392392
if (imagecopy($dest, $this->resource, 0, $i, 0, ($height - 1) - $i, $width, 1) === false) {
393-
imagedestroy($dest);
393+
self::destroyImage($dest);
394394
throw new RuntimeException('Vertical flip operation failed');
395395
}
396396
}
397397

398-
imagedestroy($this->resource);
398+
self::destroyImage($this->resource);
399399

400400
$this->resource = $dest;
401401
}
@@ -822,4 +822,16 @@ private function getColor(ColorInterface $color)
822822

823823
return $index;
824824
}
825+
826+
/**
827+
* @param resource|\GdImage $resource
828+
*
829+
* @return void
830+
*/
831+
private static function destroyImage($resource)
832+
{
833+
if (PHP_VERSION_ID < 80500) {
834+
imagedestroy($resource);
835+
}
836+
}
825837
}

src/Gd/Imagine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ private function wrap($resource, PaletteInterface $palette, MetadataBag $metadat
181181

182182
imagecopy($truecolor, $resource, 0, 0, 0, 0, $width, $height);
183183

184-
imagedestroy($resource);
184+
if (PHP_VERSION_ID < 80500) {
185+
imagedestroy($resource);
186+
}
185187
$resource = $truecolor;
186188
}
187189
}

0 commit comments

Comments
 (0)