PHP | imagickPixelIterator newPixelIterator()函数
ImagickPixelIterator::newPixelIterator()函数是PHP中的一个内置函数,用于获取新的像素迭代器来迭代 imagick 图像的像素。
句法:
bool ImagickPixelIterator::newPixelIterator( Imagick $wand )
参数:此函数接受单个参数$wand ,它保存图像以迭代像素。
返回值:此函数在成功时返回 TRUE。
下面的程序说明了PHP中的ImagickPixelIterator::newPixelIterator()函数:
方案一:
newImage(800, 250, 'black');
// Create a new ImagickPixelIterator instance
$imageIterator = new ImagickPixelIterator();
// Get the pixels from the image
$imageIterator->newPixelIterator($imagick);
$i = 0;
// Loop through pixel rows
foreach ($imageIterator as $row => $pixels) {
$i++;
}
echo 'Total rows are ' . $i;
?>
输出:
Total rows are 250
方案二:
newImage(800, 250, 'black');
// Create a new ImagickPixelIterator instance
$imageIterator = new ImagickPixelIterator();
// Get the pixels from the image
$imageIterator->newPixelIterator($imagick);
$colors = ['red', 'green', 'blue', 'yellow'];
$i = 0;
// Loop through pixel rows
foreach ($imageIterator as $row => $pixels) {
foreach ($pixels as $column => $pixel) {
// Set the color of each pixel lines to colors
$pixel->setColor($colors[$i % 4]);
}
$i++;
// 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.newpixeliterator。 PHP