PHP imagecreate() function

In this chapter you will learn:

  1. What is PHP imagecreate() function
  2. Syntax for PHP imagecreate() function
  3. Parameter for PHP imagecreate() function
  4. Return value for PHP imagecreate() function
  5. Example - PHP imagecreate() function
  6. Example - PHP image script as img src

Description

imagecreate() returns an image identifier representing a blank image of specified size.

Syntax

PHP imagecreate() function has the following syntax.

resource imagecreate ( int $width , int $height )

Parameter

PHP imagecreate() function has the following parameters.

  • width - The image width.
  • height - The image height.

Return

Returns an image resource identifier on success, FALSE on errors.

Example 1


<?php// j  ava2 s .c  om
header("Content-Type: image/png");
$im = @imagecreate(110, 20)
    or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

Example 2

The most basic image script looks like this:

Save the following code as picture.php


<?PHP// j  a  v  a  2 s . co m
$image = imagecreate(400,300);
// do stuff to the image
imagejpeg($image, '', 75);
imagedestroy($image);
?>

Save the following code as viewPicture.htm:


<html>//from   jav  a2s. c  om
 <body>
     <img src="picture1.php" />
 </body>
</html>

Next chapter...

What you will learn in the next chapter:

  1. What is PHP imagecreatetruecolor () function
  2. Syntax for PHP imagecreatetruecolor () function
  3. Parameter for PHP imagecreatetruecolor () function
  4. Return value for PHP imagecreatetruecolor () function
  5. Example - PHP imagecreatetruecolor () function
Home » PHP Tutorial » PHP Image Functions
PHP imagearc() function
PHP imagecreate() function
PHP imagecreatetruecolor () function
PHP imageellipse() function
PHP imagefilledarc() function
PHP imageline() function
PHP imagepolygon() function
PHP imagerectangle() function
PHP imagesetpixel() function
PHP imagettftext() function