Using WordPress ‘is_gd_image()’ PHP function

The is_gd_image() WordPress PHP function determines whether the value is an acceptable type for GD image functions.

Usage

is_gd_image($image);

Parameters

  • $image (resource|GdImage|false) – A value to check the type for.

More information

See WordPress Developer Resources: is_gd_image()

Examples

Check if the value is a GD image

This code checks if $image is a valid GD image type.

$image = imagecreatefromjpeg('path/to/image.jpg');
if (is_gd_image($image)) {
    echo 'This is a valid GD image.';
} else {
    echo 'This is not a valid GD image.';
}

Check if an image file is a GD image

This code checks if an image file is a valid GD image type by creating a new GD image from the file.

$image_path = 'path/to/image.png';
$image = imagecreatefrompng($image_path);
if (is_gd_image($image)) {
    echo 'This is a valid GD image.';
} else {
    echo 'This is not a valid GD image.';
}

Check if a variable is not a GD image

This code checks if a variable is not a valid GD image type.

$not_an_image = 'Hello, World!';
if (!is_gd_image($not_an_image)) {
    echo 'This is not a valid GD image.';
}

Check if an image created from a string is a GD image

This code checks if an image created from a string is a valid GD image type.

$data = file_get_contents('path/to/image.jpg');
$image = imagecreatefromstring($data);
if (is_gd_image($image)) {
    echo 'This is a valid GD image.';
} else {
    echo 'This is not a valid GD image.';
}

Check if a newly created GD image is a GD image

This code checks if a newly created GD image is a valid GD image type.

$width = 200;
$height = 100;
$image = imagecreatetruecolor($width, $height);
if (is_gd_image($image)) {
    echo 'This is a valid GD image.';
} else {
    echo 'This is not a valid GD image.';
}