PHP 8.0 : \GDImage object is used instead of resource type
28, September 2020

From PHP 8.0, All GD functions with work with \GDImage class object instead of 'resource' type.

Similar to : CurlHandle Object

What is GD?

GD is an open source library for the dynamic creation of images. This functionality in available in PHP via an 'gd' extension. This extenstion provide several functions such as : imagecreate(), imagecreatetruecolor(), imagecopyresampled() to create and manipulate images.

So, How this change affects me?

If you are using GD library, This will slightly require you to look into your existing code if you have 'is_resource()' check in it. As gd functions now accept and return \GDImage class object (no longer resource), You need to update that check like below:


$image = imagecreatetruecolor(100, 50);

// PHP 7.4
if(is_resource($image) && get_resource_type($image) === 'gd') {
  * valid gd resource
  * do something with $image

// PHP 8.0
if(is_object($image) && $image instanceof \GDImage) {
  // valid gd resource
  // do something with $image 

// Combine both version to support both PHP 7 & 8
    (is_resource($image) && get_resource_type($image) === 'gd') ||
    (is_object($image) && $image instanceof \GDImage)
  // valid $image

Please Note: gd functions like imagecreate() returns false on failure in both PHP 7 & 8.

As, there is no 'resource' type anymore, imagedestroy() is now useless. It does nothing. PHP can free memory usage of \GDImage when it is not used anymore, or you can explicitly free by calling unset($image);

Limitations of GDImage:

  • GDImage is a final class, so we can't extend it.
  • It has no method.
  • It does not even has a public __construct() method, so we can't initialize it via new GDImage()
  • It can not be cloned or serialized.