PHP | Imagick getPixelRegionIterator() Function

The Imagick::getPixelRegionIterator() function is an inbuilt function in PHP which is used to get an ImagickPixelIterator for an image section.

Syntax:

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

Parameters:This function accepts four parameters as mentioned above and described below:

  • $x: It specifies the x-coordinate of the region.
  • $y: It specifies the y-coordinate of the region.
  • $columns: It specifies the width of the region.
  • $rows: It specifies the height of the region.

Return Value: This function returns an ImagickPixelIterator for an image section.

Errors/Exceptions: This function throws ImagickException on error.



Below programs illustrate the Imagick::getPixelRegionIterator() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
$imageIterator = $imagick->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;
?>

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
$imageIterator = $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;
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/imagick.getpixelregioniterator.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.