Sunday, June 14, 2026
HomeLanguagesPHP | ImagickPixel setColor() function

PHP | ImagickPixel setColor() function

The ImagickPixel::setColor() function is an inbuilt function in PHP which is used to set the color of the ImagickPixel object.

Syntax:

bool ImagickPixel::setColor( string $color )

Parameters:This function accepts a single parameter $color which holds the color.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickPixel::setColor() function in PHP:
Program 1:




<?php
// Create a new imagickPixel object
$imagickPixel = new ImagickPixel();
  
// Set the color
$imagickPixel->setColor('#428554');
  
// Get the color
$color = $imagickPixel->getColor();
print("<pre>".print_r($color, true)."</pre>");
?>


Output:

Array
(
    [r] => 66
    [g] => 133
    [b] => 84
    [a] => 1
)

Program 2:




<?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) {
    // Loop through the pixels in the row
    if ($row % 5) {
        foreach ($pixels as $column => $pixel) {
            if ($column % 5) {
                // Set the color
                $pixel->setColor("green");
            }
        }
    }
  
    // Sync the iterator after each iteration
    $imageIterator->syncIterator();
}
  
header("Content-Type: image/jpg");
echo $imagick;
?>


Output:

Reference: https://www.php.net/manual/en/imagickpixel.setcolor.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
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS