CakeFest 2024: The Official CakePHP Conference

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.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));
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

参考

add a note

User Contributed Notes 3 notes

up
1
Polda18
9 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
7 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
10 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;
}
}
?>
To Top