📌  相关文章
📜  PHP | imagickpixeliterator syncIterator()函数

📅  最后修改于: 2022-05-13 01:56:33.225000             🧑  作者: Mango

PHP | imagickpixeliterator syncIterator()函数

ImagickPixelIterator::syncIterator()函数是PHP中的一个内置函数,用于同步像素迭代器。

句法:

bool ImagickPixelIterator::syncIterator( void )

参数:此函数不接受任何参数。

返回值:此函数在成功时返回 TRUE。

异常:此函数在出错时抛出 ImagickException。

下面给出的程序说明了PHP中的ImagickPixelIterator::syncIterator()函数

方案一:

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();
?>

输出:

方案二:

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