stil / gd-text

PHP class making it easy to type text on pictures. Supports multi-lined text, horizontal and vertical alignment.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Font size calculating

vendelieu opened this issue · comments

commented

Could you add the adaptability of the size of the font.
Calculating the size of the font depending on the length of the text. This will allow small texts to see more clearly, bigger.

Try This First Declare the Text In a Variable

$imgtxt = 'Some Text';

$imy = imagesy($im);

$txtchars = strlen($imgtxt);

$customfontsize = '15';

$fontsize = ($imy - $txtchars) / 5;
$fontsize = $fontsize + $customfontsize;

And Then

$box->setFontSize($fontsize);
$box->draw($imgtxt);

@Lukasss93
does you text size change to big when the string is small if yes then
try playing around with the initial font size and other values and see if you get the desired results

@CFlanc

does you text size change to big when the string is small

Nope :(

Then something must be wrong with the code
@Lukasss93
Ok maybe because you are creating image from color
You cant use imagey command

You should enter the actual width

This is my code:
//config
$text='asd sad asdsa asd asdasd asda asdadasdd dasdasdasd aasdasd';
$initialfontsize=50;

//canvas
$im = imagecreatetruecolor(512, 512);

//set transparent background
$background=imagecolorallocate($im, 123, 123, 123);
$background=imagecolortransparent($im, $background);
imagefill($im, 0, 0, $background);

//auto size font?
$imy = imagesy($im);
$txtchars = strlen($text);
$fontsize = ($imy - $txtchars) / 5;
$fontsize = $fontsize + $initialfontsize;

//text
$box = new Box($im);
$box->setFontFace(FCPATH.'assets/fonts/arial.ttf');
$box->setLineHeight(1);
$box->setFontColor(new Color(0, 0, 0));
$box->setBox(0, 0, 512, 512);
$box->setTextAlign('center', 'center');
$box->setStrokeColor(new Color(255, 255, 255));
$box->setStrokeSize(3);
$box->setFontSize($fontsize);
$box->draw($text);

//output + destroy
header("Content-type: image/png;");
imagepng($im, null, 9, PNG_ALL_FILTERS);
imagedestroy($im);

@Lukasss93
Did it work?

commented

Related #36