Open In App

PHP | imagecreatefromgd2() function

Last Updated : 30 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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('geeksforgeeks.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('geeksforgeeks.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
 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads