Open In App

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:

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


Article Tags :