當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP imagetruecolortopalette()用法及代碼示例

imagetruecolortopalette()函數是PHP中的內置函數,用於將真實的彩色圖像轉換為調色板圖像。

用法:

bool imagetruecolortopalette( $image, $dither, $ncolors )

參數:此函數接受上述和以下所述的三個參數:


  • $image:它由圖像創建函數之一(例如imagecreatetruecolor())返回。它用於創建圖像的尺寸。
  • $dither:布爾值表示圖像抖動。如果為True,則將使用抖動處理,這將使圖像更斑點,但色彩逼近效果更好。
  • $ncolors:此參數包含調色板中應保留的最大顏色數集。

返回值:如果成功,此函數返回True;如果失敗,則返回False。

以下示例程序旨在說明PHP中的imagetruecolortopalette()函數:

示例1:

<?php 
  
// Create a 400*300 image 
$im = imagecreatetruecolor(400, 300); 
  
// Set the background color of image  
$background_color = imagecolorallocate($im,  0, 153, 0);  
       
// Fill background with above selected color  
imagefill($im, 0, 0, $background_color);  
  
// Convert to palette-based with no dithering and 255 colors 
imagetruecolortopalette($im, true, 255); 
  
// Output the picture to the browser  
header('Content-type: image/png');  
       
imagepng($im);  
?>

輸出:

示例2:

<?php 
  
// Setup the true color and palette images  
$im = imagecreatefrompng(  
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-9.png');  
  
// Convert to palette-based with no dithering and 255 colors 
imagetruecolortopalette($im, false, 255); 
  
// Output the picture to the browser  
header('Content-type: image/png');  
       
imagepng($im);  
?>

輸出:

參考: http://php.net/manual/en/function.imagetruecolortopalette.php



相關用法


注:本文由純淨天空篩選整理自Mahadev99大神的英文原創作品 PHP | imagetruecolortopalette() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。