Jump to content

How to change font in PHP?


CocaCola

Recommended Posts

Struggling to change font & font size.

 

This is the code i'm using, and it works perfectly fine apart from i need to change the outputted font's but as it's joined to a image i can't do that with css after the image so need to do it before its processed but have no clue how?

 

Please any help is appreciated.

<?php
// load the image from the file specified:
 
$im = imagecreatefrompng("image.png");
// if there's an error, stop processing the page:
if(!$im)
{
    die("");
}
// define some colours to use with the image
$black = imagecolorallocate($im, 80, 77, 77);
// get the width and the height of the image
$width = imagesx($im);

$height = imagesy($im);



$textt = $_GET['textt'];
$dealer = $_GET['dealer'];
$purchase = $_GET['purchase'];
$name = $_GET['name'];
$address = $_GET['address'];
$address1 = $_GET['address1'];

// store the text we're going to write in $textt


// calculate the left position of the text:
$leftTextPos = ( $width - imagefontwidth($font)*strlen($textt) )/2;
$leftTextPos = ( $width - imagefontwidth($font)*strlen($dealer) )/2;
// finally, write the string:

imagestring($im,$font,$leftTextPos-210,$height-295,$textt,$black);
imagestring($im,$font,$leftTextPos-210,$height-165,$dealer,$black);
imagestring($im,$font,$leftTextPos-200,$height-115,$purchase,$black);
imagestring($im,$font,$leftTextPos-130,$height-70,$name,$black);
imagestring($im,$font,$leftTextPos-100,$height-50,$address,$black);
imagestring($im,$font,$leftTextPos-00,$height-50,$address1,$black);
// output the image
// tell the browser what we're sending it
Header('Content-type: image/png');
// output the image as a png
imagepng($im);
// tidy up
imagedestroy($im);
?>
Edited by CocaCola
Link to comment
Share on other sites

Hi Q695,

 

My script does the following :

 

User inputs 5 details into text fields then hit's submit. This creates a image that's uneditable after it has been submitted. So i require to be able to change the font within the script itself before it being submitted.

 

Hopefully this makes sense.

Link to comment
Share on other sites

imagestring uses a set of built-in, fixed size monospaced font numbered 1 to 5.

 

If you use imagettftext instead you can use any ttf font on the server and any size. Calculating the text size is a little more difficult as you have to call imagettfbbox which returns the corner coords of a box that contains the text

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.