Friday, October 24, 2025
HomeLanguagesPHP | imagegd() function

PHP | imagegd() function

The imagegd() function is an inbuilt function in PHP which is used to output GD image to browser or file. This is most useful to convert any other image type to gd. imagecreatefromgd() function can be used to further read gd images. 

Syntax:

bool imagegd( resource $image, float $to)

Parameters:This function accepts two parameters as mentioned above and described below:

  • $image: It specifies the image to be worked upon.
  • $to (Optional): It specifies the path to save the file to.

Return Value: This function returns TRUE on success or FALSE on failure. 

Exceptions: This function throws Exception on error. 

Below given programs illustrate the imagegd() function in PHP: 
Program 1 (Viewing a GD file): 

php




<?php
// Create a blank image and add text
$im = imagecreatetruecolor(100, 100);
$text_color = imagecolorallocate($im, 10, 10, 51);
imagestring($im, 0, 20, 20,  "neveropen", $text_color);
 
// Output the image as string
imagegd($im);
?>


Output:

This will output the image in the form of string as gd isn't supported in browser.

Program 2 (Convert images into gd): 

php




<?php
// Create a image from png
 
// Convert into GD and save to the same folder
imagegd($image, 'neveropen.gd');
?>


Output:

This will save a image with name neveropen.gd in the same folder.

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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS