: This article mainly introduces how to use PHP to generate dynamic images. For more information about PHP tutorials, see. Many people do not know that php can generate non-HTML data. this is very useful for generating image graphs. it can generate a simple ad horizontal chart from the database or simply generate only one graphic button.
I use TTF 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 );
$ White = ImageColorAllocate ($ im, 255,255,255 );
ImageRectangle ($ im, $ 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 is very important that you cannot put any HTML tags in this file. there cannot be blank lines in Before or after the tag. if you see an incomplete image after using this Script, it means that you may mistakenly typed characters outside the PHP tag.
The above scripts can be called in the webpage by syntax:
######################################## ###############
---- Test. php -----
New Page 1
######################################## ###############
The result of test. php is as follows :.
The 'S' parameter is used to set the font size.
This is when s = 18:
Note I:
Font path "/fonts/TIMES. TTF can be obtained from the windows/fonts directory. copy the TTF file to the fonts directory of your website to test the Chinese performance.
Note that I first drew a black block area and then used a white shift to produce a 3D effect. you can also see it in a light background by using the L method, but you can change the background color to a dark color to see the effect. the font also has the same effect to show the stereoscopic effect.
Make sure that you have configured GD and TTF support when installing PHP. refer to the php faq. I suggest you copy libgd. a to/usr/local/lib and gd *. h file to/usr/local/include and then
'Make install' for FreeTTF library.
Can be on this http://rover.wiesbaden.netsurf.de /~ Kikita/find the httf font!
Note:
The following original code improves the above signature? Multi-line 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 ($ pipeline quality) $ pipeline quality = 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 (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 );
$ Fontcolor = ImageColorAllocate ($ im, $ chred, $ chgreen, $ chblue );
ImageRectangle ($ im, $ 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, "", $ quality );
ImageDestroy ($ im );
?>
######################################## ###############
This can be produced using the following form:
######################################## ###############
---------- Test. php --------------------
New Page 1
######################################## ###############
Or call directly as in the previous example:
######################################## ###############
---- Test. php -----
New Page 1
The above introduces how to use PHP to generate dynamic image images, including dynamic image content, and hope to help those who are interested in PHP tutorials.