HomeLanguagesPHP | ImagickPixelIterator setIteratorRow() function

PHP | ImagickPixelIterator setIteratorRow() function

The ImagickPixelIterator::setIteratorRow() function is an inbuilt function in PHP which is used to set the pixel iterator row. This function is used to move to any row in the current image pixels.

Syntax:

bool ImagickPixelIterator::setIteratorRow( int $row )

Parameters:This function accepts a single parameter $row which holds the row number.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickPixelIterator::setIteratorRow() function in PHP:

Program 1:




<?php
// Create a new imagick object
$imagick = new Imagick(
  
  
// Get the pixel iterator
$pixelIterator = $imagick->getPixelIterator();
  
// Set the pixel iterator to 50
$pixelIterator->setIteratorRow(50);
  
// Get the current iterator row
echo "Current row is " . $pixelIterator->getIteratorRow();
?>


Output:

Current row is 50

Program 2:




<?php
// Create a new imagick object
$imagick = new Imagick();
  
// Create a image on imagick object
$imagick->newImage(800, 250, 'black');
  
$imageIterator = $imagick->getPixelIterator();
  
$x = 0;
  
while ($x < 250) {
    // Set the iterator row
    $imageIterator->setIteratorRow($x);
  
    // Get the current row
    $pixels = $imageIterator->getCurrentIteratorRow();
      
    foreach ($pixels as $pixel) {
        if ($x % 2) {
            // Set the color of pixel
            $pixel->setColor('white');
  
        } else {
            // Set the color of pixel
            $pixel->setColor('red');
  
        }
    }
    // Sync the iterator
    $imageIterator->syncIterator();
  
    // Give a gap of 4 pixels between lines
    $x = $x + 5;
}
  
// Show the output
$imagick->setImageFormat('png');
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Reference: https://www.php.net/manual/en/imagickpixeliterator.setiteratorrow.php

RELATED ARTICLES

Most Popular

Dominic
32715 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7092 POSTS0 COMMENTS
Nicole Veronica
12231 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12313 POSTS0 COMMENTS
Shaida Kate Naidoo
7209 POSTS0 COMMENTS
Ted Musemwa
7473 POSTS0 COMMENTS
Thapelo Manthata
7192 POSTS0 COMMENTS
Umr Jansen
7173 POSTS0 COMMENTS