PHP | ImagickPixelIterator __construct() function
Last Updated :
02 Jan, 2020
The ImagickPixelIterator::__construct() function is an inbuilt function in PHP which is used create a instance of ImagickPixelIterator object. This object is used to iterate through the pixels.
Syntax:
bool ImagickPixelIterator::__construct( Imagick $wand )
Parameters:This function accepts a single parameter $wand which holds the image.
Return Value: This function returns TRUE on success.
Exceptions: This function throws ImagickException on error.
Below given programs illustrate the ImagickPixelIterator::__construct() function in PHP:
Program 1:
<?php
$imagick = new Imagick();
$imagick ->newImage(800, 250, 'black' );
$imageIterator = new ImagickPixelIterator( $imagick );
foreach ( $imageIterator as $row => $pixels ) {
foreach ( $pixels as $column => $pixel ) {
$pixel ->setColor( 'blue' );
}
$imageIterator ->syncIterator();
}
$imagick ->setImageFormat( 'png' );
header( "Content-Type: image/png" );
echo $imagick ->getImageBlob();
?>
|
Output:
Program 2:
<?php
$imagick = new Imagick(
$imageIterator = new ImagickPixelIterator( $imagick );
$i = 0;
foreach ( $imageIterator as $row => $pixels ) {
foreach ( $pixels as $column => $pixel ) {
$i ++;
}
$imageIterator ->syncIterator();
}
echo 'Total number of pixels: ' . $i ;
?>
|
Output:
Total number of pixels: 122728
Reference: https://www.php.net/manual/en/imagickpixeliterator.construct.php
Share your thoughts in the comments
Please Login to comment...