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


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


ImagickPixelIterator::syncIterator()函數是PHP中的一個內置函數,用於同步像素迭代器。

用法:

bool ImagickPixelIterator::syncIterator( void )

參數:此函數不接受任何參數。


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

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

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

程序1:

<?php 
// Create a new imagick object 
$imagick = new Imagick(); 
  
// Create a image on imagick object 
$imagick->newImage(800, 250, 'black'); 
  
$imageIterator = $imagick->getPixelIterator(); 
  
// Loop through rows pixel value 
foreach ($imageIterator as $pixels) { 
  
    // Loop through the pixels in the row value 
    foreach ($pixels as $column => $pixel) { 
  
        if ($column % 11) { 
            $pixel->setColor('yellow'); 
        } 
    } 
    // Sync the iterator after each iteration 
    $imageIterator->syncIterator(); 
} 
  
// Loop through pixel rows again for new color 
foreach ($imageIterator as $pixels) { 
    // Loop through the pixels in the row 
    foreach ($pixels as $column => $pixel) { 
        if ($column % 20) { 
            $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'); 
  
$imageIterator = $imagick->getPixelIterator(); 
  
// Loop through pixel rows 
foreach ($imageIterator as $pixels) { 
    // Loop through the pixels in the row 
    foreach ($pixels as $column => $pixel) { 
  
        // Get the current HSL 
        $HSL = $pixel->getHSL(); 
   
        // Set the HSL and change only saturation 
        $pixel->setHSL($HSL['hue'], 2, $HSL['luminosity']); 
    } 
    // Sync the iterator after each iteration 
    $imageIterator->syncIterator(); 
} 
  
// Show the output 
$imagick->setImageFormat('png'); 
header("Content-Type:image/png"); 
echo $imagick->getImageBlob(); 
?>

輸出:

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



相關用法


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