downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imagettfbbox> <imagesy
Last updated: Fri, 24 Jul 2009

view this page in

imagetruecolortopalette

(PHP 4 >= 4.0.6, PHP 5)

imagetruecolortopaletteConvert a true color image to a palette image

설명

bool imagetruecolortopalette ( resource $image , bool $dither , int $ncolors )

imagetruecolortopalette() converts a truecolor image to a palette image. The code for this function was originally drawn from the Independent JPEG Group library code, which is excellent. The code has been modified to preserve as much alpha channel information as possible in the resulting palette, in addition to preserving colors as well as possible. This does not work as well as might be hoped. It is usually best to simply produce a truecolor output image instead, which guarantees the highest output quality.

인수

image

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.

ncolors

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

반환값

성공할 경우 TRUE를, 실패할 경우 FALSE를 반환합니다.

예제

Example #1 Converting a true color image to a palette-based image

<?php
// Create a new true color image
$im = new imagecreatetruecolor(100100);

// Convert to palette-based with no dithering and 255 colors
imagetruecolortopalette($imfalse255);

// Save the image
imagepng($im'./paletteimage.png');
imagedestroy($im);
?>

주의

Note: 이 함수는 GD 2.0.1 이상이 필요합니다. (2.0.28 이상을 권장합니다)



imagettfbbox> <imagesy
Last updated: Fri, 24 Jul 2009
 
add a note add a note User Contributed Notes
imagetruecolortopalette
djcassis(a)gmail.com
21-Sep-2008 10:52
>> 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...
burninleo at gmx dot net
29-May-2006 01:51
If You know which palette to use (e.g. 255 colors greyscale) You may achieve better results using the following way:

1. Create new image
2. Apply palette
3. imagecopy() the content

This is especially helpful if you created a greyscale picture in trucolor-mode (to use antialiasing for example) but need to send it as palette (to use transparency in Internet Explorer).

The following example will *not* create great results from "real" truecolor images but works well on grey truecolor images:

<?PHP
function imageTruecolorToGrayscale(&$image) {
   
$copy = $image;
   
$dx = imagesx($image);
   
$dy = imagesy($image);
   
$image = imagecreate($dx, $dy);
   
   
// 254 Colors + 1 reserved for transparency
   
$transparency = imagecolorallocate($image, 0, 255, 0);
   
$max = 255; $dd = 254;
    for (
$i=0; $i<$dd; $i++) {
       
$val = round($max * $i / ($dd-1));
       
imagecolorallocate($image, $val, $val, $val);
    }
   
   
imagecopy($image, $copy, 0, 0, 0, 0, $dx, $dy);
   
imagedestroy($copy);
    return
$transparency;
}
?>
will at fnatic dot com
25-Feb-2006 05:49
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;
        }
    }
?>
zmorris at zsculpt dot com
17-Aug-2004 02:58
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 );
}
?>
php at roelvanmastbergen dot nl
07-Jun-2004 01:34
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.
jemore at nospaM dot m6net dot fr
23-Nov-2003 03:25
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.
darkelder at php dot net
18-Jul-2003 05:34
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();

imagettfbbox> <imagesy
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites