1. Home
  2. Computing & Technology
  3. PHP / MySQL

GD Library - The Basics of Drawing with PHP

By , About.com Guide

3 of 7

Playing with Fonts

Image created with GD Library

Our Code Creates This Image

<?php
header ("Content-type: image/png");
$handle = ImageCreate (130, 50) or die ("Cannot Create image");
$bg_color = ImageColorAllocate ($handle, 255, 0, 0);
$txt_color = ImageColorAllocate ($handle, 0, 0, 0);
ImageTTFText ($handle, 20, 15, 30, 40, $txt_color, "/Fonts/Quel.ttf", "Quel");
ImagePng ($handle);
?>
Although most of our code has stayed the same you will notice we are now using ImageTTFText () instead of ImageString (). This allows us to choose our font, which must be in TTF format.

The first parameter is our handle, then font size, rotation, starting X, starting Y, text color, font, and finally our text. For the font parameter, you need to include the path to font file. For our example I have placed the font Quel in a folder called Fonts. As you can see from our example, we have also set the text to print at a 15 degree angle.

If your text isn't showing, you may have the path to your font wrong. Another possibility is that your Rotation, X and Y parameters are placing the text outside of the viewable area.

Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Graphics- GD Library
  5. GD Library - PHP GD Library - GD Image Library

©2009 About.com, a part of The New York Times Company.

All rights reserved.