Wednesday, July 29, 2026
HomeLanguagesPHP | Gmagick setimagetype() Function

PHP | Gmagick setimagetype() Function

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

Syntax:

Gmagick Gmagick::setimagetype( int $imgType )

Parameters: This function accepts a single parameter $imgType which holds an integer value corresponding to one of the IMGTYPE constants.

All the IMGTYPE constants are listed below:

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

Return Value: This function returns Gmagick object on success.

Exceptions: This function throws GmagickException on error.

Below given programs illustrate the Gmagick::setimagetype() function in PHP:

Used Image:

Program 1:




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('neveropen.png');
  
// Set the image type
$gmagick->setimagetype(1);
  
// Display the image
header("Content-Type: image/png");
echo $gmagick;
?>


Output:

Program 2:




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('neveropen.png');
  
// Set the image type
$gmagick->setimagetype(3);
  
// Display the image
header("Content-Type: image/png");
echo $gmagick;
?>


Output:

Program 3:




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('neveropen.png');
   
// Create a GmagickDraw object
$draw = new GmagickDraw();
   
// Set the color
$draw->setFillColor('white');
   
// Function to draw rectangle
$draw->rectangle(0, 0, 800, 400);
   
// Set the fill color
$draw->setFillColor('red');
   
// Set the font size
$draw->setfontsize(50);
   
// Annotate a text
$draw->annotate(30, 100, 'neveropen');
   
// Use of drawimage function
$gmagick->drawImage($draw);
  
// Set the image type
$gmagick->setimagetype(2);
  
// Display the image
header("Content-Type: image/png");
echo $gmagick;
?>


Output:

Reference: https://www.php.net/manual/en/gmagick.setimagetype.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
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6981 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS