peeps Posted August 27, 2007 Share Posted August 27, 2007 I found a tutorial on how to write text onto an existing image, but now I am having trouble changing the font of the outputted text. <?php $text = $_GET['text']; // load the image from the file specified: $im = imagecreatefrompng("button.png"); if(!$im) { die(""); } $black = imagecolorallocate($im, 0, 0, 0); $width = imagesx($im); $height = imagesy($im); // calculate the left position of the text: $leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2; //write the string imagestring($im, 4, $leftTextPos-8, $height-23, $text, $black); // output the image // tell the browser what we're sending it Header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?> Link to comment https://forums.phpfreaks.com/topic/66906-solved-change-font-gd-extension/ Share on other sites More sharing options...
Barand Posted August 27, 2007 Share Posted August 27, 2007 Here's an example <?php $font = 'c:/windows/fonts/arial.ttf'; // define path to preferred ttf file $text = $_GET['text']; // load the image from the file specified: $im = imagecreatefrompng("button.png"); if(!$im) { die(""); } $black = imagecolorallocate($im, 0, 0, 0); $width = imagesx($im); $height = imagesy($im); // calculate the left position of the text: $bb = imagettfbbox(12, 0, $font, $text); // get text bounding box $textWidth = $bb[2] - $bb[0]; $leftTextPos = ( $width - $textWidth )/2; //write the string imagettftext($im, 12, 0, $leftTextPos-8, $height-8, $black, $font, $text); // note y pos is text baseline with ttf fonts // output the image // tell the browser what we're sending it Header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?> Link to comment https://forums.phpfreaks.com/topic/66906-solved-change-font-gd-extension/#findComment-335430 Share on other sites More sharing options...
peeps Posted August 27, 2007 Author Share Posted August 27, 2007 Thanks a lot, works like a charm. Link to comment https://forums.phpfreaks.com/topic/66906-solved-change-font-gd-extension/#findComment-335748 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.