Check we have the appropriate variable data
Variables is button-text and color
$button _text=$_request[' Button_text '];
$color =$_request[' color '];
if ((Empty ($button _text) | | Empty ($color)) | | (! ($color = = ' Red '
|| $color = = ' Blue ' | | $color = = ' green ')))
{
Echo ' Could not create image-form not filled out correctly ';
Exit
}
Create an image of the background and check size
$im =imagecreatefrompng ($color. '-button.png ');
if (! $im)
{
Echo ' Could not create image ';
Exit
}
$width _image=imagesx ($im);
$height _image=imagesy ($im);
Our images need a pixel margin in from the edage of the image
$width _image_wo_margins= $width _image-(2*18);
$height _image_wo_margins= $height-(2*18);
Work we if the font size would fit and make itsmaller until it does
Start our with the biggest size, that would reasonably fit on our buttons
$font _size=3;
You need to tell GD2 where fonts reside
Putenv (' gdfontfath=c:\windows\fonts ');
$fontname = ' Arial ';
Do
{
$font _size--;
Find out the size of the text at that font size
$bbox =imagettfbbox ($font _size,0, $fontname, $button _text);
$right _text= $bbox [2];//right co-ordinate
$left _text= $bbox [0];
$width _text= $right _text-$left _text; How wide is it?
$height _text=abs ($bbox [7]-$bbox [1]);//how tall is it?
}
while ($font _size>8&& ($height _text> $height _image_wo_margins| |
$width _text> $width _image_wo_margins)
);
if ($height _text> $height _image_wo_margins| |
$width _text> $width _image_wo_margins)
{
No readable font size would fit on button
Echo ' Text given won't fit on button.
';
}
Else
{
We have found a font size, that would fit
Now work out where to put it
$text _x= $width _image/2.0-$width _text/2/0;
$text _y= $height _image/2.0-$height _text/2.0;
if ($left _text<0)
$text _x+=abs ($left _text);
$above _line_text=abs ($bbox [7]);
$text _y+= $above _line_text;
$text _y-=2;
$white =imagecolorallocate ($im, 255,255,255);
Imagettftext ($im, $font _size,0, $text _x, $text _y, $white, $fontname,
$button _text);
Header (' content-type:image/png ');
Imagepng ($im);
}
Imagedestroy ($im);
?>
Warning:imagettfbbox () [Function.imagettfbbox]: Invalid font filename in E:\AppServ\www\make_button.php on line 38
Reply to discussion (solution)
There is no font to change a font or to copy the font file to the corresponding directory
There are Arial font files under the C-drive path.
I put putenv (' gdfontfath=c:\windows\fonts ');
Change to Putenv (' gdfontfath=c:\windows\fonts ');
Error: Warning:imagettfbbox () [Function.imagettfbbox]: Could not find/open font ' in ' E:\AppServ\www\make_button.php on line 38
But there are Arial font files in the folder
Please help the hero! ~~~
Excuse me
Error has been Warning:imagettfbbox () [Function.imagettfbbox]: Invalid font filename in E:\AppServ\www\make_button.php on line 38
is a problem with font variables. Put
Putenv (' gdfontfath=c:\windows\fonts ');
$fontname = ' Arial ';
Change directly into
$fontname = ' C:/windows/fonts/arial.ttf ' on it, not sensitive to case.
I also encountered this situation, but also according to the building of # #, or in that case, can't find the file name
The font path of the backslash is how to write, is \ or/, I tried several kinds, no, and the font exists in the system
Warning:imagettfbbox () [Function.imagettfbbox]: Invalid font filename in D:\xampp\htdocs\test\class\pImage.class.php On line 211
Warning:imagettftext () [Function.imagettftext]: Invalid font filename in D:\xampp\htdocs\test\class\pDraw.class.php On line 1023
My question appears in the called Class, what do you think?
4 Floor method solved, thank you.
4 Floor positive solution, 3ku!
The 4 floor method works!