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


PHP ImagickPixel setColor()用法及代碼示例

ImagickPixel::setColor()函數是PHP中的內置函數,用於設置ImagickPixel對象的顏色。

用法:

bool ImagickPixel::setColor( string $color )

參數:該函數接受單個參數$color來保存顏色。


返回值:成功時此函數返回TRUE。

異常:該函數在錯誤時引發ImagickException。

下麵給出的程序說明了PHP中的ImagickPixel::setColor()函數:程序1:

<?php 
// Create a new imagickPixel object 
$imagickPixel = new ImagickPixel(); 
  
// Set the color 
$imagickPixel->setColor('#428554'); 
  
// Get the color 
$color = $imagickPixel->getColor(); 
print("<pre>".print_r($color, true)."</pre>"); 
?>

輸出:

Array
(
    [r] => 66
    [g] => 133
    [b] => 84
    [a] => 1
)

程序2:

<?php 
// Create a new imagick object 
$imagick = new Imagick( 
    'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png'); 
  
// Get the pixel iterator to iterate through each pixel 
$imageIterator = $imagick->getPixelIterator(); 
  
// Loop through pixel rows 
foreach ($imageIterator as $row => $pixels) { 
    // Loop through the pixels in the row 
    if ($row % 5) { 
        foreach ($pixels as $column => $pixel) { 
            if ($column % 5) { 
                // Set the color 
                $pixel->setColor("green"); 
            } 
        } 
    } 
  
    // Sync the iterator after each iteration 
    $imageIterator->syncIterator(); 
} 
  
header("Content-Type:image/jpg"); 
echo $imagick; 
?>

輸出:

參考: https://www.php.net/manual/en/imagickpixel.setcolor.php



相關用法


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