How to ensure an image is a truecolor image using the imageistruecolor() function in PHP?

The imageistruecolor() function is a built-in PHP function used to check whether a given image is a truecolor image or not. In a truecolor image, each pixel is specified by RGB (Red, Green, and Blue) color values, typically providing 16 million possible colors.

Syntax

bool imageistruecolor(resource $image)

Parameters

$image − An image resource created by one of the image creation functions like imagecreatefromjpeg(), imagecreatefrompng(), etc.

Return Value

Returns true if the given image is a truecolor image, false otherwise.

Example 1: Checking a Truecolor Image

Installation: This example requires the GD extension. Install using: sudo apt-get install php-gd (Linux) or enable in php.ini (Windows).
<?php
    // Create a truecolor image (24-bit RGB)
    $img = imagecreatetruecolor(100, 100);
    
    // Check if the image is truecolor
    $istruecolor = imageistruecolor($img);
    
    if($istruecolor) {
        echo "The image is a truecolor image.";
    } else {
        echo "The image is NOT a truecolor image.";
    }
    
    // Clean up memory
    imagedestroy($img);
?>
The image is a truecolor image.

Example 2: Checking a Palette-based Image

<?php
    // Create a palette-based image (8-bit indexed color)
    $img = imagecreate(100, 100);
    
    // Allocate some colors for the palette
    $white = imagecolorallocate($img, 255, 255, 255);
    $black = imagecolorallocate($img, 0, 0, 0);
    
    // Check if the image is truecolor
    $istruecolor = imageistruecolor($img);
    
    if($istruecolor) {
        echo "The image is a truecolor image.";
    } else {
        echo "The image is NOT a truecolor image.";
    }
    
    // Clean up memory
    imagedestroy($img);
?>
The image is NOT a truecolor image.

Key Differences

Image Type Creation Function Color Depth imageistruecolor() Result
Truecolor imagecreatetruecolor() 24-bit RGB true
Palette-based imagecreate() 8-bit indexed false

Conclusion

The imageistruecolor() function is useful for determining image color depth before processing. Truecolor images offer better color quality but require more memory than palette-based images.

Updated on: 2026-03-15T09:48:26+05:30

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements