Current Location: Home> Latest Articles> How to convert GD2 images to PNG format using the imagecreatefromgd2 function and process them in conjunction with the imagepng() function?

How to convert GD2 images to PNG format using the imagecreatefromgd2 function and process them in conjunction with the imagepng() function?

M66 2025-06-05

In PHP, a common requirement for processing images is to convert images in different formats into one of the common formats, such as PNG. The GD image library provides powerful functions to handle various image formats, where imagecreatefromgd2() can be used to read image files in .gd2 format, while imagepng() can output images into PNG format.

Below we will explain in detail how to use these two functions to complete the conversion from GD2 to PNG and provide a complete example.

1. Introduction to imagecreatefromgd2() function

imagecreatefromgd2() is one of the GD library functions provided by PHP to create an image resource from a GD2 image file.

grammar:

 resource imagecreatefromgd2 ( string $filename )
  • $filename : The path to read .gd2 file.

  • Return value: Return image resource when successful, and return false when failure.

2. Introduction to imagepng() function

imagepng() is used to output or save image resources to PNG format.

grammar:

 bool imagepng ( resource $image [, string $filename [, int $quality [, int $filters ]]] )
  • $image : Image resource.

  • $filename (optional): The path to the output file, if not specified, it will be output directly to the browser.

  • $quality (optional): PNG compression quality, ranging from 0 (no compression) to 9 (maximum compression), default is -1 (auto selected).

  • $filters (optional): Specifies the filter applied to the PNG, which does not generally require changes.

3. Complete example: Convert GD2 images to PNG files

Here is a complete PHP script example for reading and saving .gd2 images as .png files:

 <?php
// set up GD2 File path
$gd2_file = 'https://m66.net/images/sample.gd2';

// Local save path(In actual applications, you should download it first GD2 document)
$local_gd2 = 'sample.gd2';
file_put_contents($local_gd2, file_get_contents($gd2_file));

// from GD2 document创建图像资源
$image = imagecreatefromgd2($local_gd2);

// Check if the image is loaded successfully
if (!$image) {
    die('无法from GD2 document创建图像资源。');
}

// set up输出 PNG document名
$output_file = 'output_image.png';

// Save image resources as PNG document,The compression quality is 6
if (imagepng($image, $output_file, 6)) {
    echo 'PNG The image has been saved successfully as:' . $output_file;
} else {
    echo 'keep PNG Image failed。';
}

// Destroy image resources,Free memory
imagedestroy($image);
?>

4. Things to note

  1. GD library support : Make sure that the GD library is enabled in your PHP environment, otherwise these functions will not be available.

  2. GD2 file format : .gd2 is a specific format of the GD library. It is usually not used directly and needs to be generated in advance through relevant tools or scripts.

  3. File path security : When handling user uploads or external resources, please pay attention to path verification to avoid the risk of accessing arbitrary files.

  4. Browser output image : If you want to display the image directly in the browser instead of saving it as a file, you can omit the second parameter of imagepng() and set the correct Content-Type :

 header('Content-Type: image/png');
imagepng($image);
  • Related Tags:

    PNG