Open In App

PHP | imagegd2() Function

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

The imagegd2() function is an inbuilt function in PHP which is used to output GD2 image to browser or file. This is most useful to convert any other image type to gd2. The imagecreatefromgd2() function can be used to further read gd2 images.

Syntax:

bool imagegd2( resource $image, float $to, 
             int $chunk_size, int $type )

Parameters: This function accepts four 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.
  • $chunk_size (Optional): It specifies the size of chunk.
  • $type (Optional): It specifies the type of compression to use which can be one of IMG_GD2_RAW or IMG_GD2_COMPRESSED.

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

Exceptions: This function throws Exception on error.

Below examples illustrate the imagegd2() function in PHP:

Example 1: Viewing a GD2 file.




<?php
  
// Create a blank image and add text
$im = imagecreatetruecolor(200, 200);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 0, 30, 30,  "GeeksforGeeks", $text_color);
  
// Output the image as string
imagegd2($im);
?>


Output:

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

Example 2: Convert images into GD2.




<?php
  
// Create a image from png
$im = imagecreatefrompng(
  
// Convert into GD2 and save to the same folder
imagegd2($im, 'geeksforgeeks.gd2');
?>


Output:

This will save a image with name geeksforgeeks.gd2 in the same folder.

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads