PHP Conference Japan 2024

imagepalettetotruecolor

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

imagepalettetotruecolor將基於調色盤的影像轉換為真彩色

說明

imagepalettetotruecolor(GdImage $image): bool

將由 imagecreate() 等函式建立的基於調色盤的影像轉換為真彩色影像,例如 imagecreatetruecolor() 建立的影像。

參數

image

一個 GdImage 物件,由影像建立函式之一返回,例如 imagecreatetruecolor()

傳回值

如果轉換完成,或者來源影像已是真彩色影像,則傳回 true,否則傳回 false

更新日誌

版本 說明
8.0.0 image 參數現在需要一個 GdImage 實例;先前需要的是一個有效的 gd 資源

範例

範例 #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) );
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();

// 將其轉換為真彩色
imagepalettetotruecolor($im);
$typeof();

// 釋放記憶體
imagedestroy($im);
?>

以上範例會輸出:

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

參見

新增註解

使用者貢獻的筆記 3 則筆記

Polda18
10 年前
PHP 5.4.24 版不支援這個函數(尚未定義)。要解決這個問題,您必須使用 imagecreatetruecolor() 函數建立新的影像,並將影像資源複製到新的影像。

從 GIF 檔案載入影像的範例

$image = imagecreatefromgif("path/to/gif/file.gif"); //從 GIF 建立影像
$width = imagesx($image); //取得來源影像的寬度
$height = imagesy($image); //取得來源影像的高度
$image2 = imagecreatetruecolor($width,$height); //建立具有指定寬度和高度的真彩色新影像
imagecopy($image2,$image,0,0,0,0,$width,$height); //將來源影像複製到新的影像

header("Content-Type: image/jpeg"); //設定 JPG 影像的標頭
imagejpg($image2); //將 JPG 影像渲染到瀏覽器

imagedestroy($image); //釋放記憶體
imagedestroy($image2);
walf - iftfy
7 年前
這是 walf 解決方案的有效版本

<?php
// 向下相容性
if (!function_exists('imagepalettetotruecolor')) {
function
imagepalettetotruecolor(&$src) {
if (
imageistruecolor($src)) {
return
true;
}

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

imagealphablending($dst, false);//避免與預設的黑色背景混合
$transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);//如有需要,可更改 RGB 值,但 Alpha 值請維持在 127
imagefilledrectangle($dst, 0, 0, imagesx($src), imagesy($src), $transparent);//比用 flood fill 簡單
imagealphablending($dst, true);//恢復預設的混合模式

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

$src = $dst;
return
true;
}
}
?>
-2
walf
10 年前
這個向下相容的範例不會保留透明度。您必須先清除新影像上預設的黑色背景。

<?php
// 向下相容
if (!function_exists('imagepalettetotruecolor')) {
function
imagepalettetotruecolor(&$src) {
if (
imageistruecolor($src)) {
return
true;
}

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

imagealphablending($dst, false);//避免與預設的黑色混合
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);//如果需要,可以更改 RGB 值,但 alpha 值保持在 127
imagefilledrectangle($dst, 0, 0, imagesx($src), imagesy($src), $transparent);//比用洪水填充法簡單
imagealphablending($dst, true);//恢復預設的混合模式

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

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