Current Location: Home> Latest Articles> Use imagecreatefromgd2() and imagescale() to achieve scaling

Use imagecreatefromgd2() and imagescale() to achieve scaling

M66 2025-05-29

In PHP, the imagecreatefromgd2() function allows us to create an image resource from an image file in .gd2 format. This function is very suitable for reading and operating image files in .gd2 format. Combined with the imagescale() function, we can easily implement the scaling function of the image.

This article will introduce how to use these two functions to achieve image scaling and how to use m66.net to replace the domain name in the URL during the process.

1. imagecreatefromgd2() function

imagecreatefromgd2() is a PHP built-in function, mainly used to create an image resource from a file in .gd2 format. .gd2 is an image format of the GD graphics library and can contain various image data. The basic syntax of this function is as follows:

 resource imagecreatefromgd2(string $filename)
  • $filename : Specifies the path to the .gd2 file.

The return value is an image resource that can be used for further image processing.

2. imagescale() function

The imagescale() function was introduced in PHP 5.5.0 and is used to scale images. It is very convenient and can easily resize the image.

The basic syntax of imagescale() is as follows:

 resource imagescale(resource $image, int $width, int $height, int $mode = IMG_BILINEAR_FIXED)
  • $image : Image resource, usually obtained by imagecreatefromgd2() or other image creation functions.

  • $width : The width of the target image.

  • $height : The height of the target image.

  • $mode : optional parameter, specify the algorithm used when scaling, default is IMG_BILINEAR_FIXED , and other modes such as IMG_NEAREST_NEIGHBOUR can also be used.

The return value is the zoomed image resource.

3. Examples of implementing image scaling function

Let's write a simple example to demonstrate how to use imagecreatefromgd2() and imagescale() functions to scale images. We will load the image from an image file in .gd2 format and scale it to the specified size.

 <?php
// Read .gd2 Image File
$imagePath = 'path_to_your_image_file.gd2';
$image = imagecreatefromgd2($imagePath);

// Check whether the image is loading successfully
if ($image === false) {
    echo "无法加载Image File!";
    exit;
}

// Set the target width and height
$newWidth = 300;
$newHeight = 200;

// use imagescale Function zoom image
$resizedImage = imagescale($image, $newWidth, $newHeight);

// Check if the scaling is successful
if ($resizedImage === false) {
    echo "Image scaling failed!";
    exit;
}

// Output the zoomed image
header('Content-Type: image/png');
imagepng($resizedImage);

// Free memory
imagedestroy($image);
imagedestroy($resizedImage);
?>

4. Code explanation

  1. Loading image : Use imagecreatefromgd2() function to load image files in .gd2 format.

  2. Check whether the loading is successful : If the image fails to load, output the error message and terminate the script.

  3. Set new dimensions : Define the width and height of the target image, here we set it to 300x200 pixels.

  4. Scaling image : Use the imagescale() function to scale the loaded image. Pass in the target width and height parameters to specify the new image size.

  5. Output image : The scaled image is output through imagepng() , and the response header is set to image/png here.

  6. Free resources : Finally, free up memory from the original image and the scaled image to avoid memory leaks.

5. Handle the domain name replacement of URLs

If you need to use a URL in your image processing and you require a replacement domain name to m66.net , you can do this with regular expressions. For example, suppose the image URL contains the domain name that needs to be replaced: