PHP provides GD extension which can be used for creating and manipulating images. Before PHP 8.0, the imagedestroy function was used to close image resource created with various GD extension functions. Since PHP 8.0, the imagedestroy function has no effect because the GD extension now uses GdImage object instead of resource. The GdImage object is automatically closed when it has no remaining references or when it is explicitly destroyed.
Since PHP 8.5, the imagedestroy function triggers a deprecation warning:
<?php
$img = imagecreatetruecolor(200, 200);
imagepng($img, 'test.png');
imagedestroy($img);
Example will output:
Deprecated: Function imagedestroy() is deprecated since 8.5, as it has no effect since PHP 8.0 in ...
In the previous example, the deprecation warning can be resolved simply by removing the imagedestroy call. The GdImage object will be closed automatically when $img is no longer referenced, such as when it goes out of scope. We can also close it explicitly by calling unset($img) or by setting $img = null.
<?php
$img = imagecreatetruecolor(200, 200);
imagepng($img, 'test.png');
unset($img);
Leave a Comment
Cancel reply