Add a simple text box to an image using PHP's Imagick Library
Composer:
composer install christianberkman/imagick-text-box
use \Imagick;
use ImagickTextBox\ImagickTextBox;
$image = new Imagick()->readImage('your-image.php');
$textBox = new ImagickTextBox($image);
$textBox->string = 'Hello World';
$textBox->draw(Imagick::GRAVITY_NORTHWEST);
Also see example.php with a more detailed example of usage.
void __construct(Imagick $image)
Construct the class and initialize ImagickDraw objectsvoid draw(int $gravity = Imagick::GRAVITY_SOUTHEST)
draw the textbox on theImagick $image
objectbool textFits()
Returns if the text fits on the given image
All these objects are fully accessible and can be manipulated to set desired fill color, font size, etc.
Imagick $image
Your image objectImagickDraw $text
Text object, initialized by ImagickTextBoxImagickDraw $box
Box object, initializes by ImagickTextBox
string $string = 'Hello World'
The string of text to printint $margin_x = 10
Margin (px) from the side (does not apply to north, south, center)int $margin_y = 10
Margin (px) from the top/bottom (does not apply to west, east, center)int $padding = 5
Padding (px) of text inside text boxint $gravity = Imagick::GRAVITY_SOUTHWEST
Imagick Gravity Constantfloat $heightFactor = 0.6
Height factor to use. By using the default font, a factor of 1 produces a text box with a lot of vertical whitespace, 0.6 seems to do the trick.