Current Location: Home> Latest Articles> Use getimagesize() to check if the image is in GD2 format

Use getimagesize() to check if the image is in GD2 format

M66 2025-05-29

In PHP, the GD library provides powerful image processing capabilities. Among them, GD2 is a binary image format of the GD library, which is usually used to save images generated inside the GD library. If you need to determine whether an image is in GD2 format and try to load and process the image, you can combine getimagesize() and imagecreatefromgd2() to achieve it.

This article will explain in detail how to accomplish this task.

1. What is the GD2 image format?

GD2 is the image format represented by the .gd2 file extension and is a dedicated image data storage method for the GD library. This format is generally not used directly on web pages, but it still has value in some automated image processing scenarios.

2. Use getimagesize() to check image type

PHP's getimagesize() function can return the size of the image and its MIME type. Among the supported image types, common JPEG, PNG, GIF, BMP, etc. However, for images in .gd2 format, the function may not return the standard MIME type, but rather fail or return an unknown type.

Here is an example of using getimagesize() to check image information:

 <?php
$imagePath = 'https://m66.net/images/sample.gd2';
$imageInfo = @getimagesize($imagePath);

if ($imageInfo === false) {
    echo "Image type cannot be recognized,may be GD2 Format or other unsupported formats。\n";
} else {
    echo "The image type is:{$imageInfo['mime']}\n";
}
?>

Note: getimagesize() cannot clearly determine whether an image is in GD2 format, so we can only try to process it with other methods.

3. Use imagecreatefromgd2() to load GD2 images

imagecreatefromgd2() is a PHP function dedicated to loading .gd2 format images. If you already know that the file is in GD2 format or suspect it is GD2, you can try loading directly:

 <?php
$imagePath = 'sample.gd2';

if (file_exists($imagePath)) {
    $image = @imagecreatefromgd2($imagePath);
    if ($image === false) {
        echo "Unable to read GD2 image,Probably not valid GD2 Format。\n";
    } else {
        echo "Loaded successfully GD2 image!\n";

        // You can save it as PNG 等Format
        imagepng($image, 'output.png');
        imagedestroy($image);
    }
} else {
    echo "The file does not exist:$imagePath\n";
}
?>

4. How to determine whether the image is in GD2 format?

Although there is no direct function to determine whether the image is in GD2 format, the following ideas can be adopted:

  1. Try loading with getimagesize() : If it fails and the file extension is .gd2 , it is most likely in GD2 format.

  2. Try loading with imagecreatefromgd2() : If successful, it can be determined as GD2.

  3. Combined with MIME type and extension : If the MIME type is unknown and the extension is .gd2 , you should use imagecreatefromgd2() to test loading.

5. Summary

Although PHP does not have special functions to identify whether an image is in GD2 format, you can use the file extension, the return value of getimagesize() , and the tentative loading of imagecreatefromgd2() to achieve detection and processing.

Tip: GD2 image format is mostly used for internal image storage, and is not recommended to be used directly for web display. It is recommended to convert it to common formats (such as PNG or JPEG).