imagepalettetotruecolor

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

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

説明

imagepalettetotruecolor(GdImage$image): bool

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

パラメータ

image

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

戻り値

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

変更履歴

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

例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));
imagedestroy($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();

// メモリを解放します
imagedestroy($im);
?>

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

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

参考

To Top