Home  >  Article  >  Backend Development  >  png picture php code to generate text png picture

png picture php code to generate text png picture

WBOY
WBOYOriginal
2016-07-29 08:44:431421browse

Copy the code The code is as follows:


/*
php generates text png images, you can use the following method to call the function:
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'; //Default font. Relative path to the directory where the script is stored.
var $msg = "undefined"; // Default text.
var $size = 24;
var $rot = 0; // Rotation angle.
var $pad = 0; / / Padding.
var $transparent = 1; // Text transparency.
var $red = 0; // On a 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.
ImageTTFText($image, $ this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
// Output in png format.
imagePNG($image);
}
}
$text = new textPNG;
if (isset($msg)) $text->msg = $msg; // 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();
?>

The above introduces the code for generating text png images from png images in php, including the content of png images. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn