Font size calculating
vendelieu opened this issue · comments
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);
@CFlanc your snippet doesn't work :(
My code: http://sharex.lucapatera.it/uploads/phpstorm64_2017-09-11_00-03-13.png
Result: http://sharex.lucapatera.it/uploads/firefox_2017-09-10_23-57-01.png
The goal: http://sharex.lucapatera.it/uploads/ApplicationFrameHost_2017-09-11_00-08-50.png
@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
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?