Jump to content

[SOLVED] Change Font GD Extension


peeps

Recommended Posts

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

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.