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:
<?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(); ?> |
Output:
Program 2:
<?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(); ?> |
Output:
Reference: https://www.php.net/manual/en/imagick.setgravity.php