You can use stil/gd-text class. Disclaimer: I am the author.
<?php
use GDText\Box;
use GDText\Color;
$img = imagecreatefromjpeg('image.jpg');
$textbox = new Box($img);
$textbox->setFontSize(12);
$textbox->setFontFace('arial.ttf');
$textbox->setFontColor(new Color(255, 255, 255));
$textbox->setBox(
50, // distance from left edge
50, // distance from top edge
200, // textbox width
100 // textbox height
);
// now we have to align the text horizontally and vertically inside the textbox
$textbox->setTextAlign('left', 'top');
// or like this:
// $textbox->setTextAlign('right', 'top');
// $textbox->setTextAlign('center', 'top');
// $textbox->setTextAlign('center', 'bottom');
// $textbox->setTextAlign('right', 'center');
// it accepts multiline text
$textbox->draw("text to write on picture\nanother line below");
Demonstration:
