Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP | ImagickPixel setColor() function

  • Last Updated : 02 Jan, 2020

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!