Open In App

PHP | imagecreatefromgd() function

Improve
Improve
Like Article
Like
Save
Share
Report

The imagecreatefromgd() function is an inbuilt function in PHP which is used to create a new image from GD file or URL. Further, this image can be worked upon in the program. An image can be converted into GD format using the imagegd() function.

Syntax:

resource imagecreatefromgd( 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 imagecreatefromgd() function in PHP:
Program 1 (Viewing a gd file in browser):




<?php
// Load the GD image from localfile
$im = imagecreatefromgd('geeksforgeeks.gd');
  
// Output the image to browser
imagegd($im);
imagedestroy($im);
?>


Output:

This will load gd image in text form as it is not supported by browser.

Program 2 (Converting GD into png and viewing in browser):




<?php
// As GD isn't supported in browser, it can be
// converted into PNG to be viewed in browser
// Load the GD image
$im = imagecreatefromgd('geeksforgeeks.gd');
  
// Output the image to browser
header("Content-Type: image/png");
imagepng($im);
imagedestroy($im);
?>


Output:

Program 3 (Converting GD into jpg):




<?php
// Load the GD image
$im = imagecreatefromgd('geeksforgeeks.gd');
  
// Convert the image and save in local folder
imagejpeg($im, 'geeksforgeeks.jpg');
imagedestroy($im);
?>


Output:

This will convert GD image into JPEG and save in local folder.

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



Last Updated : 30 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads