Friday, September 5, 2025
HomeLanguagesPHP | imagecreatefromgd2() function

PHP | imagecreatefromgd2() function

The imagecreatefromgd2() function is an inbuilt function in PHP which is used to create a new image from 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 imagecreatefromgd2( string $filename )

Parameters: This function accepts a single parameter $filename which holds the name or URL of file.
Return Value: This function returns an image resource identifier on success, FALSE on errors.

Below given programs illustrate the imagecreatefromgd2() function in PHP:
Program 1 (Viewing a gd2 file in browser):  

php




<?php
// Load the GD2 image from local folder
// GD2 images can be created with imagegd2() function
$im = imagecreatefromgd2('neveropen.gd2');
  
// 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




<?php
// Load the GD2 image
$im = imagecreatefromgd2('neveropen.gd2');
   
// Convert it to a JPEG file, press Ctrl + S to save the new jpeg image
header('Content-type: image/jpeg');  
imagejpeg($im);
imagedestroy($im);
?>


Output: 
 

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

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11861 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6699 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS