Home arrow PHP arrow Page 6 - Generating Images on the Fly With PHP

Generating Images on the Fly With PHP

For developers like Divyesh, one of the most popular features of PHP is real-time image generation. That is, we can create a new image canvas, "paint" it, and either save it to a file, or send it directly to the browser. In this article, Divyesh will show you how to create an image in real-time with PHP.

Author Info:
By: Divyesh Jariwala
Rating: 4 stars4 stars4 stars4 stars4 stars / 69
December 13, 2001
  1. · Generating Images on the Fly With PHP
  2. · The GD image library
  3. · Creating our image
  4. · Displaying our image
  5. · Generating the random number
  6. · Changing the font of our random number
  7. · Conclusion

print this article

Generating Images on the Fly With PHP - Changing the font of our random number
(Page 6 of 7 )

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)
  1. im: A reference to an image canvas created using the imagecreate function
  2. size: The height of the font, in pixels
  3. angle: The rotation of the font. Use 0 for standard horizontal display
  4. x: The starting x co-ordinate of where the text will be drawn
  5. y: The starting y co-ordinate of where the text will be drawn
  6. col: A reference to a color created using the imagecolorallocate function
  7. fontfile: The path and filename to the font that the text will be rendered in (more on this in a minute)
  8. 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 at http://www.freetype.org/freetype2/index.html.

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:

Our random number now has a larger, bolder font
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials