Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 43,846 times

Contents

Downloads

Related Categories

Image Generation on the FLY using PHP - Changing the font

jdk

Changing the font

Changing the font of our random number

To control the look and style attributes of our random number, we can replace the imagestring function with the imagettftext function, which lets us control some of the aspects of the font used to render the text.

The imagettftext function takes eight arguments, as described below:

array Imagettftext (int im, int size, int angle, int x, int y, int col, string fontfile, string text)

im: A reference to an image canvas created using the imagecreate function
size: The height of the font, in pixels
angle: The rotation of the font. Use 0 for standard horizontal display
x: The starting x co-ordinate of where the text will be drawn
y: The starting y co-ordinate of where the text will be drawn
col: A reference to a color created using the imagecolorallocate function
fontfile: The path and filename to the font that the text will be rendered in (more on this in a minute)
text: The actual string of text to be drawn

So, for our example, we would used the imagettftext function instead of the imagestring function, like this:

Imagettftext($img_number, 30,0,10,33,$black,'trebuchet.ttf',$number);

Notice the seventh argument? It's the location of the true-type font file that will be used to render our text onto the image. In our example, the file trebuchet.ttf is in the same directory as our script. Trebuchet.ttf is contained in the support material at the end of this article.

The Imagettftext file uses the FreeType font support, which is built-in to all of the latest releases of the GD image library. You can read more about the FreeType project here.

Once you have saved the random_number.php file with the new Imagettftext function instead of the imagestriing function, reload the page in your browser. You should see something like this:

A fellow who believes in simple things, simple way.

Comments

  • dynamic image on the fly

    Posted by babulgogoi on 02 Apr 2005

    Hi

    I saw them using a image-on-the-fly script as (at www.worldspaceasia.com):
    Image Generation on the fly

    Posted by chukws on 06 Nov 2004

    Very good tutorial and useful. But am wondering, if it can be converted to be used as a counter in a website, to get the number of visitors to the site. It will then start from a certain number and co...

  • php_gd.dll

    Posted by altimastr on 03 Sep 2004

    after installing the gd.dll am i suppose to uncomment it in the php.ini file and if so do i restart my server to get it to load??? after putting it in my php.ini file and uncommenting it nothing came ...

  • Checking

    Posted by niceguy_reddy on 18 Aug 2004

    I used the script it was good. The random script is accessed as an image in another script, then how to access the number displayed on the image so that i can check that the user entered the correct d...

  • great tutorial, thanks

    Posted by Idea_Rat on 05 Aug 2004

    very well-written & useful javascript:smilie(':)')