Related Articles

Related Articles

PHP | imagecreatefromgd2() function
  • Last Updated : 30 Jan, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
?>

chevron_right


Output: 
 

Program 2 (Converting gd2 into jpeg):

php

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
?>

chevron_right


Output: 
 

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :