Php code for generating text png Images

Source: Internet
Author: User

Copy codeThe Code is as follows:
<?
/*
Php generates a text png image. You can call the function as follows:
Http://www.yourdomian.com/text_png.php3? Msg = helloworld + class & rot = 15 & size = 48 & font = fonts/ARIAL. TTF
*/
Header ("Content-type: image/png ");
Class textPNG {
Var $ font = 'fonts/TIMES. ttf'; // default font. Relative Path to the script storage directory.
Var $ msg = "undefined"; // default text.
Var $ size = 24;
Var $ rot = 0; // rotation angle.
Var $ pad = 0; // fill.
Var $ transparent = 1; // text transparency.
Var $ red = 0; // In the black background...
Var $ grn = 0;
Var $ blu = 0;
Var $ bg_red = 255; // set the text to white.
Var $ bg_grn = 255;
Var $ bg_blu = 255;
Function draw (){
$ Width = 0;
$ Height = 0;
$ Offset_x = 0;
$ Offset_y = 0;
$ Bounds = array ();
$ Image = "";
// Determine the text height.
$ Bounds = ImageTTFBBox ($ this-> size, $ this-> rot, $ this-> font, "W ");
If ($ this-> rot <0 ){
$ Font_height = abs ($ bounds [7]-$ bounds [1]);
} Else if ($ this-> rot> 0 ){
$ Font_height = abs ($ bounds [1]-$ bounds [7]);
} Else {
$ Font_height = abs ($ bounds [7]-$ bounds [1]);
}
// Determine the border height.
$ Bounds = ImageTTFBBox ($ this-> size, $ this-> rot, $ this-> font, $ this-> msg );
If ($ this-> rot <0 ){
$ Width = abs ($ bounds [4]-$ bounds [0]);
$ Height = abs ($ bounds [3]-$ bounds [7]);
$ Offset_y = $ font_height;
$ Offset_x = 0;
} Else if ($ this-> rot> 0 ){
$ Width = abs ($ bounds [2]-$ bounds [6]);
$ Height = abs ($ bounds [1]-$ bounds [5]);
$ Offset_y = abs ($ bounds [7]-$ bounds [5]) + $ font_height;
$ Offset_x = abs ($ bounds [0]-$ bounds [6]);
} Else {
$ Width = abs ($ bounds [4]-$ bounds [6]);
$ Height = abs ($ bounds [7]-$ bounds [1]);
$ Offset_y = $ font_height ;;
$ Offset_x = 0;
}
$ Image = imagecreate ($ width + ($ this-> pad * 2) + 1, $ height + ($ this-> pad * 2) + 1 );
$ Background = ImageColorAllocate ($ image, $ this-> bg_red, $ this-> bg_grn, $ this-> bg_blu );
$ Foreground = ImageColorAllocate ($ image, $ this-> red, $ this-> grn, $ this-> blu );
If ($ this-> transparent) ImageColorTransparent ($ image, $ background );
ImageInterlace ($ image, false );
// Draw a picture.
ImageTTFText ($ image, $ this-> size, $ this-> rot, $ offset_x + $ this-> pad, $ offset_y + $ this-> pad, $ foreground, $ this-> font, $ this-> msg );
// The output is in png format.
ImagePNG ($ image );
}
}
$ Text = new textPNG;
If (isset ($ msg) $ text-> msg = $ msg; // The text to be displayed
If (isset ($ font) $ text-> font = $ font; // font
If (isset ($ size) $ text-> size = $ size; // text size
If (isset ($ rot) $ text-> rot = $ rot; // Rotation Angle
If (isset ($ pad) $ text-> pad = $ pad; // padding
If (isset ($ red) $ text-> red = $ red; // text color
If (isset ($ grn) $ text-> grn = $ grn ;//..
If (isset ($ blu) $ text-> blu = $ blu ;//..
If (isset ($ bg_red) $ text-> bg_red = $ bg_red; // background color.
If (isset ($ bg_grn) $ text-> bg_grn = $ bg_grn ;//..
If (isset ($ bg_blu) $ text-> bg_blu = $ bg_blu ;//..
If (isset ($ tr) $ text-> transparent = $ tr; // transparency (boolean ).
$ Text-> draw ();
?>

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.