I'm working on a project for a friend that will generate an image of text where the two variables are font and size using php's Graphics Draw library.
For this example, I am using the free font After Shok.
The main functions I am trying to wrap my mind around are imagettfbbox, imagecreatetruecolor, and imagettftext
The script seems to work great with getting the horizontal, but the vertical height/positioning is off.
The first script displays the position of the image using the imagettfbbox function.
The second script, which is nearly identical to the first, will display the image.PHP Code:<?php
$text='Imperium';
$font = 'storage/fonts/After_Shok.ttf';
$size=180;
$bbox = imagettfbbox($size, 0, $font, $text);
$width = '0';
$angle = 0;
$a=abs($bbox[3]);
$b=abs($bbox[5]);
$height=$a+$b;
$im = imagecreatetruecolor($bbox[2]-$bbox[0], $height);
$bg = imagecolorallocate($im, 5, 0, 5);
$color = imagecolorallocate($im, 255, 0, 0);
$width = '0';
$angle = 0;
##$height=abs($bbox[3])+abs($bbox[5]);
imagefill($im, 0, 0, 5);
imagettftext($im, $size, $angle, $width-$bbox[0], $size+10, $color, $font, $text);
echo "width=$width-$bbox[0]<br>height=$size+10<br>
lower left corner = $bbox[0],$bbox[1]<br>
lower right corner = $bbox[2], $bbox[3]<br>
upper right corner = $bbox[4], $bbox[5]<br>
upper left corner = $bbox[6], $bbox[7]<br>";
##header('Content-type: image/png');
##imagepng($im);
imagedestroy($im);
?>
Feel free to play around with it. I'm open to any criticism and ideas.PHP Code:<?php
$text='Imperium';
$font = 'storage/fonts/After_Shok.ttf';
$size=18;
$bbox = imagettfbbox($size, 0, $font, $text);
$width = '0';
$angle = 0;
$a=abs($bbox[3]);
$b=abs($bbox[5]);
$height=$a+$b;
$im = imagecreatetruecolor($bbox[2]-$bbox[0], $height);
$bg = imagecolorallocate($im, 5, 0, 5);
$color = imagecolorallocate($im, 255, 0, 0);
$width = '0';
$angle = 0;
##$height=abs($bbox[3])+abs($bbox[5]);
imagefill($im, 0, 0, 5);
imagettftext($im, $size, $angle, $width-$bbox[0], $size+10, $color, $font, $text);
##echo "width=$width-$bbox[0]<br>height=$size+10<br>
##lower left corner = $bbox[0],$bbox[1]<br>
##lower right corner = $bbox[2], $bbox[3]<br>
##upper right corner = $bbox[4], $bbox[5]<br>
##upper left corner = $bbox[6], $bbox[7]<br>";
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>



Reply With Quote
Bookmarks