首頁  >  文章  >  後端開發  >  png圖片 php 產生文字png圖片的程式碼

png圖片 php 產生文字png圖片的程式碼

WBOY
WBOY原創
2016-07-29 08:44:431434瀏覽

複製程式碼 程式碼如下:



/*
php產生文字pngng圖片,可以使用如下方式呼叫函數:
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'; //預設字型. 相對於腳本存放目錄的相對路徑.
var $msg = "undefined"; / / 預設文字.
var $size = 24;
var $rot = 0; // 旋轉角度.
var $pad = 0; // 填充.
var $transparent = 1; / / 文字透明度.
var $red = 0; // 在黑色背景...
var $grn = 0;
var $blu = 0;
var $bg_red = 255; / / 將文字設為​​白色.
var $bg_grn = 255;
var $bg_blu = 255;
function draw() {
$width = 0;
$height = 0 $offset_x = 0;
$offset_y = 0;
$bounds = array();
$image = "";
// 確定文字高度。 ($this->size, $this->rot, $this->font, "W");
if ($this->rot $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]);
}
// 決定邊框高度.
$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
if ($this->rot $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);
// 畫圖.
ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg );
// 輸出為png格式.
imagePNG($image);
}
}
$text = new textPNG;
if (isset($msg)) $ text->msg = $msg; // 需要顯示的文字
if (isset($font)) $text->font = $font; // 字體
if (isset($size)) $text ->size = $size; // 文字大小
if (isset($rot)) $text->rot = $rot; // 旋轉角度
if (isset($pad)) $text-> pad = $pad; // padding
if (isset($red)) $text->red = $red; // 文字顏色
if (isset($grn)) $text->grn = $ grn; // ..
if (isset($blu)) $text->blu = $blu; // ..
if (isset($bg_red)) $text->bg_red = $bg_red; // 背景顏色.
if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
if (isset($bg_blu)) $text->bg_blu = $bg_blu; / / ..
if (isset($tr)) $text->transparent = $tr; // 透明度(boolean).
$text->draw();
?>

以上就介紹了png圖片 php 產生文字png圖片的程式碼,包含了png圖片方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn