Open In App

PHP | ImagickPixel setHSL() function

Last Updated : 02 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
The ImagickPixel::setHSL() function is an inbuilt function in PHP which is used to set the color described by the ImagickPixel object using normalized values for hue, saturation and luminosity. Syntax:
bool ImagickPixel::setHSL( float $hue, float $saturation, float $luminosity )
Parameters:This function accepts three parameters as mentioned above and described below:
  • $hue: It specifies the normalized value for hue.
  • $saturation: It specifies the normalized value for saturation.
  • $luminosity: It specifies the normalized value for luminosity.
Return Value: This function returns TRUE on success. Exceptions: This function throws ImagickException on error. Below given programs illustrate the ImagickPixel::setHSL() function in PHP: Program 1: php
<?php
// Create a new imagickPixel object
$imagickPixel = new ImagickPixel();

// Set the HSL for pixel
$imagickPixel->setHSL(0.4, 0.4, 0.4);

// Get the HSL of pixel
$HSL = $imagickPixel->getHSL();
print("<pre>".print_r($HSL, true)."</pre>");
?>
Output:
Array
(
    [hue] => 0.40000158942082
    [saturation] => 0.4000152590219
    [luminosity] => 0.4
)
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
    foreach ($pixels as $column => $pixel) {

        // Get the current HSL
        $HSL = $pixel->getHSL();

        // Set the HSL and change only hue
        $pixel->setHSL(0.6, $HSL['saturation'], $HSL['luminosity']);

    }

    // Sync the iterator after each iteration
    $imageIterator->syncIterator();
}

header("Content-Type: image/jpg");
echo $imagick;
?>
Output: Reference: https://www.php.net/manual/en/imagickpixel.sethsl.php

Next Article

Similar Reads