<?php
class textPNG 
{
    private $font; // 默认字体. 相对于脚本存放目录的相对路径.
    private $msg; // 默认文字.
    private $width = ""; // 默认宽度.
    private $size;
    private $rot = 0; // 旋转角度.
    private $pad = 10; // 填充.
    private $transparent = true; // 文字透明度.
    private $red = 255; // 在黑色背景中...
    private $grn = 255;
    private $blu = 255;
    private $bg_red = 255; // 将文字设置为白色.
    private $bg_grn = 255;
    private $bg_blu = 255;

    public function __construct($paras = array()) 
    {
        empty($paras) && $paras = $_GET;
        isset($paras['text']) && $this->msg = $paras['text'];
        isset($paras['font']) && $this->font = $paras['font'];
        isset($paras['fontsize']) && $this->size = $paras['fontsize'];
        isset($paras['rot']) && $this->rot = $paras['rot'];
        isset($paras['pad']) && $this->pad = $paras['pad'];
        isset($paras['transparent']) && $this->transparent = $paras['transparent'];
        if (isset($paras['color']) && !empty($paras['color'])) {
            $this_color_hex = $this->HexToRGB($paras['color']);
            $this->red = $this_color_hex['red']; // 文字颜色
            $this->grn = $this_color_hex['green'];
            $this->blu = $this_color_hex['blue'];
        } else {
            isset($paras['red']) && $this->red = $paras['red'];
            isset($paras['grn']) && $this->grn = $paras['grn'];
            isset($paras['blu']) && $this->blu = $paras['blu'];
        }
        if (isset($paras['bg_color']) && !empty($paras['bg_color'])) {
            $bg_color_hex = $this->HexToRGB($paras['bg_color']);
            $this->bg_red = $bg_color_hex['red']; // 背景颜色.
            $this->bg_grn = $bg_color_hex['green'];
            $this->bg_blu = $bg_color_hex['blue'];
        } else {
            isset($paras['bg_red']) && $this->bg_red = $paras['bg_red'];
            isset($paras['bg_grn']) && $this->bg_grn = $paras['bg_grn'];
            isset($paras['bg_blu']) && $this->bg_blu = $paras['bg_blu'];
        }
        if (isset($paras['width']) && !empty($paras['width'])) {
            $this->width = $paras['width'];
            $this->msg = $this->autowrap($this->size, $this->font, $this->msg, $paras['width']);
        } else {
            $this->msg = $this->autowrap($this->size, $this->font, $this->msg);
        }
    }

    public function draw( $savepic=null ) {
        $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 < 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]);
        }
        // 确定边框高度.
        $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;
        }
        if ($this->width) {
            $image = imagecreate($this->width, $height + ($this->pad * 2) + 1);
        } else {
            $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格式.
        Header("Content-type: image/png");

        if( $savepic )
            imagePNG($image, $savepic);
        else
        {
            // 输出为png格式.
            Header("Content-type: image/png");
            imagePNG($image);
        }
    }

    // 自动调整宽度(字体大小, 字体名称, 字符串, 预设宽度, 角度)
    public function autowrap($fontsize, $fontface, $string, $width = "", $angle = 0) {
        // 将字符串拆分成一个个单字 保存到数组 letter 中
        for ($i = 0; $i < mb_strlen($string); $i++) {
            $letter[] = mb_substr($string, $i, 1, 'utf-8');
        }
        $letter = array_filter($letter);
        $content = "";
        for ($i = 0; $i < count($letter); $i++) {
            // 换行处理
            if ($letter[$i] == "/" && $letter[$i + 1] == "n") {
                $content .= "\n";
                unset($letter[$i]);
                unset($letter[$i + 1]);
                continue;
            }
            if ($width) {
                $teststr = $content . " " . $letter[$i];
                $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
                if (($testbox[2] > $width) && ($content !== "")) {
                    $content .= "\n";
                }
            }
            $content .= $letter[$i];
        }
        return $content;
    }

 // Hex颜色转RGB颜色
    public function HexToRGB($colour) {
        if ($colour[0] == '#') {
            $colour = substr($colour, 1);
        }
        if (strlen($colour) == 6) {
            list($r, $g, $b) = array(
                $colour[0] . $colour[1],
                $colour[2] . $colour[3],
                $colour[4] . $colour[5],
            );
        } elseif (strlen($colour) == 3) {
            list($r, $g, $b) = array(
                $colour[0] . $colour[0],
                $colour[1] . $colour[1],
                $colour[2] . $colour[2],
            );
        } else {
            return false;
        }
        $r = hexdec($r);
        $g = hexdec($g);
        $b = hexdec($b);
        return array(
            'red' => $r,
            'green' => $g,
            'blue' => $b,
        );
    }
}

//配置文字
$paras = [
   "font" => getcwd() . '/msyh.ttf',//字体位置
   "text" => "商旅大酒店",//字符串,14个字最佳
   "fontsize" => 80, //字体大小
   "transparent" => true,//图片透明
   "color" => "#FFFFFF",//字体颜色
   "bg_color" => "#0E6EB8",//背景颜色
];
$png = new textPNG( $paras );

$savepng = "./tt.png";
$png->draw( $savepng );
?>

点赞(0)
发表
评论
返回
顶部