imagestring font size

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

imagestring font size

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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().
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Alright, thank you onion. I'll see what happens.
Post Reply