stil / gd-text

PHP class making it easy to type text on pictures. Supports multi-lined text, horizontal and vertical alignment.
400 stars 131 forks source link

Font size calculating #26

Open vendelieu opened 7 years ago

vendelieu commented 7 years ago

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.

CFlanc commented 7 years ago

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 commented 6 years ago

@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

CFlanc commented 6 years ago

@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

Lukasss93 commented 6 years ago

@CFlanc

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

Nope :(

CFlanc commented 6 years ago

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

Lukasss93 commented 6 years ago

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);

CFlanc commented 6 years ago

@Lukasss93 Did it work?

stil commented 6 years ago

Related #36