Current Location: Home> Latest Articles> How to Use imagecolorclosest and imagecreatetruecolor Functions Together? Detailed Example Explained

How to Use imagecolorclosest and imagecreatetruecolor Functions Together? Detailed Example Explained

gitbox 2025-06-22

1. imagecreatetruecolor() Function Explained

The imagecreatetruecolor() function is used to create a true color image. It returns an image resource with specified width and height that can be used for subsequent image operations such as color filling, drawing shapes, or outputting the image.

Function Syntax:

<span><span>resource </span><span><span class="hljs-title function_ invoke__">imagecreatetruecolor</span></span><span>(</span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$width</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$height</span></span><span>);
</span></span>
  • $width: The width of the image in pixels.

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

Example:

<span><span><span class="hljs-comment">// Create a true color image with width 200px and height 100px</span></span><span>
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatetruecolor</span></span><span>(</span><span><span class="hljs-number">200</span></span><span>, </span><span><span class="hljs-number">100</span></span><span>);
</span></span>

2. imagecolorclosest() Function Explained

The imagecolorclosest() function finds the closest color in the palette to the given RGB values. It returns the color index in the palette that is closest to the target color, which is particularly useful when working with palette-based images.

Function Syntax:

<span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-title function_ invoke__">imagecolorclosest</span></span><span>(resource </span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$red</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$green</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$blue</span></span><span>);
</span></span>
  • $image: The image resource.

  • $red, $green, $blue: The RGB values of the target color, ranging from 0 to 255.

Example:

<span><span><span class="hljs-comment">// Get the color index closest to RGB(255, 0, 0) (red)</span></span><span>
</span><span><span class="hljs-variable">$colorIndex</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecolorclosest</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-number">255</span></span><span>, </span><span><span class="hljs-number">0</span></span><span>, </span><span><span class="hljs-number">0</span></span><span>);
</span></span>

3. How to Use imagecreatetruecolor() and imagecolorclosest() Together?

Suppose we want to draw a rectangle on a true color image and ensure the rectangle's color is the closest red color available in the image's palette.

Steps:

  1. Create a true color image using imagecreatetruecolor().

  2. Find the closest palette color index to the target color using imagecolorclosest().

  3. Draw a rectangle on the image using imagefilledrectangle(), filling it with the closest red color.

Complete Code Example:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Create a 400x200 true color image</span></span><span>
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatetruecolor</span></span><span>(</span><span><span class="hljs-number">400</span></span><span>, </span><span><span class="hljs-number">200</span></span><span>);
<p></span>// Get the closest color index to RGB(255, 0, 0)<br>
$redIndex = imagecolorclosest($image, 255, 0, 0);</p>
<p>// Draw a rectangle on the image filled with red<br>
imagefilledrectangle($image, 50, 50, 350, 150, $redIndex);</p>
<p>// Output the image<br>
header('Content-Type: image/png');<br>
imagepng($image);</p>
<p>// Destroy the image resource<br>
imagedestroy($image);<br>
?><br>
</span>

4. Notes

  • imagecolorclosest() is intended for palette-based images. If you use imagecreatetruecolor() to create a true color image (24-bit), imagecolorclosest() may not behave as expected because it is designed for palette images. For true color images, it is recommended to use imagecolorallocate() or imagecolorallocatealpha() to allocate colors directly.

  • In PHP, true color images (created by imagecreatetruecolor()) are 24-bit color (each pixel represented by 3 bytes), whereas palette-based images store colors using indexed palettes.

5. Summary

By combining imagecreatetruecolor() and imagecolorclosest(), you can handle image colors more flexibly, especially when converting images to palette-based formats. However, when working with true color images, imagecolorallocate() and imagecolorallocatealpha() are more appropriate choices. Regardless of the method, understanding the image color model and the functions’ purposes is fundamental for effective image processing.