imagedestroy

imagedestroy($image)

In PHP 7 it freed the memory of a GD image. Since PHP 8.0 the image is a GdImage object and the memory is freed by itself as soon as no variable refers to the image – the function does nothing and since PHP 8.5 it prints a Deprecated notice when called. It returns true.

Parameters

  • $image – a GD image.

Examples

$photo = imagecreatefromjpeg("photo.jpg");
$thumb = imagescale($photo, 200);
imagejpeg($thumb, "thumbnails/thumb.jpg", 80);
// PHP 7.4: free the memory of a large photo before further processing
var_dump(imagedestroy($photo)); // bool(true)
// PHP 8: unsetting the variable is enough
unset($thumb);

Things to watch out for

  • Code that must also run in PHP 7.4 can keep the call; in PHP 8.5+ it prints Deprecated, though. Without notices in both versions: if (PHP_VERSION_ID < 80000) imagedestroy($photo); and otherwise unset($photo).
  • When processing many photos in a loop, free the images as you go (unset, or by overwriting the variable), otherwise the memory runs out.

Related functions:
imagecreatefromjpeg()
unset()
memory_get_usage()