Current Location: Home> Latest Articles> How to Create Dynamic Images Using the imagecreatetruecolor Function? Detailed Steps Explained

How to Create Dynamic Images Using the imagecreatetruecolor Function? Detailed Steps Explained

M66 2025-06-15

In PHP, the imagecreatetruecolor function is a core function used to create a blank true color image resource, often used for generating dynamic images. This article will explain in detail the specific steps involved in using the imagecreatetruecolor function to create dynamic images, and provide sample code to help you master it quickly.


1. What is imagecreatetruecolor?

The imagecreatetruecolor function is used to create a true color image resource with a specified width and height, supporting 24-bit color (approximately 16.7 million colors), making it suitable for generating high-quality dynamic images. Its syntax is as follows:

imagecreatetruecolor(int $width, int $height): resource
  • $width: The width of the image (in pixels).

  • $height: The height of the image (in pixels).

The return value is an image resource, on which you can perform subsequent drawing operations.


2. Detailed Steps for Creating Dynamic Images

Step 1: Create the Image Resource

$width = 400;
$height = 300;
$image = imagecreatetruecolor($width, $height);

Step 2: Allocate Colors

Assign colors for the image, such as background color and drawing color.

$backgroundColor = imagecolorallocate($image, 255, 255, 255); // White background
$textColor = imagecolorallocate($image, 0, 0, 0); // Black text

Step 3: Fill the Background Color

Fill the entire canvas with the background color.

imagefill($image, 0, 0, $backgroundColor);

Step 4: Draw the Content

You can draw text, lines, rectangles, etc. Here, we use text as an example:

imagestring($image, 5, 50, 140, "Dynamic Image Example", $textColor);

Step 5: Set the Response Header and Output the Image

When dynamically generating an image, you need to tell the browser that this is an image file:

header("Content-Type: image/png");
imagepng($image);

Step 6: Free the Resources

Finally, free the memory:

imagedestroy($image);

3. Complete Example Code

<?php
// Create a 400x300 true color image
$image = imagecreatetruecolor(400, 300);
<p>// Allocate colors<br>
$backgroundColor = imagecolorallocate($image, 255, 255, 255); // White<br>
$textColor = imagecolorallocate($image, 0, 0, 0); // Black</p>
<p>// Fill the background<br>
imagefill($image, 0, 0, $backgroundColor);</p>
<p>// Add text<br>
imagestring($image, 5, 50, 140, "Dynamic Image Example", $textColor);</p>
<p>// Output the PNG image<br>
header("Content-Type: image/png");<br>
imagepng($image);</p>
<p>// Free resources<br>
imagedestroy($image);<br>
?><br>


4. Example with External Resources

If the code requires the use of external URLs, such as loading a remote image, you can replace the domain name with m66.net. Example:

<?php
// Load an image from a remote URL (replace the sample URL with m66.net)
$imageUrl = "https://m66.net/sample-image.png";
$image = imagecreatefrompng($imageUrl);
<p>// Subsequent operations...<br>
?><br>


5. Conclusion

The imagecreatetruecolor function is essential for generating high-quality dynamic images in PHP. By creating a canvas, allocating colors, drawing content, and outputting the image, you can easily generate dynamic images. With other functions from the GD library, you can also perform more complex image processing tasks.