claviska / SimpleImage

A PHP class that makes working with images and GD as simple as possible.
MIT License
1.38k stars 382 forks source link

Images not destroyed? #325

Closed rasteiner closed 1 year ago

rasteiner commented 1 year ago

Since PHP8 the GD functions work with the GdImage class and no longer with resources. The __destruct function: https://github.com/claviska/SimpleImage/blob/a447473883e5dd0daa0d014ae849f0db5b8a1d08/src/claviska/SimpleImage.php#L116-L124 however checks if the the current image is both a GdImage as well as a resource. But as far as I can test, GdImages are not resources:

$img = imagecreatetruecolor(10,10);

var_dump($img); // instance of class GdImage
var_dump(is_resource($img)); // false

Run code in sandbox

Therefore the conditions are never met and the image is never destroyed.

Since PHP 7 support has been dropped, the function could be rewritten as:

    protected $image = null;

    // snip

    /**
     * Destroys the image resource.
     */
    public function __destruct()
    {
        if ($this->image instanceof \GdImage) {
            imagedestroy($this->image);
        }
    }
claviska commented 1 year ago

Good catch. This was left over from a much earlier version — perhaps PHP 5.6. Would you like to send a PR for this update?