Tuesday, October 7, 2025
HomeLanguagesPHP | Imagick getImageGravity() Function

PHP | Imagick getImageGravity() Function

The Imagick::getImageGravity() function is an inbuilt function in PHP which is used to get the image gravity. Difference between getGravity() and getImageGravity() is that the former applies for the whole Imagick object whereas the latter gets the gravity of the current image (in case of multiple images) in the sequence.

Syntax:

int Imagick::getImageGravity( void )

Parameters: This function does not accept any parameters.

Exceptions: This function throws ImagickException on error.

Return Value: This function returns an integer value which represents the gravity constant for the image.

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)

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

Program 1:




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


Output:

0 //which corresponds to imagick::GRAVITY_NORTHWEST.

Program 2:




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


Output:

4 //which corresponds to imagick::GRAVITY_CENTER.

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

RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS