Resize text from
Posted: Thu Jun 10, 2010 4:01 pm
Hey guys, I'm working on a project and need to create an image from text due to the fact, the headers need a shadow for a more 3d-ish look and i can't photoshop them because users create them dynamically... the script below is taking care of my needs but also creates a problem...
I would like the script to resize the font to fit the image.... is this possible? If not, I'll have to restrain the user from making the string too long.
P.S. Yes, I know this code is a resource hog, projects still in development... soon this script will save the image to a folder and upon load see if it was already created and if so forward it to the user, if not then create it.
I would like the script to resize the font to fit the image.... is this possible? If not, I'll have to restrain the user from making the string too long.
Code: Select all
<?php
if(empty($_GET['text'])) {
$_GET['text'] = 'No text defined.';
}
$_GET['text'] = html_entity_decode(urldecode($_GET['text']));
$im = imagecreate(550, 50) or die("Cannot Initialize new GD image stream");
$top = imagecreatefrompng('top.png');
$background_color = imagecolorallocate($im, 255, 255, 0); //RGB color background.
$text_color = imagecolorallocate($im, 40, 40, 40); //RGB color text.
$shadow_color = imagecolorallocate($im, 200, 200, 200); //RGB color text shadow.
$border_color = imagecolorallocate($im, 0, 0, 0); //RGB color border.
imagefilledrectangle($im, 0, 0, 550, 50, $border_color);
imagefilledrectangle($im, 2, 2, 547, 47, $background_color);
//bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
imagecopy($im,$top, 4,4,0,0,44,44);
imagettftext($im, 30, 0, 51, 40, $shadow_color, './fonts/Futura.ttf', $_GET['text']);
imagettftext($im, 30, 0, 48, 40, $text_color, './fonts/Futura.ttf', $_GET['text']);
header("Content-type: image/png");
return imagepng($im);
imagedestroy($im);