Skip to content

Commit e828abe

Browse files
committed
only run imagedestroy on resources
On PHP 8+, the GD library no longer works on resources that need to be manually destroyed. Instead Image objects are passed around that are automatically garbage collected.
1 parent 11cda89 commit e828abe

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

phpunit.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit backupGlobals="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
backupGlobals="false"
45
backupStaticAttributes="false"
5-
bootstrap="vendor/autoload.php"
6+
bootstrap="tests/bootstrap.php"
67
colors="true"
78
convertErrorsToExceptions="true"
89
convertNoticesToExceptions="true"
910
convertWarningsToExceptions="true"
1011
processIsolation="false"
1112
stopOnFailure="false">
12-
13+
<coverage processUncoveredFiles="true">
14+
<include>
15+
<directory suffix=".php">./src/</directory>
16+
</include>
17+
</coverage>
1318
<testsuites>
1419
<testsuite name="all">
1520
<directory suffix="Test.php">./tests</directory>
1621
</testsuite>
1722
</testsuites>
18-
<filter>
19-
<whitelist processUncoveredFilesFromWhitelist="true">
20-
<directory suffix=".php">./src/</directory>
21-
</whitelist>
22-
</filter>
2323
</phpunit>

src/GdAdapter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function __construct($imagepath, $options = [])
3030
*/
3131
public function __destruct()
3232
{
33+
// destroy the GD image resource (only needed on PHP < 8.0)
3334
if (is_resource($this->image)) {
3435
imagedestroy($this->image);
3536
}
@@ -106,7 +107,7 @@ public function rotate($orientation)
106107
imageflip($image, IMG_FLIP_HORIZONTAL);
107108
}
108109

109-
imagedestroy($this->image);
110+
$this->__destruct(); // destroy old image
110111
$this->image = $image;
111112

112113
//keep png alpha channel if possible
@@ -163,7 +164,7 @@ public function save($path, $extension = '')
163164
$saver($this->image, $path);
164165
}
165166

166-
imagedestroy($this->image);
167+
$this->__destruct();
167168
}
168169

169170
/**
@@ -436,7 +437,7 @@ protected function resizeOperation($toWidth, $toHeight, $offsetX = 0, $offsetY =
436437
}
437438

438439
// destroy original GD image ressource and replace with new one
439-
imagedestroy($this->image);
440+
$this->__destruct();
440441
$this->image = $newimg;
441442
$this->width = $toWidth;
442443
$this->height = $toHeight;

tests/TestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ public function getColor($path, $position)
9999
$image = $opener($path);
100100
$rgb = imagecolorat($image, $x, $y);
101101
$found = imagecolorsforindex($image, $rgb);
102-
imagedestroy($image);
102+
if(is_resource($image)) {
103+
imagedestroy($image);
104+
}
103105
return $found;
104106
}
105107

0 commit comments

Comments
 (0)