Re: [GD-DEVEL] TTF Point Sizes and PPI?

[email protected] ("Evan Kaufman") Thu, 4 Sep 2008 22:32:46 -0500
Newsgroups php.gd.devel
Message-ID <[email protected]>
Thanks for the response, and I look forward to a proper solution in
time.  In the meantime, I have managed to put together a workaround if
anyone else is facing the same problem...In the interest of terseness,
there's no error checking in my example below:

<?php

// load the base image we want annotated with text
$baseResource = imagecreatefrompng('/home/user/some-image.png');

// specify whatever text we want to render
$text = 'The quick fox jumps over the lazy dog.';

// get bounds of given text and use them to create new image appropriately sized
$bounds = imagettfbbox(20.0, 0, 'FreeMono.ttf', $text);
$textResource = imagecreatetruecolor($bounds[2], $bounds[1] - $bounds[7]);

// allocate foreground color for text and background color for transparency
$color = imagecolorallocate($textResource, 0, 0, 0); # black
$background = imagecolorallocatealpha($textResource, 255, 255, 255,
127); # white

// fill image with transparent background color
imagefill($textResource, 0, 0, $background);

// create new image and render text to fit in it
imagettftext($textResource, 20.0, 0, 0, abs($bounds[7]), $color,
'FreeMono.ttf', $text);

// get dimensions of rendered text and calculate size for downsampling the ppi
$textWidth = imagesx($textResource);
$textHeight = imagesy($textResource);
$targetWidth = round( ($textWidth / 95.95) * 72 );
$targetHeight = round( ($textHeight / 95.95) * 72 );

// overlay resized text onto the base image, and destroy the text
image we no longer need
$status = imagecopyresampled($baseResource, $textResource, 0, 100, 0,
0, $targetWidth, $targetHeight, $textWidth, $textHeight);
imagedestroy($textResource);

// output the newly annotated image
header('Content-Type: image/png');
imagepng($originalResource);

?>


On Thu, Sep 4, 2008 at 12:04 PM, Pierre Joye <[email protected]> wrote:
> hi!
>
> On Sat, Aug 30, 2008 at 7:55 AM, Evan Kaufman <[email protected]> wrote:
>
>> 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?
>
> Not right now, but it will be available soon. Maybe in  5.3 but that's
> not certain. It will be possible to do it using the imagefttext extra
> parameter to define all supported options.
>
>
> Cheers,
> --
> Pierre
> http://blog.thepimp.net | http://www.libgd.org
>