PHP | Imagick setImageGravity() Function

The Imagick::setImageGravity() function is an inbuilt function in PHP which is used to set the gravity property of an image. Difference between setGravity() and setImageGravity() is that the former applies for the whole Imagick object whereas the latter sets the gravity of the current image (in case of multiple images) in the sequence.

Syntax:

bool Imagick::setImageGravity( int $gravity )

Parameters: This function accepts single parameter $gravity which holds an integer 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::setImageGravity() 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
$imagick->setImageGravity(7);
  
// Get the Gravity
$gravity = $imagick->getImageGravity();
echo $gravity;
?>

chevron_right


Output:

7

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Set the Gravity
$imagick->setImageGravity(3);
  
// Get the Gravity
$gravity = $imagick->getImageGravity();
echo $gravity;
?>

chevron_right


Output:

3

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


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.