PHP 8.3.4 Released!

imagetruecolortopalette

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

imagetruecolortopaletteConvertit une image en couleurs vraies en image à palette

Description

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

imagetruecolortopalette() convertit l'image en vraies couleurs image en image à palette. Le code de cette fonction est directement tiré de la bibliothèque du Independent JPEG Group, qui est tout simplement génial. Le code a été modifié pour préserver l'essentiel du canal alpha dans la nouvelle palette, en plus de conserver les couleurs du mieux possible. Mais cela ne fonctionne pas toujours comme voulu. Il est alors préférable de générer un résultat en vraies couleurs, ce qui donne toujours le meilleur rendu.

Liste de paramètres

image

Un objet GdImage, retournée par une des fonctions de création d'images, comme imagecreatetruecolor().

dither

Indique si l'image doit être granuleuse - si défini à true, alors l'image sera un peu plus granuleuse mais l'approximation des couleurs sera meilleure.

num_colors

Le nombre maximal de couleurs dans la palette finale.

Valeurs de retour

Cette fonction retourne true en cas de succès ou false si une erreur survient.

Historique

Version Description
8.0.0 image attend une instance de GdImage désormais; auparavant, une resource gd était attendue.

Exemples

Exemple #1 Convertion d'une image truecolor en une palette

<?php
// Création d'une image truecolor
$im = imagecreatetruecolor(100, 100);

// Conversion en palette de 255 couleurs
imagetruecolortopalette($im, false, 255);

// Sauvegarde de l'image
imagepng($im, './paletteimage.png');
imagedestroy($im);
?>

add a note

User Contributed Notes 6 notes

up
7
zmorris at zsculpt dot com
19 years ago
Sometimes this function gives ugly/dull colors (especially when ncolors < 256). Here is a replacement that uses a temporary image and ImageColorMatch() to match the colors more accurately. It might be a hair slower, but the file size ends up the same:

<?php
function ImageTrueColorToPalette2( $image, $dither, $ncolors )
{
$width = imagesx( $image );
$height = imagesy( $image );
$colors_handle = ImageCreateTrueColor( $width, $height );
ImageCopyMerge( $colors_handle, $image, 0, 0, 0, 0, $width, $height, 100 );
ImageTrueColorToPalette( $image, $dither, $ncolors );
ImageColorMatch( $colors_handle, $image );
ImageDestroy( $colors_handle );
}
?>
up
2
djcassis(a)gmail.com
15 years ago
>> zmorris at zsculpt dot com

I don't have the imageColorMatch() function on my server, but I could slighty improve the quality of the GIF/PNG image by converting it first to 256 colors, then to true colors and finally to the desired number of colors.

<?php

$dither
= true;
$colors = 64;

$tmp = imageCreateFromJpeg('example.jpg');
$width = imagesX($tmp);
$height = imagesY($tmp);
imageTrueColorToPalette($tmp, $dither, 256);
$image = imageCreateTrueColor($width, $height);
imageCopy($image, $tmp, 0, 0, 0, 0, $width, $height);
imageDestroy($tmp);
imageTrueColorToPalette($image, $dither, $colors);

?>

Final $image will still have less than 64 colors, but more than if it was directly converted to 64 colors, and they match the JPEG image more.

Dunno why true colors to palette conversions are such a problem...
up
1
php at roelvanmastbergen dot nl
19 years ago
The palette created by this function often looks quite awful (at least it did on all of my test images). A better way to convert your true-colour images is by first making a resized copy of them with imagecopyresampled() to a 16x16 pixel destination. The resized image then contains only 256 pixels, which is exactly the number of colours you need. These colours usually look a lot better than the ones generated by imagetruecolortopalette().

The only disadvantage to this method I have found is that different-coloured details in the original image are lost in the conversion.
up
0
jemore at nospaM dot m6net dot fr
20 years ago
If you open a truecolor image (with imageCreateFromPng for example), and you save it directly to GIF format with imagegif, you can have a 500 internal server error. You must use imageTrueColorToPalette to reduce to 256 colors before saving the image in GIF format.
up
0
darkelder at php dot net
20 years ago
TrueColor images should be converted to Palette images with this function. So, if you want to use imagecolorstotal() function [ http://php.net/manual/en/function.imagecolorstotal.php ] , you should first convert the image to a palette image with imagetruecolortopalette();
up
-4
will at fnatic dot com
18 years ago
a basic palette to true color function
<?php
function imagepalettetotruecolor(&$img)
{
if (!
imageistruecolor($img))
{
$w = imagesx($img);
$h = imagesy($img);
$img1 = imagecreatetruecolor($w,$h);
imagecopy($img1,$img,0,0,0,0,$w,$h);
$img = $img1;
}
}
?>
To Top