Skip to content
Related Articles
Open in App
Not now

Related Articles

PHP | ImagickPixel setColor() function

Improve Article
Save Article
  • Last Updated : 02 Jan, 2020
Improve Article
Save Article

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


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:

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


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

Program 2:

// 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
    // Sync the iterator after each iteration
header("Content-Type: image/jpg");
echo $imagick;



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!