Home >Backend Development >PHP Tutorial >How to create a new truecolor image in PHP using imagecreatetruecolor()?

How to create a new truecolor image in PHP using imagecreatetruecolor()?

WBOY
WBOYforward
2023-09-06 17:25:041745browse

imagecreatetruecolor( ) is a built-in function in PHP that creates a new true color image. It returns a blank image of a given size.

Syntax

resource imagecreatetruecolor($width, $height)

Parameters

imagecreatetruecolor() takes two parameters, $width and $Height.

  • $Width - The $width parameter is used to set the image width.

  • $height - The $height parameter is used to set the image height.

Return value

imagecreatetruecolor() Returns the resource identifier if the image is successful, and returns false if there is an error.

Example 1

<?php
   // Set the vertices of polygon
   $values = array(
      150, 50, // Point 1 (x, y)
      50, 250, // Point 2 (x, y)
      250, 250 // Point 3 (x, y)
   );
   // Create the size of image or blank image
   $image = imagecreatetruecolor(700, 350);

   // Set the background color of image
   $background_color = imagecolorallocate($image, 122, 122, 122);

   // Fill background with above selected color
   imagefill($image, 0, 0, $background_color);

   // Allocate a color for the polygon
   $image_color = imagecolorallocate($image, 0, 255, 255);

   // Draw the polygon
   imagepolygon($image, $values, 3, $image_color);

   // Output the picture to the browser
   header(&#39;Content-type: image/png&#39;);

   imagepng($image);
?>

Output

How to create a new truecolor image in PHP using imagecreatetruecolor()?

Example 2 - The following PHP code will create a new GD image stream

<?php
   header (&#39;Content-Type: image/gif&#39;);
   $img = @imagecreatetruecolor(550, 220)
   or die(&#39;Cannot Initialize new GD image stream&#39;);
   $text_color = imagecolorallocate($img, 255, 255, 0);
   
   imagestring($img, 10, 50, 50, &#39;A Simple PHP Example&#39;, $text_color);
   imagepng($img);
   imagedestroy($img);
?>

Output

How to create a new truecolor image in PHP using imagecreatetruecolor()?

The above is the detailed content of How to create a new truecolor image in PHP using imagecreatetruecolor()?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete