PHP | Imagick setImageType() Function

The Imagick::setImageType() function is an inbuilt function in PHP which is used to set the image type.

Syntax:

bool Imagick::setImageType( int $image_type )

Parameters: This function accepts a single parameter $image_type which contains an interger value corresponding to one of IMGTYPE constants. We can also pass the constant directly like setImageType(imagick::IMGTYPE_GRAYSCALE);.



All the IMGTYPE constants are listed below:

  • imagick::IMGTYPE_UNDEFINED (0)
  • imagick::IMGTYPE_BILEVEL (1)
  • imagick::IMGTYPE_GRAYSCALE (2)
  • imagick::IMGTYPE_GRAYSCALEMATTE (3)
  • imagick::IMGTYPE_PALETTE (4)
  • imagick::IMGTYPE_PALETTEMATTE (5)
  • imagick::IMGTYPE_TRUECOLOR (6)
  • imagick::IMGTYPE_TRUECOLORMATTE (7)
  • imagick::IMGTYPE_COLORSEPARATION (8)
  • imagick::IMGTYPE_COLORSEPARATIONMATTE (9)
  • imagick::IMGTYPE_OPTIMIZE (10)

Return Value: This function returns TRUE on success.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Set the Image Type to imagick::IMGTYPE_GRAYSCALEMATTE
$imagick->setImageType(3);
  
// Display the image
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Set the Image Type to imagick::IMGTYPE_BILEVEL
$imagick->setImageType(1);
  
// Display the image
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/imagick.setimagetype.php



My Personal Notes arrow_drop_up

I love Open Source Culture have contributed to phpMyAdmin, LibreOffice, Ant-Design and Web is my passion

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.