PHP | imagecreate() Function
Last Updated :
23 Aug, 2019
The imagecreate() function is an inbuilt function in PHP which is used to create a new image. This function returns the blank image of given size. In general imagecreatetruecolor() function is used instead of imagecreate() function because imagecreatetruecolor() function creates high quality images.
Syntax:
imagecreate( $width, $height )
Parameters: This function accepts two parameters as mentioned above and described below:
- $width: It is mandatory parameter which is used to specify the image width.
- $height: It is mandatory parameter which is used to specify the image height.
Return Value: This function returns an image resource identifier on success, FALSE on errors.
Below programs illustrate the imagecreate() function in PHP:
Program 1:
<?php
$image = imagecreate(500, 300);
$background_color = imagecolorallocate($image, 0, 153, 0);
$text_color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 5, 180, 100, "GeeksforGeeks", $text_color);
imagestring($image, 3, 160, 120, "A computer science portal", $text_color);
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>
|
Output:

Program 2:
<?php
$image = imagecreate(500, 300);
$values = array(
50, 50,
50, 250,
250, 50,
250, 250
);
$background_color = imagecolorallocate($image, 0, 153, 0);
imagefill($image, 0, 0, $background_color);
$image_color = imagecolorallocate($image, 255, 255, 255);
imagepolygon($image, $values, 4, $image_color);
header('Content-type: image/png');
imagepng($image);
?>
|
Output:

Related Articles:
Reference: http://php.net/manual/en/function.imagecreate.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...