Tuesday, June 16, 2026
HomeLanguagesPHP | imagecreatefromgd2part() Function

PHP | imagecreatefromgd2part() Function

The imagecreatefromgd2part() function is an inbuilt function in PHP which is used to create a new image from a given part of GD2 file or URL. Further, this image can be worked upon in the program. Usually, gd2 extension is not supported in the browser thus it can be used to convert it into png and view it in the browser.

Syntax:

resource imagecreatefromgdpart( string $filename,
         int $srcX, int $srcY, int $width, int $height )

Parameters: This function accept five parameters as mentioned above and described below:

  • $filename: It specifies the GD2 image.
  • $srcX: It specifies the x-coordinate of source point.
  • $srcY: It specifies the y-coordinate of source point.
  • $width: It specifies the width of image.
  • $height: It specifies the height of image.

Return Value: This function returns an image resource identifier on success, FALSE on errors.

Below given programs illustrate the imagecreatefromgd2part() function in PHP:

Program 1 (Viewing a gd2 file in browser):




<?php
  
// Load the GD2 image from local folder GD2 images
// can be created with imagegd2() function
$im = imagecreatefromgd2part('./neveropen.gd2',
                   300, 0, 300, 300);
  
// Convert to PNG and output to the browser
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>


Output:

Program 2 (Converting gd2 into jpeg):




<?php
  
// Load the GD2 image from local folder GD2 images
// can be created with imagegd2() function
$im = imagecreatefromgd2part('./neveropen.gd2',
                 30, 0, 100, 250);
  
// Convert to JPEG and save to local folder
imagejpeg($im, 'newneveropen.jpeg');
imagedestroy($im);
?>


Output:

It will load the part of image and convert it
into JPEG and saves it into same folder.

Reference: https://www.php.net/manual/en/function.imagecreatefromgd2part.php

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS