Dynamic image creation using PHP

Dynamic image creation with PHP: Part II

On page 1 we looked at how PHP can be used to generate images dynamically. Now we will explore a few more advanced features of the PHP GD image extension.

PostScript fonts

PostScript fonts are very widely used and were purpose built for software development. The quality of fonts available and their sheer variety make them a must for anyone interested in graphics development.

The Type1 Font library (t1lib) is used by the PHP Image extension to provide PostScript font functionality to developers. The t1lib package comes with several free fonts included. They can be found in the t1lib-1.3.1/Fonts/Type1/ directory of the unpacked archive.

Executing the following script will generate the 'Hello World' image. Notice how the fonts change from top to bottom.


01 <?
02 header("Content-type: image/jpeg");
03 $fontdir = '/home/swm/t1lib-1.3.1/Fonts/type1/';
04 $handle = opendir($fontdir);
05 
06 $fonts = array();
07 $maxwidth = 0;
08 $str = "Hello World";
09 $fontsize = 30;
10 $spacing = $fontsize + 10;
11 
12 while(($file = readdir($handle)) !== false) {
13     if(!is_file($fontdir.$file))
14    continue;
15  $font = imagepsloadfont($fontdir.$file);
16  if(!$font)
17    continue;
18 
19   list($x1,$y1,$x2,$y2) = imagepsbbox($str,$font,$fontsize,0,0,0);
20 
21    if($maxwidth < $x2)
22    $maxwidth = $x2;
23   $fonts[] = $font;
24 }
25 
26 $im = imagecreate($maxwidth +40,(count($fonts) * $spacing) + ($spacing - $fontsize));
27 $black = Imagecolorallocate($im, 0, 0, 0);
28 $white = Imagecolorallocate($im, 255, 255, 255);
29 $x = 20;
30 $y = 0;
31 for($i=0;$i<count($fonts);$i++) {
32   imagepstext($im,$str,$fonts[$i],$fontsize,$white,$black,$x,$y + $spacing);
33   $y += $spacing;
34   imagepsfreefont($fonts[$i]);
35 }
36 ImageJpeg($im, "", 100);
37 ImageDestroy ($im);
38 ?>

Now for the script itself. First, we set the Content Type header so that Web browsers understand that the content is an image. The third line sets the variable $fontdir to a directory containing fonts (change this to the location of the t1lib fonts on your system).

Line eight sets the string the script will be printing - 'Hello World' - while lines nine and 10 register the font size and line spacing in pixels, respectively.

The script then loops through the files in $fontdir, being careful on line 13 to avoid entries other than files and those that are not PostScript fonts in .pbf format (the only format supported). The font is then loaded: imagepsloadfont(). We do this here so that we can measure the width of the string $str using this font, because we need this information in order to generate a palette large enough to ensure that no strings will be truncated.

The script measures the width of the string using the imagepsbbox() routine which returns an array whose elements are, in order, the lower left x- and y-coordinates and the upper right x- and y-coordinates. Using the upper right x-coordinate is sufficient for measuring the horizontal width of the text drawn in the particular font. This width is then compared to the running largest width encountered ($maxwidth). If the width of the current string is longer than that of the previous maximum, then $maxwidth is replaced with the new width. Finally, the script adds the font handle $font to an array of fonts, $fonts.

Line 26 generates the new palette, $im. The width is $maxwidth plus 40 pixels - for padding. The height is 40 pixels times the number fonts. We also add the difference between the height of a line space (40 pixels) and that of the font size (30 pixels) to give the effect of padding at the bottom of the image.

Finally, the script iterates through the list of fonts and draws the text on the image using imagepstext(). The last two arguments to imagepstext() are the lower left x- and y-coordinates where the text is to begin. For each iteration, we advance the y-coordinate (line 33). The 34th line also frees the loaded font from the extension. The PostScript library can be quite memory intensive.

Finally, line 36 outputs the image in JPEG format with 100 per cent image quality.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World

Comments

Comments are now closed.

Latest News Articles

Most Popular Articles

Follow Us

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Resources

Best Deals on GoodGearGuide

Compare & Save

Deals powered by WhistleOut
WhistleOut

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?