imagecreate() function in PHP


The imagecreate() function is used to create a new image. It is preferred to use imagecreatetruecolor() to create an image instead of imagecreate(). This is because the image processing occurs on the highest quality image possible which can be created using imagecreatetruecolor().

Syntax

imagecreate( $width, $height )

Parameters

  • width: The width of image

  • height: The height of image

Return

The imagecreate() function returns an image resource identifier on success or FALSE on errors.

Example

The following is an example:

<?php
   $img = imagecreate(500, 300);
   $bgcolor = imagecolorallocate($img, 150, 200, 180);
   $fontcolor = imagecolorallocate($img, 120, 60, 200);
   imagestring($img, 12, 150, 120, "Demo Text1", $fontcolor);
   imagestring($img, 3, 150, 100, "Demo Text2", $fontcolor);
   imagestring($img, 9, 150, 80, "Demo Text3", $fontcolor);
   imagestring($img, 12, 150, 60, "Demo Text4", $fontcolor);
   header("Content-Type: image/png");
   imagepng($img);
   imagedestroy($img);
?>

Output

The following is the output:

Updated on: 31-Dec-2019

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements