首页 > php图像处理 > PHP实现在图片中添加中文文字

PHP实现在图片中添加中文文字

<?php
/*
注重:需要gd库支持,需要iconv支持(php5已经包含不用加载) 
在图片中添加中文文字 
*/
/*
param $image 图象资源 
param size 字体大小 
param angle 字体输出角度 
param showX 输出位置x坐标 
param showY 输出位置y坐标 
param font 字体文件位置 
param content 要在图片里显示的内容 
*/
class showChinaText {
    var $text = '你好';
    var $font = 'fs.ttf';
    var $angle = 0;
    var $size = 50;
    var $showX = 100;
    var $showY = 100;
    function showChinaText($showText = '') {
        $this->text = !isset($showText) ? $showText : $this->text;
        //exit();
        $this->show();
    }
    function createText($instring) {
        $outstring = "";
        $max = strlen($instring);
        for ($i = 0; $i < $max; $i) {
            $h = ord($instring[$i]);
            if ($h >= 160 && $i < $max - 1) {
                $outstring.= "&#" . base_convert(bin2hex(iconv("gb2312", "ucs-2", substr($instring, $i, 2))) , 16, 10) . ";";
                $i;
            } else {
                $outstring.= $instring[$i];
            }
        }
        return $outstring;
    }
    function createJpeg() {
    }
    function show() {
        //输出头内容
        Header("Content-type: image/png");
        //建立图象
        $image = imagecreate(400, 300);
        //定义颜色
        $red = ImageColorAllocate($image, 255, 0, 0);
        $white = ImageColorAllocate($image, 255, 255, 255);
        $black = ImageColorAllocate($image, 0, 0, 0);
        //填充颜色
        ImageFilledRectangle($image, 0, 0, 200, 200, $red);
        //显示文字
        $txt = $this->createText($this->text);
        //写入文字
        imagettftext($image, $this->size, $this->angle, $this->showX, $this->showY, $white, $this->font, $txt);
        //ImageString($image,5,50,10,$txt,$white);
        //显示图形
        imagejpeg($image);
        ImageDestroy($image);
    } 
    
}
//本类,并没有经过很好的考虑,只是简单的进行了封装,以后有机会,可能跟原来的图片类整合
?>
<?php
//使用示例
$s = new showChinaText();
?>


本文地址:http://www.phprm.com/tuxiang/fs4515.html

转载随意,但请附上文章地址:-)

标签:php添加文字 php图片中文文字

发表留言