Tuesday, November 19, 2024
Google search engine
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

Recent Comments