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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.