Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP | ImagickPixel setIndex() Function

  • Last Updated : 14 Jan, 2020

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.




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

Output:

15

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




<?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();
}
?>

Output:

0
1
2
3
.
.
.

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!