Using PHP to generate dynamic image _php Foundation

Source: Internet
Author: User
Tags abs chr file copy install php html tags
Many people do not understand that PHP can produce non-HTML data. This is useful for generating image images. You can generate a simple ad map from the database or simply create a graphical button.

I use the TTF font in the following example
I usually name ' button.php3 ':

#######################################################
-----BUTTON.PHP3------
?
Header ("Content-type:image/gif");
if (!isset ($s)) $s = 11;
$size = Imagettfbbox ($s, 0, "fonts/times." TTF ", $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/times." TTF ", $text);
Imagettftext ($im, $s, 0, (int) ($xpad/2), $dy + (int) ($ypad/2)-1, $white, "Fonts/times." TTF ", $text);
Imagegif ($im);
Imagedestroy ($im);
?>
#######################################################
It's important that you can't put any HTML tags in this file. There is no blank line before or after the?> tag. If you use this script to see an incomplete image, you may have mistakenly typed characters outside of the PHP tag.

The above script can be called out of the Web page by this syntax:

#######################################################
----test.php-----

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>new Page 1</title>

<body>



</body>
#######################################################

The test.php result would be like this:.

The ' s ' parameter is the set font size.

This is when s=18:

Note I:
Font path "/fonts/times. TTF "can be obtained from the Windows/fonts catalogue TIMS. TTF font file Copy to the directory of your site fonts can test to the performance of the Chinese users have yet to provide experience

Notice that I drew a black square and then used the white displacement to produce a 3D effect. Also Zou L method in light background but you can change the background color to dark to see the effect. The font has also done the same effect of three-dimensional.

You have to make sure that you install PHP with a set of support GD and TTF. Refer to the PHP FAQ. I suggest you copy libgd.a to/usr/local/lib and gd*.h files to/usr/local/include.
' Make install ' for Freettf Library.

Can find in this http://rover.wiesbaden.netsurf.de/~kikita/chai Httf font Oh!

Note:

The following source code improves the Benten? can be multiple lines of text display:
#######################################################
--------------button.php-----------------
?

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/arialbd.ttf";
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) >=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:
#######################################################
----------test.php--------------------

<title>new Page 1</title>

<body>

<form method= "POST" action= "button.php" >
<p> text <input type= "text" name= "text" size= "a" ></p>
<p> size <input type= "text" name= "s" size= "6" value= "a" ></p>
<p> location of the punctuation (wrap break position) <input type= "text" name= "Wrappos" size= "3" value= "></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= "Wuyi" >
Blue <input type= "text" name= "Bgblue" size= "7" value= "153" ></p>
<p> character 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 <input type= "text" name= "Font" size= "value=" "ARIALBD" ></p>
<p> Shadow <input type= "Radio" value= "yes" checked name= "Shadow" > is
<input type= "Radio" name= "Shadow" value= "no" > No </p>
<p>crop size <input type= "text" name= "crop" size= "value=" "2.2" ></p>
<p>jpeg Quality (0-100) <input type= "text" name= "jpegquality" size= "value=" ></p>
<p><input type= "Submit" value= "Submit" Name= "B1" >
<input type= "reset" value= "reset" name= "B2" ></p>
</form>
</body>
#######################################################

Or just like the example above:

#######################################################
----test.php-----

<title>new Page 1</title>

<body>



</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.