Using PHP to generate dynamic image _php Tutorials

Source: Internet
Author: User
Many people do not understand that PHP can produce non-HTML data. This is very useful for generating image images. You can create a simple ad map from database or simply create a graphical button.

I use the 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/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. Nor can there be blank lines in Before or after the tag. If you use this script to see an incomplete image, it means that you may have mistakenly hit a character outside of the PHP tag.

The script above can be called from this syntax in the Web page:

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




<title>New Page 1</title>








#######################################################

test.php results will look like this:.

The ' s ' parameter is the set font size.

This is when s=18:

Note I:
Font path "/fonts/times. TTF "can be TIMS by the windows/fonts directory. TTF font file Copy to your Site Directory fonts can be tested to the performance of the Chinese has yet to be provided by the user experience

Note that I drew a black block first and then the 3D effect with the white shift. Also Zou L method is shown on a light background but you can change the background color to dark to see the effect. The font also does the same effect as the three-dimensional.

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

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

Note:

The following original code improved the above? 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>







#######################################################

Or just like the previous example, call:

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



<title>New Page 1</title>







http://www.bkjia.com/PHPjc/315865.html www.bkjia.com true http://www.bkjia.com/PHPjc/315865.html techarticle Many people do not understand that PHP can produce non-HTML data. This is very useful for generating image images. You can generate a simple ad map from database or simply create a graphical button ...

  • 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.