Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

imagetruecolortopalette

Description

Convert a true color image to a palette image

Syntax

imagetruecolortopalette(
    GdImage $image,
    bool $dither,
    int $num_colors
): bool

Parameters

image

A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().

dither

Indicates if the image should be dithered - if it is true then dithering will be used which will result in a more speckled image but with better color approximation.

num_colors

Sets the maximum number of colors that should be retained in the palette.

Return

Returns true on success or false on failure.

Examples

1 · return

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$dither = false;
$num_colors = 255;

$return = imagetruecolortopalette($image, $dither, $num_colors);

var_export($return);

?>
true

2 · base64 · image

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$red = 255;
$green = 0;
$blue = 0;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 25;
$center_y = 25;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$red = 0;
$green = 255;
$blue = 0;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 50;
$center_y = 50;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$red = 0;
$green = 0;
$blue = 255;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 75;
$center_y = 75;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$dither = false;
$num_colors = 4;
imagetruecolortopalette($image, $dither, $num_colors);

ob_start();

    imagepng($image);

$output = ob_get_clean();

echo '<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,' . base64_encode($output) . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAgMAAAANjH3HAAAADFBMVEUEAgQE/gT8AgQEAvwy7A8JAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABWElEQVRIib3RMW6DMBgFYEPkxUsnLuFLtEfogNWRoySX6J4FqeEUHIkDINEa8O/n3/4LS+Ip0ie/90yU8scOd1U8dhjKVA/+SFfKl1YYfnIwmww3IawUt0MeVwfJ1hkSXmRJeJEsBHxCHMAnyGJAbifFgtyfKADsqU+T3rVl6Z1LKIrz56Mg/SptQb5Xcfk3eGzgPjPpd2kz2cMgLkiAuM6kNVBk0hooqo+EBsAENgAm2P/lAZKOK0qdTsPvI4sVxYii0oeyP0+QWpS/S5IoC/K1LEgNQkJRrl6mgqxX8BJJt8lIooNssMyZ7GEQp9MwiAtyDULrqrQGiipWA0VH0kWhCU06ACboA4nAx5VEsWkwrhFFi1KJ4uO6smhRlCxaFKVFUW8vksuLRAHMZ+U9ynRW4KljKjBOnZU4buZCEyYuNGHkcpFqqCirobgsjOJy2Ndly+iS//EL5DaB3w62iJMAAAAASUVORK5CYII=">
</body>
</html>

3 · base64 · dither · false

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$red = 255;
$green = 0;
$blue = 0;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 25;
$center_y = 25;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$red = 0;
$green = 255;
$blue = 0;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 50;
$center_y = 50;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$red = 0;
$green = 0;
$blue = 255;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 75;
$center_y = 75;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$dither = false;
$num_colors = 3;
imagetruecolortopalette($image, $dither, $num_colors);

ob_start();

    imagepng($image);

$output = ob_get_clean();

echo '<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,' . base64_encode($output) . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAgMAAAANjH3HAAAACVBMVEUEAk4E/gT8AgTi7KbHAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABC0lEQVRIie3SvRGDMAwFYOE7GvdZwlMwAgXehyXS03AXPGXCj40s6YU0dFH7nZ+eDUTrhDSROSElm1xaBx2xD22QXhr8LmkEYVbcATrOZVHtfBG5KBSRi7AUkBXOArICFs9k/FECk+lGYSCuepvMcbBljrGiU+I6nSHzJoMhz02ifoNlh9grmQ8ZlBxhLC5LhrOdr9ewRb5ewxa5KykFWAVRgFUI32VhUpczxdXV+PtgCVA8FKovKj4eEAflcwgJBSbV30j0QMCkQyKPnNJLaa1elaiwIiqsiGxG1KA1WfQaoivRBfJVdYFcDosBRzlLCFXbK9jSQmmgrHHWRfc4IISlhUItlP/85+55AyCilqVmPKUOAAAAAElFTkSuQmCC">
</body>
</html>

4 · base64 · dither · true

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$red = 255;
$green = 0;
$blue = 0;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 25;
$center_y = 25;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$red = 0;
$green = 255;
$blue = 0;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 50;
$center_y = 50;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$red = 0;
$green = 0;
$blue = 255;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 75;
$center_y = 75;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$dither = true;
$num_colors = 3;
imagetruecolortopalette($image, $dither, $num_colors);

ob_start();

    imagepng($image);

$output = ob_get_clean();

echo '<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,' . base64_encode($output) . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAgMAAAANjH3HAAAACVBMVEUEAk4E/gT8AgTi7KbHAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABC0lEQVRIie3SvRGDMAwFYOE7GvdZwlMwAgXehyXS03AXPGXCj40s6YU0dFH7nZ+eDUTrhDSROSElm1xaBx2xD22QXhr8LmkEYVbcATrOZVHtfBG5KBSRi7AUkBXOArICFs9k/FECk+lGYSCuepvMcbBljrGiU+I6nSHzJoMhz02ifoNlh9grmQ8ZlBxhLC5LhrOdr9ewRb5ewxa5KykFWAVRgFUI32VhUpczxdXV+PtgCVA8FKovKj4eEAflcwgJBSbV30j0QMCkQyKPnNJLaa1elaiwIiqsiGxG1KA1WfQaoivRBfJVdYFcDosBRzlLCFXbK9jSQmmgrHHWRfc4IISlhUItlP/85+55AyCilqVmPKUOAAAAAElFTkSuQmCC">
</body>
</html>

5 · base64 · num_colors

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$red = 255;
$green = 0;
$blue = 0;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 25;
$center_y = 25;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$red = 0;
$green = 255;
$blue = 0;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 50;
$center_y = 50;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$red = 0;
$green = 0;
$blue = 255;
$color = imagecolorallocate($image, $red, $green, $blue);

$center_x = 75;
$center_y = 75;
$width = 50;
$height = 50;
imagefilledellipse($image, $center_x, $center_y, $width, $height, $color);

$dither = false;
$num_colors = 2;
imagetruecolortopalette($image, $dither, $num_colors);

ob_start();

    imagepng($image);

$output = ob_get_clean();

echo '<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,' . base64_encode($output) . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAQMAAABKLAcXAAAABlBMVEU0Aj8E/gRiX+cVAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAoUlEQVQ4je3SsQ3DMAwEQAYuXHoEjaLR6NE0ikZQmSIIbTKA+R+4SZB0ZneAwJdIiVz175qeqGodZHaHg2ZwdDazVNnVoIlBG901oCU0vbkeEAARLI/LwCW0nqqE2qlqqH8sDY3vdLxBseVLKyqnqziWuMsgdVIjHS3j7YKC9S3vyjifNa+dlVfx/YHE6PMoiT9WIc3wBI9ACauSJtJVv6oN7YnLgdJcmb8AAAAASUVORK5CYII=">
</body>
</html>
HomeMenu