This function appears to work by changing the values of the colors of the paletted image -- no good if you're trying to force the resultant image to stick with certain pre-defined color values.
imagecolormatch
(PHP 4 >= 4.3.0, PHP 5)
imagecolormatch — Fait correspondre un peu plus les couleurs de la version palette d'une image aux couleurs de sa version truecolor
Description
Fait correspondre un peu plus les couleurs de la version palette d'une image aux couleurs de sa version truecolor.
Liste de paramètres
- image1
-
Une ressource d'image truecolor.
- image2
-
Une ressource de palette d'image pointant sur une image qui a la même taille que image1 .
Valeurs de retour
Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.
Exemples
Exemple #1 Exemple avec imagecolormatch()
<?php
// Définit l'images true color et la palette
$im1 = imagecreatefrompng('./gdlogo.png');
$im2 = imagecreate(imagesx($im1), imagesy($im1));
// Ajout de quelques couleurs à $im2
$colors = Array();
$colors[] = imagecolorallocate($im2, 255, 36, 74);
$colors[] = imagecolorallocate($im2, 40, 0, 240);
$colors[] = imagecolorallocate($im2, 82, 100, 255);
$colors[] = imagecolorallocate($im2, 84, 63, 44);
// Fait correspondre ces couleurs avec l'image true color
imagecolormatch($im1, $im2);
// Libération de la mémoire
imagedestroy($im);
?>
Notes
Note: Cette fonction n'est disponible que si PHP est compilé avec la version embarquée de la bibliothèque GD.
Note: Cette fonction requiert la bibliothèque GD 2.0.1 ou supérieure (2.0.28 ou supérieure est recommandée).
imagecolormatch
12-Dec-2005 07:22
09-Feb-2004 05:14
This function is a godsend! It works exactly as documented.
I'm working on an application where I need to take a transparent GIF, matte the GIF on a user defined background color, and finally scale the GIF based on a user defined %.
The only way I could get this to work so that the final image was high quality, ie: no jagged edges, and a smooth scale, was to convert the GIF to a JPG, and then copy the JPG into a new GIF image like this:
// open transparent gif
$GIFimg = imagecreatefromgif($file_path);
// create jpg image
$JPGimg = imagecreatetruecolor($width, $height);
// copy GIF to JPG
imagecopy($JPGimg, $GIFimg, 0, 0, 0, 0, $width, $height);
// create a true color image
$JPGscaled = imagecreatetruecolor($n_width, $n_height);
// scale the new JPG using the truecolor image
imagecopyresampled($JPGscaled, $JPGimg, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// create final GIF image
$GIFfinal = imagecreate($n_width, $n_height);
// copy the scaled JPG back to a GIF
imagecopymerge($GIFfinal, $JPGscaled, 0, 0, 0, 0, $n_width, $n_height, 100);
This worked great except the final step, copying the JPG to a GIF. If the JPG had too many colors, the function would index the colors to make it a palette image. So what would end up happening is the final image contained INCORRECT colors.
Adding this one line at the bottom of the code fixed everything.
imagecolormatch($JPGscaled, $GIFfinal);
I hope this helps anyone who is converting images back and forth and dealing with palette issues and color correction. Also, be aware that the above code is a sample and will not work by copying and pasting.
