PNG image PHP generate text PNG image code

Source: Internet
Author: User
Copy CodeThe code is as follows:


/*
PHP generates a text PNG image, which can be called using the following methods:
Http://www.yourdomian.com/text_png.php3?msg=helloworld+class&rot=15&size=48&f/ARIAL. TTF
*/
Header ("Content-type:image/png");
Class Textpng {
var $font = ' Fonts/times. TTF '; The default font. Relative path to the directory relative to the script.
var $msg = "undefined"; The 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; Sets 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 = "";
Determines 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]);
}
Determines the height of the border.
$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);
Drawing.
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; Text that needs 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 ();
?>

The above describes the PNG image PHP generated text PNG image code, including the content of the PNG image, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.