TTF Point Sizes and PPI?

[email protected] ("Evan Kaufman") Sat, 30 Aug 2008 00:55:52 -0500
Newsgroups php.gd.devel
Message-ID <[email protected]>
I was rendering images in PHP using the GD extension, and I noticed
that the point size in which text is rendered in GD is different than
the default in Photoshop, ImageMagick, etc.  The logical hypothesis I
came to is that it has something to do with the PPI (pixels per inch).

So I created an image with GD big enough in which to render a sentence
at 25pt, which produces an image at 72ppi, the usual for screen
display, but with an unusually large font pointsize:

  <?php
  /* send content type so we can see the image */
  header("Content-type: image/png");

  /* get bounding dimensions of the given text in the given font face */
  $bounds = imagettfbbox(25, 0, './FreeMono.ttf', 'The quick fox jumps
over the lazy dog');

  /* determine the minimum image dimensions in which to to render the text */
  $width = $bounds[2];
  $height = $bounds[1] - $bounds[7];

  /* create an image to work with */
  $im = imagecreatetruecolor($width, $height);

  /* allocate background and foreground colors */
  $white = imagecolorallocate($im, 255, 255, 255);
  $black = imagecolorallocate($im, 0, 0, 0);

  /* fill the background color */
  imagefilledrectangle($im, 0, 0, $width, $height, $white);

  /* render the text in the foreground color */
  imagettftext($im, 25, 0, 0, abs($bounds[7]), $black,
'./FreeMono.ttf', 'The quick fox jumps over the lazy dog');

  /* see what we've got */
  imagepng($im);
  imagedestroy($im);
  ?>

The result is visible here:

  http://markovchainsaw.com/gdppi/fig1.png

If I load this into photoshop at 72ppi, and superimpose the same
sentence at the same size of 25pt (in red), we easily notice the size
discrepancy:

  http://markovchainsaw.com/gdppi/fig2.png

Now, in photoshop I create a new 96ppi image, copy and paste the image
from figure 1, and render the sentence again at 25pt (again in red),
it's just slightly too big, but after some more trial and error, I
nail down the magic number, 95.95ppi:

  http://markovchainsaw.com/gdppi/fig3.png

So GD for PHP renders images at 72ppi, but TrueType fonts at 95.95ppi.
 Does anyone know exactly why this is, or if there's a way to rectify
it?