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


PHP ImagickPixelIterator __construct()用法及代碼示例

ImagickPixelIterator::__construct()函數是PHP中的內置函數,用於創建ImagickPixelIterator對象的實例。該對象用於遍曆像素。

用法:

bool ImagickPixelIterator::__construct( Imagick $wand )

參數:該函數接受保存圖像的單個參數$wand。


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

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

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

<?php 
// Create a new imagick object 
$imagick = new Imagick(); 
   
// Create a image on imagick object 
$imagick->newImage(800, 250, 'black'); 
  
// Create a new imagickPixelIterator object 
$imageIterator = new ImagickPixelIterator($imagick); 
   
// Loop through pixel rows 
foreach ($imageIterator as $row => $pixels) { 
   
    foreach ($pixels as $column => $pixel) { 
        // Set the color of each pixel to blue 
        $pixel->setColor('blue'); 
    } 
   
    // Sync the iterator after each iteration 
    $imageIterator->syncIterator(); 
} 
  
// Show the output 
$imagick->setImageFormat('png'); 
header("Content-Type:image/png"); 
echo $imagick->getImageBlob(); 
?>

輸出:

程序2:

<?php 
// Create a new imagick object 
$imagick = new Imagick( 
    'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png'); 
  
// Create a new imagickPixelIterator object 
$imageIterator = new ImagickPixelIterator($imagick); 
  
$i = 0; 
  
// Loop through pixel rows 
foreach ($imageIterator as $row => $pixels) { 
  
    foreach ($pixels as $column => $pixel) { 
        $i++; 
    } 
  
    // Sync the iterator after each iteration 
    $imageIterator->syncIterator(); 
} 
  
echo 'Total number of pixels:' . $i; 
?>

輸出:

Total number of pixels:122728

參考: https://www.php.net/manual/en/imagickpixeliterator.construct.php



相關用法


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