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
Release: 2023-09-06 17:26:02
forward
1743 people have browsed it

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)
Copy after login

Parameters

imagecreatetruecolor() takes two parameters, $width and $< /span>Height.

  • $Width - The $< /span>width parameter is used to set the image width.

  • $height - The $< /span>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);
?>
Copy after login

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);
?>
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template