Page 1 of 1

imagestring font size

Posted: Fri Jan 20, 2006 10:54 am
by Nathaniel

Code: Select all

// THUMBNAILS

function imagestringcentered($img, $font, $text, $color) {
$cy = (imagesy($img)/2) - (imagefontwidth($font)/2);
imagestring($img,$font,imagesx($img) / 2 - strlen($text) * imagefontwidth($font) / 2,$cy,$text,$color);
}

//$destination = filename of original, not-watermarked large photograph
$imageinfo = array();
$imageinfo = getimagesize($destination,$imageinfo);
$image = imagecreatefromjpeg($destination);
    
imagejpeg($image, '/home/ap/public_html/thumbnails/' . $insert->insertID() . '.jpg');  //copy original to thumbnails
imagejpeg($image, '/home/ap/public_html/photos/' . $insert->insertID() . '.jpg'); //copy original to photos (smaller version, but not too small)

$old_x = $imageinfo[0];
$old_y = $imageinfo[1];

$new_y = (500/$old_x)*$old_y; //always 500 pixels wide

$old_photo = imagecreatefromjpeg('/home/ap/public_html/photos/' . $insert->insertID() . '.jpg');
imagestringcentered($old_photo, 6, 'Copyright AbovePhotography.com.au', imagecolorallocate($old_photo, 255, 255, 255));

$photo = imagecreatetruecolor(500,$new_y);
$white = imagecolorallocate($photo,255,255,255);
imagefill($photo,0,0,$white);

imagecopyresampled ( $photo, $old_photo, 0, 0, 0, 0, 500, $new_y, $old_x, $old_y); 
	
$new_y = (200/$old_x)*$old_y; //always 200 pixels wide

$old_thumb = imagecreatefromjpeg('/home/ap/public_html/thumbnails/' . $insert->insertID() . '.jpg');

$thumbnail = imagecreatetruecolor(200,$new_y);
$white = imagecolorallocate($thumbnail,255,255,255);
imagefill($thumbnail,0,0,$white);

imagecopyresampled ( $thumbnail, $old_thumb, 0, 0, 0, 0, 200, $new_y, $old_x, $old_y); 
	
imagejpeg($thumbnail, '/home/ap/public_html/thumbnails/' . $insert->insertID() . '.jpg'); 
imagejpeg($photo, '/home/ap/public_html/photos/' . $insert->insertID() . '.jpg');

imagedestroy($image);
imagedestroy($thumbnail);
imagedestroy($photo);
Anyway, all that to get this. Why is the font so small, and what do I do about it? I've tried every integer (1, -20, -800, +83) I could think of for the value of $font in the imagestringcentered function, but it's always that small. Sometimes a bit larger, but not large enough.

- Nathaniel

Posted: Fri Jan 20, 2006 2:51 pm
by onion2k
The problem is that you're using imagestring(). imagestring() uses PHP's built in fonts .. they're not scalable. The biggest one is 6.

If you want large text you'll need to use imagettftext(), or imagepstext() depending on what your installation of PHP GD was compiled with. In order to work out the position to center the text you'll need to use imagettfbbox() or imagepsbbox(). I've written an article that covers TrueType text with GD: http://www.phpgd.com/articles.php?article=7

Alternatively, create a watermark image and use imagecopy().

Posted: Fri Jan 20, 2006 4:39 pm
by Nathaniel
Alright, thank you onion. I'll see what happens.