I am trying to create a title on the top of each dynamic page. The pages will be created and deleted regularly via a CMS, so it must stay dynamic.
I am having an issue figuring out how to calculate image width based upon string length and font width. Basically, I can narrow it down to “close”, but then when I use a string that’s all CAPS, the string falls off of the image. I would also like the string to be centered in the image (best if the image was only ~5 pixels larger than the string on each side.
Any input is appreciated.
[code=php]<?php
$font = “coopheavy.ttf”;
$title = $_REQUEST[‘title’];
$imw = strlen($title) * 13;
header (“Content-type: image/png”);
$image = imagecreate($imw, 30);
$bg = imagecolorallocate($image, 255, 255, 255);
$fontColor = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, 24, 0,0, 24, $fontColor, $font, $title);
imagepng($image);
imagedestroy($image);
?>