imagepalettetotruecolor

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

imagepalettetotruecolor β€” ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Ρ‹Π²Π°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° основС ΠΏΠ°Π»ΠΈΡ‚Ρ€Ρ‹ Π² настоящий Ρ†Π²Π΅Ρ‚

ОписаниС

function imagepalettetotruecolor(GdImage $image): bool

Ѐункция ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Ρ‹Π²Π°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° основС ΠΏΠ°Π»ΠΈΡ‚Ρ€Ρ‹, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ создали Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ Π½Π°ΠΏΠΎΠ΄ΠΎΠ±ΠΈΠ΅ imagecreate(), Π² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ с истинным Ρ†Π²Π΅Ρ‚ΠΎΠΌ, ΠΊΠ°ΠΊ это Π΄Π΅Π»Π°Π΅Ρ‚ функция imagecreatetruecolor().

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

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

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

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ Π·Π°Π²Π΅Ρ€ΡˆΠΈΠ»ΠΎΡΡŒ, ΠΈΠ»ΠΈ Ссли исходноС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΡƒΠΆΠ΅ относится ΠΊ truecolor-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ, ΠΈΠ½Π°Ρ‡Π΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ false.

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

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

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

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ прСобразования ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° изобраТСния Π² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ с истинным Ρ†Π²Π΅Ρ‚ΠΎΠΌ

<?php

// Π’ цСлях ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠΉ совмСстимости
if (!function_exists('imagepalettetotruecolor')) {
function
imagepalettetotruecolor(&$src)
{
if (
imageistruecolor($src)) {
return (
true);
}

$dst = imagecreatetruecolor(imagesx($src), imagesy($src));

imagecopy($dst, $src, 0, 0, 0, 0, imagesx($src), imagesy($src));

$src = $dst;

return (
true);
}
}

// АнонимноС Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠ΅-ΠΏΠΎΠΌΠΎΡ‰Π½ΠΈΠΊ
$typeof = function () use ($im) {
echo
'typeof($im) = ' . (imageistruecolor($im) ? 'true color' : 'palette'), PHP_EOL;
};

// Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° основС ΠΏΠ°Π»ΠΈΡ‚Ρ€Ρ‹
$im = imagecreate(100, 100);
$typeof();

// ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅ΠΌ Ρ†Π²Π΅Ρ‚Π° изобраТСния ΠΊ истинным
imagepalettetotruecolor($im);
$typeof();

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

typeof($im) = palette
typeof($im) = true color

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imagecreatetruecolor() - Π‘ΠΎΠ·Π΄Π°Ρ‘Ρ‚ Π½ΠΎΠ²ΠΎΠ΅ truecolor-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅
  • imageistruecolor() - ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚, относится Π»ΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΊ изобраТСниям truecolor
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

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

up
1
Polda18 ΒΆ
12 years ago
PHP ver 5.4.24 does not support this function (it has not been defined). To get rid of this, you must copy image resource to new image created by function imagecreatetruecolor();

Example with image loaded from GIF file:

$image  = imagecreatefromgif("path/to/gif/file.gif");  //create an image from GIF
$width  = imagesx($image);                             //get width of source image
$height = imagesy($image);                             //get height of source image
$image2 = imagecreatetruecolor($width,$height);        //create new image of true colors with given width and height
imagecopy($image2,$image,0,0,0,0,$width,$height);      //copy source image to new one

header("Content-Type: image/jpeg");                    //set header for JPG image
imagejpg($image2);                                     //render JPg image into browser

imagedestroy($image);                                  //free up memory
imagedestroy($image2);
up
0
walf - iftfy ΒΆ
9 years ago
here the working version of walf's solution

<?php
// Backwards compatiblity
if (!function_exists('imagepalettetotruecolor')) {
    function imagepalettetotruecolor(&$src) {
        if (imageistruecolor($src)) {
            return true;
        }

        $dst = imagecreatetruecolor(imagesx($src), imagesy($src));
     
        imagealphablending($dst, false);//prevent blending with default black
        $transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);//change the RGB values if you need, but leave alpha at 127
        imagefilledrectangle($dst, 0, 0, imagesx($src), imagesy($src), $transparent);//simpler than flood fill
        imagealphablending($dst, true);//restore default blending

        imagecopy($dst, $src, 0, 0, 0, 0, imagesx($src), imagesy($src));
        imagedestroy($src);

        $src = $dst;
        return true;
    }
}
?>
up
-2
walf ΒΆ
12 years ago
The backwards compatibility example does not preserve transparency. You must first wipe out the default black background on the new image:

<?php
// Backwards compatiblity
if (!function_exists('imagepalettetotruecolor')) {
    function imagepalettetotruecolor(&$src) {
        if (imageistruecolor($src)) {
            return true;
        }

        $dst = imagecreatetruecolor(imagesx($src), imagesy($src));
        
        imagealphablending($dst, false);//prevent blending with default black
        $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);//change the RGB values if you need, but leave alpha at 127
        imagefilledrectangle($dst, 0, 0, $imagesx($src), imagesy($src), $transparent);//simpler than flood fill
        imagealphablending($dst, true);//restore default blending

        imagecopy($dst, $src, 0, 0, 0, 0, imagesx($src), imagesy($src));
        imagedestroy($src);

        $src = $dst;
        return true;
    }
}
?>