Skip to content
Related Articles

Related Articles

PHP | imagecreatefromgd2part() Function

Improve Article
Save Article
  • Last Updated : 14 Feb, 2020
Improve Article
Save Article

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('./geeksforgeeks.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('./geeksforgeeks.gd2',
                 30, 0, 100, 250);
  
// Convert to JPEG and save to local folder
imagejpeg($im, 'newgeeksforgeeks.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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!