PHP | Gmagick setimagechanneldepth() Function

The Gmagick::setimagechanneldepth() function is an inbuilt function in PHP which is used to set the depth of a particular channel image.

Syntax:

public Gmagick::setimagechanneldepth( $channel , $depth ) : Gmagick

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



  • $channel: This parameter is used to specify the channel constant. Below is the list of constant channel:
    CHANNEL constants:

    • Gmagick::CHANNEL_UNDEFINED
    • Gmagick::CHANNEL_RED
    • Gmagick::CHANNEL_GRAY
    • Gmagick::CHANNEL_CYAN
    • Gmagick::CHANNEL_GREEN
    • Gmagick::CHANNEL_MAGENTA
    • Gmagick::CHANNEL_BLUE
    • Gmagick::CHANNEL_YELLOW
    • Gmagick::CHANNEL_ALPHA
    • Gmagick::CHANNEL_OPACITY
    • Gmagick::CHANNEL_MATTE
    • Gmagick::CHANNEL_BLACK
    • Gmagick::CHANNEL_INDEX
    • Gmagick::CHANNEL_ALL
    • Gmagick::CHANNEL_DEFAULT
  • $depth: This parameter is used to specify the depth to be set for Gmagick object.

Return Value: This function returns the Gmagick object on success.

Below programs illustrate the Gmagick::setimagechanneldepth() function in PHP:

Program 1:
Original Image:
https://cdncontribute.geeksforgeeks.org/wp-content/uploads/geeks-21.png

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create new Gmagick object
$im = new Gmagick(
   
// Using getImageChannelDepth function
// with red channel
echo "Before Set Channel depth: " . "</br>";
echo $im->getImageChannelDepth(Gmagick::CHANNEL_RED) . "</br>";
   
// Set channel Depth of CYAN and GREEN Channel
$im->setimagechanneldepth(Gmagick::CHANNEL_RED, 4);
   
echo "After Set Channel depth: " . "</br>";
echo $im->getImageChannelDepth(Gmagick::CHANNEL_RED) . "</br>";
  
?>

chevron_right


Output:

Before Set Channel depth:
8
After Set Channel depth:
4

Program 2:
Original Image:
https://cdncontribute.geeksforgeeks.org/wp-content/uploads/Screenshot-from-2018-10-16-23-23-54.png

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
   
$string = "Computer Science portal for Geeks!"
   
// Creating new image of above String 
// and add color
$im = new Gmagick(); 
$draw = new GmagickDraw(); 
   
// Fill the color in image 
$draw->setFillColor(new GmagickPixel('green')); 
   
// Set the text font size 
$draw->setFontSize(50); 
   
$metrix = $im->queryFontMetrics($draw, $string); 
$draw->annotation(0, 40, $string); 
$im->newImage($metrix['textWidth'], $metrix['textHeight'], 
        new GmagickPixel('white')); 
   
// Draw the image         
$im->drawImage($draw); 
$im->setImageFormat('jpeg'); 
   
// Using getImageChannelDepth function
// with red channel
echo "Before Set Channel depth: " . "</br>";
echo $im->getImageChannelDepth(Gmagick::CHANNEL_GREEN) . "</br>";
   
// Set channel Depth of Green Channel
$im->setimagechanneldepth(Gmagick::CHANNEL_GREEN, 8);
   
echo "After Set Channel depth: " . "</br>";
echo $im->getImageChannelDepth(Gmagick::CHANNEL_GREEN) . "</br>";
  
?>

chevron_right


Output:

Before Set Channel depth:
16
After Set Channel depth:
8

Reference: http://php.net/manual/en/gmagick.setimagechanneldepth.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.