Friday, November 21, 2025
HomeLanguagesPHP | Imagick setImageChannelDepth() Function

PHP | Imagick setImageChannelDepth() Function

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

Syntax:

bool Imagick::setImageChannelDepth( $channel, $depth )

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:

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

Return Value: This function returns True on success.

Below programs illustrate the Imagick::setImageChannelDepth() function in PHP:

Original Image:
https://geeksforgeeks.org/wp-content/uploads/2023/10/neveropen-21-17.png

Program 1:




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


Output:

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

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

Program 2:




<?php 
  
$string = "Computer Science portal for Geeks!"; 
  
// Creating new image of above String 
// and add color
$im = new Imagick(); 
$draw = new ImagickDraw(); 
  
// Fill the color in image 
$draw->setFillColor(new ImagickPixel('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 ImagickPixel('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(imagick::CHANNEL_GREEN) . "</br>";
  
// Set channel Depth of Green Channel
$im->setImageChannelDepth(imagick::CHANNEL_GREEN, 8);
  
echo "After Set Channel depth: " . "</br>";
echo $im->getImageChannelDepth(imagick::CHANNEL_GREEN) . "</br>";
?>


Output:

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

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11995 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7165 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS