PHP | Imagick setGravity() Function

The Imagick::setGravity() function is an inbuilt function in PHP which is used to set the global gravity property for the Imagick object.

Syntax:

bool Imagick::setGravity( int $gravity )

Parameters: This function accepts single parameter $gravity which holds an int value.
List of GRAVITY constants are given below:

  • imagick::GRAVITY_NORTHWEST (0)
  • imagick::GRAVITY_NORTH (1)
  • imagick::GRAVITY_NORTHEAST (2)
  • imagick::GRAVITY_WEST (3)
  • imagick::GRAVITY_CENTER (4)
  • imagick::GRAVITY_EAST (5)
  • imagick::GRAVITY_SOUTHWEST (6)
  • imagick::GRAVITY_SOUTH (7)
  • imagick::GRAVITY_SOUTHEAST (8)

Return Value: This function returns a boolean value.

Below programs illustrate the Imagick::setGravity() 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 Gravity to imagick::GRAVITY_NORTHWEST
$imagick->setGravity(0);
  
// Write the caption in a image
$imagick->newPseudoImage(800, 350, "caption:GeekforGeeks");
  
$imagick->floodfillPaintImage("blue", 1, "white", 1, 1, false);
  
// Show the output
$imagick->setformat('png');
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 Gravity to imagick::GRAVITY_SOUTH
$imagick->setGravity(7);
  
// Write the caption in a image
$imagick->newPseudoImage(800, 350, "caption:GeekforGeeks");
  
$imagick->floodfillPaintImage("blue", 1, "white", 1, 1, false);
  
// Show the output
$imagick->setformat('png');
header("Content-Type: image/png");
  
echo $imagick->getImageBlob();
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/imagick.setgravity.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.