<?php
$im = imagecreatetruecolor(400, 300);
$c_black = imageColorAllocate($im, 0,0,0);
$c_green = imageColorAllocate($im, 46,204,64);
$text = 'Hi';
$font = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf';
$p = imagettfbbox(40, 0, $font, $text);
imagettftext($im, 40, 0, (400 - $p[2])/2, 100, $c_green, $font, $text);
imagePng($im, '/tmp/image.png');
imagecreatetruecolor
- creates true color lib:GD image object with specified width & heightimageColorAllocate
- creates color object to later use in image$font
- path tottf
font to useimagettfbbox
- return size of box that given text fits intoimagettftext
- draw text with giventtf
font(400 - $p[2])/2
- calculatex
coordinate so our text is centered horizontallyimagePng
- saves image in PNG format to the given path
group: text
<?php
$im = imagecreatetruecolor(400, 300);
$c_black = imageColorAllocate($im, 0,0,0);
$c_green = imageColorAllocate($im, 46,204,64);
$text = 'Hi';
$font = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf';
$p = imagettfbbox(40, 0, $font, $text);
imagettftext($im, 40, 0, (400 - $p[2])/2, 100, $c_green, $font, $text);
imagePng($im, '/tmp/image.png');