-
Notifications
You must be signed in to change notification settings - Fork 189
Fix PHP 8.x & imagedestroy #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -61,7 +61,9 @@ public function fillBackground($background = 0xffffff) | |||||||||||||||||
| $n = imagecreatetruecolor($w, $h); | ||||||||||||||||||
| imagefill($n, 0, 0, ImageColor::gdAllocate($this->resource, $background)); | ||||||||||||||||||
| imagecopyresampled($n, $this->resource, 0, 0, 0, 0, $w, $h, $w, $h); | ||||||||||||||||||
| imagedestroy($this->resource); | ||||||||||||||||||
| if (PHP_VERSION_ID < 80000) { | ||||||||||||||||||
| imagedestroy($this->resource); | ||||||||||||||||||
| } | ||||||||||||||||||
| $this->resource = $n; | ||||||||||||||||||
|
|
||||||||||||||||||
| return $this; | ||||||||||||||||||
|
|
@@ -101,7 +103,9 @@ protected function doResize($bg, int $target_width, int $target_height, int $new | |||||||||||||||||
| $height | ||||||||||||||||||
| ); | ||||||||||||||||||
|
|
||||||||||||||||||
| imagedestroy($this->resource); | ||||||||||||||||||
| if (PHP_VERSION_ID < 80000) { | ||||||||||||||||||
| imagedestroy($this->resource); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+106
to
+108
|
||||||||||||||||||
|
|
||||||||||||||||||
| $this->resource = $n; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -117,7 +121,9 @@ public function crop($x, $y, $width, $height) | |||||||||||||||||
| imagealphablending($destination, false); | ||||||||||||||||||
| imagesavealpha($destination, true); | ||||||||||||||||||
| imagecopy($destination, $this->resource, 0, 0, (int) $x, (int) $y, $this->width(), $this->height()); | ||||||||||||||||||
| imagedestroy($this->resource); | ||||||||||||||||||
| if (PHP_VERSION_ID < 80000) { | ||||||||||||||||||
| imagedestroy($this->resource); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+124
to
+126
|
||||||||||||||||||
| $this->resource = $destination; | ||||||||||||||||||
|
|
||||||||||||||||||
| return $this; | ||||||||||||||||||
|
|
@@ -268,7 +274,9 @@ public function gaussianBlur($blurFactor = 1) | |||||||||||||||||
| imagefilter($this->resource, IMG_FILTER_GAUSSIAN_BLUR); | ||||||||||||||||||
|
|
||||||||||||||||||
| // clean up | ||||||||||||||||||
| imagedestroy($prevImage); | ||||||||||||||||||
| if (PHP_VERSION_ID < 80000) { | ||||||||||||||||||
|
||||||||||||||||||
| if (PHP_VERSION_ID < 80000) { | |
| if ( | |
| $prevImage !== $this->resource | |
| && ( | |
| is_resource($prevImage) | |
| || (class_exists('\\GdImage') && $prevImage instanceof \GdImage) | |
| ) | |
| ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about using destroy in PHP 8.5 and above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
destroy ? We could perhaps use unset(), but I don't think it's necessary: https://stackoverflow.com/questions/79522994/in-php-8-how-to-clear-image-data-from-script-previously-imagedestroy
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This introduces another PHP-version-based special case for resource cleanup. To reduce duplication and avoid tying behavior to a hardcoded version threshold, prefer a shared helper that safely calls imagedestroy() only when the variable is a valid GD image (resource/GdImage).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PHP version gate around
imagedestroy()is likely the wrong criterion here: in PHP 8+imagedestroy()still exists and acceptsGdImage, so skipping it based solely onPHP_VERSION_IDchanges cleanup behavior without guaranteeing the underlying issue is addressed. If the goal is to avoid type errors / invalid-handle destroys, guard on the actual value (e.g., only call when the variable is a GD image resource /GdImage), and consider centralizing this into a small helper to avoid duplicating the same version check in multiple methods.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please read the doc: https://www.php.net/manual/en/function.imagedestroy.php