您好,欢迎访问一九零五行业门户网

php生成文字png图片的函数

/*php生成文字png图片,调用方式: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'; //默认字体. 相对于脚本存放目录的相对路径. 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 = ; // 确定文字高度. $bounds = imagettfbbox($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; // paddingif (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();?>
复制代码
更多gd库的应用实例,请参考:php中开启gd2扩展的方法介绍php验证码(gd库生成验证码)的例子php gd库中文乱码的解决方法php gd库生成验证码的实例php gd库上传图片并创建缩略图的代码php使用gd库生成bmp格式的图片(imagebmp)
其它类似信息

推荐信息