Skip to content
Related Articles

Related Articles

PHP | ImagickPixel getIndex() function
  • Last Updated : 23 Jan, 2020

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

Syntax:

int ImagickPixel::getIndex( void )

Parameters:This function doesn’t accept any parameter.

Return Value: This function returns an integer value containing the index.

Exceptions: This function throws ImagickException on error.



Below given programs illustrate the ImagickPixel::getIndex() function in PHP:
Program 1 (Get the index of a single pixel):

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

0 // which is the default index for a pixel.

Program 2 (Get the index for all the pixels of a image):

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

5

Program 3:

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();
  
// Loop through pixel rows
foreach ($imageIterator as $row => $pixels) {
  
    foreach ($pixels as $column => $pixel) {
        // Get the index of each pixel of image
        echo $pixel->getindex() . '<br>';
  
    }
  
    // Sync the iterator after each iteration
    $imageIterator->syncIterator();
}
?>

chevron_right


Output:

0
0
0
0
.
.
.

Reference: https://www.php.net/manual/en/imagickpixel.getindex.php

My Personal Notes arrow_drop_up
Recommended Articles
Page :