©
本文档使用
php.cn手册 发布
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
imagetruecolortopalette — 将真彩色图像转换为调色板图像
$image
, bool $dither
, int $ncolors
)imagetruecolortopalette() 将一幅真彩色图像转换为调色板图像。本函数的代码原本是从独立的 JPEG 小组库代码中提取出来的,非常出色。此代码被修改以在结果调色板中保留尽可能多的 alpha 通道信息以及尽可能多的颜色。但并没有达到期望的效果。通常最好生成真彩色图像输出,这样可以保证得到最高的输出质量。
image
由图象创建函数(例如 imagecreatetruecolor() )返回的图象资源。
dither
指明图像是否被抖动(dithered),如果为
TRUE
则图像将被抖动使图像中的斑点更多但是颜色更接近。
ncolors
设定调色板中被保留的颜色的最大数目。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 Converting a true color image to a palette-based image
<?php
// Create a new true color image
$im = imagecreatetruecolor ( 100 , 100 );
// Convert to palette-based with no dithering and 255 colors
imagetruecolortopalette ( $im , false , 255 );
// Save the image
imagepng ( $im , './paletteimage.png' );
imagedestroy ( $im );
?>
Note: 此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。
[#1] djcassis(a)gmail.com [2008-09-21 06:52:02]
>> 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...
[#2] will at fnatic dot com [2006-02-24 12:49:19]
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;
}
}
?>
[#3] zmorris at zsculpt dot com [2004-08-16 22:58:26]
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 );
}
?>
[#4] php at roelvanmastbergen dot nl [2004-06-06 09:34:12]
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.
[#5] jemore at nospaM dot m6net dot fr [2003-11-22 10:25:38]
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.
[#6] darkelder at php dot net [2003-07-17 13:34:07]
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();