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

1 COMMENT

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS