HomeLanguagesPHP | ImagickPixel setColorValueQuantum() function

PHP | ImagickPixel setColorValueQuantum() function

The ImagickPixel::setColorValueQuantum() function is an inbuilt function in PHP which is used to set the value of the provided color channel for a given ImagickPixel’s color. The value can be in a range from 0 to 65535.

Syntax:

bool ImagickPixel::setColorValueQuantum( int $color, float $value )

Parameters:This function accepts two parameters as mentioned above and described below:

  • $color: It specifies the COLOR constants.
    List of all COLOR constants are given below:

    • imagick::COLOR_BLACK (11)
    • imagick::COLOR_BLUE (12)
    • imagick::COLOR_CYAN (13)
    • imagick::COLOR_GREEN (14)
    • imagick::COLOR_RED (15)
    • imagick::COLOR_YELLOW (16)
    • imagick::COLOR_MAGENTA (17)
    • imagick::COLOR_OPACITY (18)
    • imagick::COLOR_ALPHA (19)
    • imagick::COLOR_FUZZ (20)
  • $value: It specifies the value to be set.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickPixel::setColorValueQuantum() function in PHP:

Program 1:




<?php
// Create a new imagickPixel object
$imagickPixel = new ImagickPixel();
  
// Set the color value
$imagickPixel->setColorValueQuantum(imagick::COLOR_BLUE, 4500);
  
// Get the Color value with imagick::COLOR_BLUE
$colorValue = $imagickPixel->getColorValueQuantum(imagick::COLOR_BLUE);
echo $colorValue;
?>


Output:

4500

Program 2:




<?php
// Create a new imagick object
$imagick = new Imagick(
  
// Get the pixel iterator to iterate through each pixel
$imageIterator = $imagick->getPixelIterator();
$x = 200;
  
// Loop through pixel rows
foreach ($imageIterator as $row => $pixels) {
    // Loop through the pixels in the row
    if ($row % 2) {
        foreach ($pixels as $column => $pixel) {
            if ($column % 1000) {
                // Set the color
                $pixel->setColor("red");
  
                // Set the color value of Imagick::COLOR_RED
                $pixel->setColorValueQuantum(Imagick::COLOR_RED, $x);
                $x = $x + 1000;
            }
        }
    }
  
    // Sync the iterator after each iteration
    $imageIterator->syncIterator();
}
  
header("Content-Type: image/jpg");
echo $imagick;
?>


Output:

Reference: https://www.php.net/manual/en/imagickpixel.setcolorvaluequantum.php

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32707 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7081 POSTS0 COMMENTS
Nicole Veronica
12218 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12306 POSTS0 COMMENTS
Shaida Kate Naidoo
7200 POSTS0 COMMENTS
Ted Musemwa
7460 POSTS0 COMMENTS
Thapelo Manthata
7180 POSTS0 COMMENTS
Umr Jansen
7166 POSTS0 COMMENTS