📜  PHP |想象一下 getPixelRegionIterator()函数

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

PHP |想象一下 getPixelRegionIterator()函数

Imagick::getPixelRegionIterator()函数是PHP中的一个内置函数,用于获取图像部分的 ImagickPixelIterator。

句法:

ImagickPixelIterator Imagick::getPixelRegionIterator( int $x, int $y, int $columns, int $rows )

参数:该函数接受上面提到的四个参数,如下所述:

  • $x:指定区域的 x 坐标。
  • $y:指定区域的y坐标。
  • $columns:它指定区域的宽度。
  • $rows:指定区域的高度。

返回值:此函数返回图像部分的 ImagickPixelIterator。

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

下面的程序说明了PHP中的Imagick::getPixelRegionIterator()函数

方案一:

getPixelRegionIterator(0, 0, 330, 200);
  
// Change the color of every second pixel of region to blue
foreach ($imageIterator as $row => $pixels) {
    foreach ($pixels as $column => $pixel) {
        if ($column % 2) {
            $pixel->setColor("blue");
        }
    }
    $imageIterator->syncIterator();
}
  
// Display the output
header("Content-Type:image/png");
echo $imagick;
?>

输出:

方案二:

getPixelRegionIterator(330, 0, 338, 200);
  
// Change the color of every second pixel of region to green
foreach ($imageIterator as $row => $pixels) {
    foreach ($pixels as $column => $pixel) {
        if ($column % 2) {
            $pixel->setColor("green");
        }
    }
    $imageIterator->syncIterator();
}
  
// Display the output
header("Content-Type:image/png");
echo $imagick;
?>

输出:

参考: https://www. PHP.net/manual/en/imagick.getpixelregioniterator。 PHP