imagetruecolortopalette

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

imagetruecolortopalette β€” ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Ρ‹Π²Π°Π΅Ρ‚ truecolor-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² ΠΏΠ°Π»ΠΈΡ‚Ρ€ΠΎΠ²ΠΎΠ΅

ОписаниС

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

Ѐункция imagetruecolortopalette() ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Ρ‹Π²Π°Π΅Ρ‚ truecolor-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² ΠΏΠ°Π»ΠΈΡ‚Ρ€ΠΎΠ²ΠΎΠ΅. Код этой Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ взяли ΠΈΠ· ΠΊΠΎΠ΄Π° Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ НСзависимой Π“Ρ€ΡƒΠΏΠΏΡ‹ JPEG. Код ΠΌΠΎΠ΄ΠΈΡ„ΠΈΡ†ΠΈΡ€ΠΎΠ²Π°Π»ΠΈ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΡΠΎΡ…Ρ€Π°Π½ΡΠ»ΠΎΡΡŒ максимальноС количСство ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΈ ΠΎΠ± Π°Π»ΡŒΡ„Π°-ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π°Ρ… Ρ†Π²Π΅Ρ‚ΠΎΠ², ΠΈ ΠΏΠ΅Ρ€Π΅Π΄Π°Π²Π°Π»ΠΈΡΡŒ Ρ†Π²Π΅Ρ‚Π° Π² Π»ΡƒΡ‡ΡˆΠ΅ΠΌ Π²ΠΈΠ΄Π΅, насколько это Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. Но Π² ΠΈΡ‚ΠΎΠ³Π΅ ΠΊΠΎΠ΄ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ Π½Π΅ Ρ‚Π°ΠΊ Ρ…ΠΎΡ€ΠΎΡˆΠΎ, ΠΏΠ»Π°Π½ΠΈΡ€ΠΎΠ²Π°Π»ΠΈ. ΠŸΠΎΡΡ‚ΠΎΠΌΡƒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ Π²Ρ‹Π·Ρ‹Π²Π°ΡŽΡ‚ для Π²Ρ‹Π²ΠΎΠ΄Π° truecolor-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ, ΠΎΠ½Π° Π³Π°Ρ€Π°Π½Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ самоС высокоС качСство Π²Ρ‹Π²ΠΎΠ΄Π°. Но Π½Π΅ большС Ρ‚ΠΎΠ³ΠΎ.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

image
ΠžΠ±ΡŠΠ΅ΠΊΡ‚ GdImage, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Π΅Ρ€Π½ΡƒΠ»Π° функция imagecreatetruecolor() ΠΈΠ»ΠΈ другая функция Π³Π΅Π½Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ.
dither

Π‘ΠΎ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ΠΌ true ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ сглаТиваСтся. Π‘Π³Π»Π°ΠΆΠΈΠ²Π°Π½ΠΈΠ΅ ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅Ρ‚ ΡˆΡƒΠΌΠ½ΠΎΡΡ‚ΡŒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ, Π½ΠΎ Π² Ρ‚ΠΎ ΠΆΠ΅ врСмя Π΄Π°Ρ‘Ρ‚ Π»ΡƒΡ‡ΡˆΡƒΡŽ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‡Ρƒ Ρ†Π²Π΅Ρ‚ΠΎΠ².

num_colors

Π—Π°Π΄Π°Ρ‘Ρ‚ максимальноС количСство Ρ†Π²Π΅Ρ‚ΠΎΠ² Π² ΠΏΠ°Π»ΠΈΡ‚Ρ€Π΅.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ image Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ GdImage; Ρ€Π°Π½ΡŒΡˆΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π» ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Ρ‹ΠΉ gd-рСсурс (resource).

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ прСобразования truecolor-изобраТСния Π² ΠΏΠ°Π»ΠΈΡ‚Ρ€ΠΎΠ²ΠΎΠ΅

<?php

// Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ truecolor-изобраТСния
$im = imagecreatetruecolor(100, 100);

// ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ изобраТСния Π² ΠΏΠ°Π»ΠΈΡ‚Ρ€ΠΎΠ²ΠΎΠ΅ с 255 Ρ†Π²Π΅Ρ‚Π°ΠΌΠΈ Π±Π΅Π· сглаТивания
imagetruecolortopalette($im, false, 255);

// Π‘ΠΎΡ…Ρ€Π°Π½Π΅Π½ΠΈΠ΅ изобраТСния
imagepng($im, './paletteimage.png');

?>
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 6 notes

up
7
zmorris at zsculpt dot com ΒΆ
21 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 ΒΆ
17 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 ΒΆ
22 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 ΒΆ
22 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 ΒΆ
22 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
-5
will at fnatic dot com ΒΆ
20 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;
        }
    }
?>