PHP | ImagickPixelIterator newPixelRegionIterator() Function
Last Updated :
14 Jan, 2020
The ImagickPixelIterator::newPixelRegionIterator() function is an inbuilt function in PHP which is used to get a new pixel iterator from a specific region from the imagick wand.
Syntax:
bool ImagickPixelIterator::newPixelRegionIterator( Imagick $wand,
int $x, int $y, int $columns, int $rows )
Parameters: This function accept five parameters as mentioned above and described below:
- $wand: It specifies the imagick wand.
- $x: It specifies the x-coordinate.
- $y: It specifies the y-coordinate.
- $columns: It specifies the number of columns.
- $rows: It specifies the numbers of rows.
Return Value: This function returns TRUE on success.
Exceptions: This function throws ImagickException on error.
Below programs illustrate the ImagickPixelIterator::newPixelRegionIterator() function in PHP:
Program 1: This program drawing a square on a blank image.
<?php
$imagick = new Imagick();
$imagick ->newImage(800, 250, 'black' );
$imageIterator = new ImagickPixelIterator();
$imageIterator ->newPixelRegionIterator( $imagick , 40, 30, 200, 200);
foreach ( $imageIterator as $row => $pixels ) {
foreach ( $pixels as $column => $pixel ) {
$pixel ->setColor( 'red' );
}
$imageIterator ->syncIterator();
}
$imagick ->setImageFormat( 'png' );
header( "Content-Type: image/png" );
echo $imagick ->getImageBlob();
?>
|
Output:
Program 2: This program drawing a rectangle on a png image.
<?php
$imagick = new Imagick(
$imageIterator = new ImagickPixelIterator();
$imageIterator ->newPixelRegionIterator( $imagick , 40, 100, 1200, 20);
foreach ( $imageIterator as $row => $pixels ) {
foreach ( $pixels as $column => $pixel ) {
$pixel ->setColor( '#62AC45' );
}
$imageIterator ->syncIterator();
}
$imagick ->setImageFormat( 'png' );
header( "Content-Type: image/png" );
echo $imagick ->getImageBlob();
?>
|
Output:
Reference: https://www.php.net/manual/en/imagickpixeliterator.newpixelregioniterator.php
Share your thoughts in the comments
Please Login to comment...