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, zambiatek 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!




