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

search for in the

imagettfbbox> <imagesy
[edit] Last updated: Wed, 22 May 2013

view this page in

imagetruecolortopalette

(PHP 4 >= 4.0.6, PHP 5)

imagetruecolortopaletteTrueColor イメージをパレットイメージに変換する

説明

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

imagetruecolortopalette() は、TrueColorイメー ジをパレットイメージに変換します。この関数のコードは、元々 Independent JPEG Groupライブラリ用に書かれたもので、素晴らしいも のです。このコードは、色をできる限り維持することに加えて、アルファ チャネルに関する情報を出力されるパレットにおいてできるだけ維持す るように修正されています。これは、期待通りにうまくいきません。通 常は、最高の出力品質が保障されるTrueColor出力イメージを単に出力す るのが最良の方法です。

パラメータ

image

imagecreatetruecolor() のような画像作成関数が返す画像リソース。

dither

イメージにディザーをかけることを指定します。 TRUE の場合はディザーが行われます。 出力はぼやけますが、色の近似はより良くなります。

ncolors

パレットに保持される最大の色数を設定します。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例1 true color 画像からパレット画像への変換

<?php
// 新しい true color 画像を作成します
$im imagecreatetruecolor(100100);

// ディザリングなしの 255 色パレットに変換します
imagetruecolortopalette($imfalse255);

// 画像を保存します
imagepng($im'./paletteimage.png');
imagedestroy($im);
?>

注意

注意: この関数は、GD 2.0.1 以降を必要とします (2.0.28 以降を推奨します)。



imagettfbbox> <imagesy
[edit] Last updated: Wed, 22 May 2013
 
add a note add a note User Contributed Notes imagetruecolortopalette - [7 notes]
up
0
djcassis(a)gmail.com
4 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
0
will at fnatic dot com
7 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;
        }
    }
?>
up
0
zmorris at zsculpt dot com
8 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
0
php at roelvanmastbergen dot nl
8 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
9 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
9 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
-1
burninleo at gmx dot net
6 years ago
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;
}
?>

 
show source | credits | sitemap | contact | advertising | mirror sites