Saturday, February 7, 2026
HomeLanguagesPHP | ImagickPixel setIndex() Function

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.




<?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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32491 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6862 POSTS0 COMMENTS
Nicole Veronica
11987 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12075 POSTS0 COMMENTS
Shaida Kate Naidoo
6996 POSTS0 COMMENTS
Ted Musemwa
7237 POSTS0 COMMENTS
Thapelo Manthata
6947 POSTS0 COMMENTS
Umr Jansen
6933 POSTS0 COMMENTS