Many people don’t understand that PHP can generate non-HTML materials. This is very useful for generating image images. You can generate a simple advertising horizontal image from database or simply generate only a graphic button.
I use TTF font in the following example
I usually name it 'button.php3':
#######################################################
-----button.php3------
<?
Header("Content-type: image/gif");
if(!isset($s)) $s=11;
$size = imagettfbbox($s,0,"fonts/",$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$xpad=9;
$ypad=9;
$im = imagecreate($dx+$xpad,$dy+$ypad);
$blue = ImageColorAllocate($im, 0x2c,0x6D,0xAF);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
ImageRectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$black);
ImageRectangle($im,0,0,$dx+$xpad,$dy+$ypad,$white);
ImageTTFText($im, $s, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, "fonts/", $text);
ImageTTFText($im, $s, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, "fonts/", $text);
ImageGif($im);
ImageDestroy($im);
?>
#######################################################
It is very important that you cannot put any HTML tags in this file. You cannot have blank lines before or after <? and ?> tags. If you see an incomplete image after using this Script, it means you may have accidentally typed characters outside the PHP tag.
The above script can be called in the web page using this syntax: <IMG SRC="button.php3?s=36&text=PHP+is+Cool">
#######################################################
---------
<html>
<head>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gb2312">
<title>New Page 1</title>
</head>
<body>
<IMG SRC="button.php3?s=36&text=PHP+is+Cool">
</body>
</html>
#######################################################
The result will look like this: .
The 's' parameter is to set the font size.
This is when s=18:
Note I:
Font path "/fonts/" can be obtained from the windows/fonts directory Font file Copy to test in the directory fonts of your website As for the performance in Chinese, netizens are still to provide their experience
Note I first drew a black square and then used white displacement to generate a 3D effect. The method of L can be seen in a light background, but you can change the background color to dark to see this effect. The font has also made the same effect to show a three-dimensional feeling.
You must first make sure that your PHP installation has settings support for GD and TTF. You can refer to PHP FAQ. I suggest you copy to /usr/local/lib and gd*.h and go to /usr/local/include and then
'make install' for FreeTTF library.
You can find the font of the Chaihttf in this /~kikita/!
Note:
The following original code improves the above bar? Can be displayed in multiple lines of text:
#######################################################
-------------------------------
<?
Header("Content-type: image/jpeg");
if(!isset($bgred)) $bgred=0;
if(!isset($bggreen)) $bggreen=51;
if(!isset($bgblue)) $bgblue=153;
if(!isset($chred)) $chred=255;
if(!isset($chgreen)) $chgreen=255;
if(!isset($chblue)) $chblue=255;
if(!isset($shadow)) $shadow="yes";
if(!isset($wrappos)) $wrappos=20;
if(!isset($crop)) $crop=2.2;
if(!isset($jpegquality)) $jpegquality=80;
if(!isset($s)) $s=11;
$savetext=$text;
$text=wordwrap($text,$wrappos," ",0);
if (!isset($font)) $fontname="/www/ttfonts/";
else
$fontname="/www/ttfonts/".$font.".ttf";
$size = imagettfbbox($s,0,$fontname,$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$upper=abs($size[5]);
$under=$size[1];
$th=$upper-$under;
$xpad=9;
if (substr_count($text,chr(13))>=1)
{
$mult=(substr_count($text,chr(13)));
$ypad=($mult*$crop*$s)+$s;
}
else $ypad=($crop-2)*$s;
$im = imagecreate($dx+$xpad,$th+$ypad);
$color = ImageColorAllocate($im, $bgred,$bggreen,$bgblue);
$black = ImageColorAllocate($im, 0,0,0);
$fontcolor = ImageColorAllocate($im, $chred,$chgreen,$chblue);
ImageRectangle($im,0,0,$dx+$xpad-1,$th+$ypad-1,$black);
ImageRectangle($im,0,0,$dx+$xpad,$th+$ypad,$white);
if ($shadow=="yes")
ImageTTFText($im, $s, 0, (int)($xpad/2)-2+1, $th+2+(int)($ypad/2)-3, $black, $fontname, $text);
ImageTTFText($im, $s, 0, (int)($xpad/2)-2, $th+2+(int)($ypad/2)-1-3, $fontcolor, $fontname, $text);
Imagejpeg($im,"",$jpegquality);
ImageDestroy($im);
?>
#######################################################
This can be generated by the following form:
#######################################################
------------------------------
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<form method="POST" action="">
<p>Text<input type="text" name="text" size="60"></p>
<p>Size<input type="text" name="s" size="6" value="14"></p>
<p>wrap break position (wrap break position) <input type="text" name="wrapappos" size="3" value="20"></p>
<p>Background color</p>
<p>Red<input type="text" name="bgred" size="6" value="0">
Green<input type="text" name="bggreen" size="8" value="51">
Blue <input type="text" name="bgblue" size="7" value="153"></p>
<p>Font Color</p>
<p>Red <input type="text" name="chred" size="6" value="255">
Green <input type="text" name="chgreen" size="8" value="255">
Blue <input type="text" name="chblue" size="7" value="255"></p>
<p>Font Type <input type="text" name="font" size="20" value="arialbd"></p>
<p>Shadow <input type="radio" value="yes" checked name="shadow">Yes
<input type="radio" name="shadow" value="no">No</p>
<p>Crop size <input type="text" name="crop" size="20" value="2.2"></p>
<p>Jpeg Quality (0-100) <input type="text" name="jpegquality" size="20" value="80"></p>
<p><input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
#######################################################
Or call directly like the previous example:
#######################################################
---------
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<IMG SRC="?s=36&text=PHP+is+Cool">
</body>
</html>
I use TTF font in the following example
I usually name it 'button.php3':
#######################################################
-----button.php3------
<?
Header("Content-type: image/gif");
if(!isset($s)) $s=11;
$size = imagettfbbox($s,0,"fonts/",$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$xpad=9;
$ypad=9;
$im = imagecreate($dx+$xpad,$dy+$ypad);
$blue = ImageColorAllocate($im, 0x2c,0x6D,0xAF);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
ImageRectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$black);
ImageRectangle($im,0,0,$dx+$xpad,$dy+$ypad,$white);
ImageTTFText($im, $s, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, "fonts/", $text);
ImageTTFText($im, $s, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, "fonts/", $text);
ImageGif($im);
ImageDestroy($im);
?>
#######################################################
It is very important that you cannot put any HTML tags in this file. You cannot have blank lines before or after <? and ?> tags. If you see an incomplete image after using this Script, it means you may have accidentally typed characters outside the PHP tag.
The above script can be called in the web page using this syntax: <IMG SRC="button.php3?s=36&text=PHP+is+Cool">
#######################################################
---------
<html>
<head>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gb2312">
<title>New Page 1</title>
</head>
<body>
<IMG SRC="button.php3?s=36&text=PHP+is+Cool">
</body>
</html>
#######################################################
The result will look like this: .
The 's' parameter is to set the font size.
This is when s=18:
Note I:
Font path "/fonts/" can be obtained from the windows/fonts directory Font file Copy to test in the directory fonts of your website As for the performance in Chinese, netizens are still to provide their experience
Note I first drew a black square and then used white displacement to generate a 3D effect. The method of L can be seen in a light background, but you can change the background color to dark to see this effect. The font has also made the same effect to show a three-dimensional feeling.
You must first make sure that your PHP installation has settings support for GD and TTF. You can refer to PHP FAQ. I suggest you copy to /usr/local/lib and gd*.h and go to /usr/local/include and then
'make install' for FreeTTF library.
You can find the font of the Chaihttf in this /~kikita/!
Note:
The following original code improves the above bar? Can be displayed in multiple lines of text:
#######################################################
-------------------------------
<?
Header("Content-type: image/jpeg");
if(!isset($bgred)) $bgred=0;
if(!isset($bggreen)) $bggreen=51;
if(!isset($bgblue)) $bgblue=153;
if(!isset($chred)) $chred=255;
if(!isset($chgreen)) $chgreen=255;
if(!isset($chblue)) $chblue=255;
if(!isset($shadow)) $shadow="yes";
if(!isset($wrappos)) $wrappos=20;
if(!isset($crop)) $crop=2.2;
if(!isset($jpegquality)) $jpegquality=80;
if(!isset($s)) $s=11;
$savetext=$text;
$text=wordwrap($text,$wrappos," ",0);
if (!isset($font)) $fontname="/www/ttfonts/";
else
$fontname="/www/ttfonts/".$font.".ttf";
$size = imagettfbbox($s,0,$fontname,$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$upper=abs($size[5]);
$under=$size[1];
$th=$upper-$under;
$xpad=9;
if (substr_count($text,chr(13))>=1)
{
$mult=(substr_count($text,chr(13)));
$ypad=($mult*$crop*$s)+$s;
}
else $ypad=($crop-2)*$s;
$im = imagecreate($dx+$xpad,$th+$ypad);
$color = ImageColorAllocate($im, $bgred,$bggreen,$bgblue);
$black = ImageColorAllocate($im, 0,0,0);
$fontcolor = ImageColorAllocate($im, $chred,$chgreen,$chblue);
ImageRectangle($im,0,0,$dx+$xpad-1,$th+$ypad-1,$black);
ImageRectangle($im,0,0,$dx+$xpad,$th+$ypad,$white);
if ($shadow=="yes")
ImageTTFText($im, $s, 0, (int)($xpad/2)-2+1, $th+2+(int)($ypad/2)-3, $black, $fontname, $text);
ImageTTFText($im, $s, 0, (int)($xpad/2)-2, $th+2+(int)($ypad/2)-1-3, $fontcolor, $fontname, $text);
Imagejpeg($im,"",$jpegquality);
ImageDestroy($im);
?>
#######################################################
This can be generated by the following form:
#######################################################
------------------------------
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<form method="POST" action="">
<p>Text<input type="text" name="text" size="60"></p>
<p>Size<input type="text" name="s" size="6" value="14"></p>
<p>wrap break position (wrap break position) <input type="text" name="wrapappos" size="3" value="20"></p>
<p>Background color</p>
<p>Red<input type="text" name="bgred" size="6" value="0">
Green<input type="text" name="bggreen" size="8" value="51">
Blue <input type="text" name="bgblue" size="7" value="153"></p>
<p>Font Color</p>
<p>Red <input type="text" name="chred" size="6" value="255">
Green <input type="text" name="chgreen" size="8" value="255">
Blue <input type="text" name="chblue" size="7" value="255"></p>
<p>Font Type <input type="text" name="font" size="20" value="arialbd"></p>
<p>Shadow <input type="radio" value="yes" checked name="shadow">Yes
<input type="radio" name="shadow" value="no">No</p>
<p>Crop size <input type="text" name="crop" size="20" value="2.2"></p>
<p>Jpeg Quality (0-100) <input type="text" name="jpegquality" size="20" value="80"></p>
<p><input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
#######################################################
Or call directly like the previous example:
#######################################################
---------
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<IMG SRC="?s=36&text=PHP+is+Cool">
</body>
</html>