PHP | ImagickPixel setIndex() Function

The ImagickPixel::setIndex() function is an inbuilt function in PHP which is used to set the colormap index of the pixel.

Syntax:

bool ImagickPixel::setIndex( int $index )

Parameters: This function accepts a single parameter $index which holds the index to be set.

Return Value: This function returns TRUE on success.

Below programs illustrate the ImagickPixel::setIndex() function in PHP:



Program 1: This program set and return the index for a single pixel.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new imagickPixel object
$imagickPixel = new ImagickPixel();
  
// Set the index
$imagickPixel->setIndex(15);
  
// Get the index
$index = $imagickPixel->getIndex();
echo $index;
?>

chevron_right


Output:

15

Program 2: This program returns the index value of an image.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Get the pixel iterator to iterate through each pixel
$imageIterator = $imagick->getPixelIterator();
  
$i = 0;
  
// Loop through pixel rows
foreach ($imageIterator as $row => $pixels) {
  
    foreach ($pixels as $column => $pixel) {
  
        // Set the index
        $pixel->setIndex($i);
        echo $pixel->getIndex() . '<br>';
        $i++;
    }
  
    // Sync the iterator after each iteration
    $imageIterator->syncIterator();
}
?>

chevron_right


Output:

0
1
2
3
.
.
.

Reference: https://www.php.net/manual/en/imagickpixel.setindex.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.