PCHomepage
Programmer
I created a simple function to take text and create an image from it and it working but I've not been able to work out how to set the spacing between the lines (if there is more than one) and right now they are too far apart. If more than one line, the width of each varies and I would like them to be centered with each other but cannot work out how to even begin to do that! Any ideas on these things?
PHP:
function textimage($string, $size, $FontName, $CenterAlign=false) {
$FontPath = $_SERVER['DOCUMENT_ROOT'] . "/functions/truetype/";
$font = $FontPath . $FontName;
$stringbase = "Copyright © ". date("Y");
if ($CenterAlign === true):
$string = html_entity_decode($stringbase ."\n". $string);
$text_dimensions = imagettfbbox($size,0,$font,$string);
// Some code here
else:
$text_dimensions = imagettfbbox($size,0,$font,$string);
endif;
$width = abs($text_dimensions[4] - $text_dimensions[0] + 5);
$height = abs($text_dimensions[5] - $text_dimensions[1]);
$x_offset = ($width / 2) - ((min($text_dimensions[2],$text_dimensions[4]) - max($text_dimensions[0],$text_dimensions[6])) / 2);
$y_offset = abs($text_dimensions[5]);
$image = imagecreatetruecolor($width,$height);
$background_color = imagecolorallocate($image,0xEE,0xEE,0xEE);
$text_color = imagecolorallocate($image,0x00,0x00,0x55);
$shadow_color = imagecolorallocate($image,0x77,0x77,0x77);
imagefill($image,0,0,$background_color);
$lines = explode('\n',$string);
foreach ($lines as $line):
imagettftext($image,$size,0,$x_offset,$y_offset,$text_color,$font,$line);
$y_offset += 10;
endforeach;
header("Content-Type: image/png");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
return imagepng($image);
imagedestroy($image);
}
echo textimage("More Text Here", 10, "vineritc.ttf", true);