« imagepalettecopy
imagepng »

imagepalettetotruecolor

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

imagepalettetotruecolor — パレット形式の画像を true color に変換する

説明

function imagepalettetotruecolor(GdImage $image): bool

imagecreate() のような関数で作ったパレット形式の画像を、 imagecreatetruecolor() で作るような true color 形式に変換します。

パラメータ

image

imagecreatetruecolor()のような画像作成関数が返す GdImage オブジェクト。

戻り値

変換に成功した場合や、元画像が既に true color だった場合に true を返します。 それ以外の場合に false を返します。

変更履歴

バージョン 説明
8.0.0 image は、 GdImage クラスのインスタンスを期待するようになりました。 これより前のバージョンでは、有効な gd resource が期待されていました。

例

例1 画像オブジェクトの true color への変換

<?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();

// true color に変換します
imagepalettetotruecolor($im);
$typeof();
?>

上の例の出力は以下となります。

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

参考

関連キーワード:
画像   color   imagepalettetotruecolor   typeof   パレット   imagecreatetruecolor   GdImage   image   imageistruecolor   imagecreate   期待   return   imagesx   imagesy   palette   imagepalettecopy   imagepng   Image   成功   履歴   インスタンス   resource   過去   互換性   exists
関連する関数・ページ:
function.imageistruecolor   function.imagecreate   function.imagepalettecopy   function.imagesx   function.imagesy   function.imagexbm