Open In App

PHP | ImagickPixel setColor() function

Last Updated : 02 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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
<?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
<?php
// Create a new imagick object
$imagick = new Imagick(
    'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

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

Next Article

Similar Reads