美文网首页
PHP 图片合成

PHP 图片合成

作者: wyc0859 | 来源:发表于2019-04-15 14:44 被阅读0次

    PHP 图片合成常涉及到以下几个问题

    1、多张图合成
    2、png透明问题
    3、多行文字
    4、文字居中

    常用到的php函数

    imagecreatefrompng 创建一块画布并载入一张png图片
    imagecreatefromjpeg 创建一块画布并载入一张jpg图片
    imagesx 获取图片宽
    imagesy 获取图片高
    imagecreatetruecolor 创建画布
    imagecopy 拷贝图片,用于png透明
    imagefontwidth 获取文字宽度
    ImageColorAllocate 文字颜色
    imagettftext 文字
    Imagepng 显示或保存
    ImageDestroy 销毁图像

    123.png
    <?php
    
    class hb
    {
    
        function make($arr)
        {
            $file_name = './hb/a' . uniqid() . '.png';
    
    // picture
            $file_bg = './data/img/bg.png';
            $file_head = './data/img/head.png';
            $file_code = './data/img/wx.png';
            $file_bgcard = './data/img/bg_card.png';
    
            $text_title = $arr['title'];
            $text = $arr['info'];
            $tag_name = $arr['name'];
            $tag_content = $arr['content'];
            $tag_a = $arr['shui'];
            $tag_b = $arr['gj'];
            $tag_c = $arr['xue'];
    
    // font type
            $font_msyh = './data/fonts/msyh.ttc';
            $font_simhei = './data/fonts/simhei.ttf';
    
            $img_bg = imagecreatefrompng($file_bg);//背景,如果是jpg就用imagecreatefromjpeg
            $img_star = imagecreatefrompng($file_head);//卡片
            $img_bgcard = imagecreatefrompng($file_bgcard);//背景卡
            $img_code = imagecreatefrompng($file_code);//二维码
    
    
            $width = imagesx($img_bg);
            $height = imagesy($img_bg);
    
    //$im = $img_bg;
            $im = imagecreatetruecolor($width, $height);  //创建一张与背景图同样大小的真彩色图像
            imagecopy($im, $img_bg, 0, 0, 0, 0, $width, $height);
    
    
    //加载head图片
            $width_code = imagesx($img_star);
            $height_code = imagesy($img_star);
            imagecopymerge($im, $img_star, 110, 20, 0, 0, $width_code, $height_code, 100);
    
    //加载bg_card
            $width_code = imagesx($img_bgcard);
            $height_code = imagesy($img_bgcard);
    //imagecopymerge($im, $img_bgcard, 0, 0, 0, 0, $width_code, $height_code, 100);
    //png透明用法
            imagecopy($im, $img_bgcard, 0, 0, 0, 0, $width_code, $height_code);
    
    //加载二维码图片
            $width_code = 120;
            $height_code = 120;
            imagecopymerge($im, $img_code, $width - $width_code - 15, $height - $height_code - 15, 0, 0, $width_code, $height_code, 50);
    
    
    //字体颜色
            $color_grey = ImageColorAllocate($im, 139, 139, 139);
            $color_white = ImageColorAllocate($im, 255, 255, 255);
            $color_red = ImageColorAllocate($im, 21, 124, 175);
            $color_black = ImageColorAllocate($im, 000, 000, 000);
    
            $fontSize = 16; //18号字体
            $fontWidth = imagefontwidth($fontSize);//获取文字宽度
            $textWidth = $fontWidth * mb_strlen($tag_name);
            if (mb_strlen($tag_name) < 6) {
                $top = 293;
                $left = 395;
            } else {
                $top = 300;
                $left = 325;
            }
            $x = ceil(($left - $textWidth) / 2); //计算文字的水平位置
    
            //卡片名称
            imagettftext($im, $fontSize, 6, $x, $top, $color_black, $font_msyh, $tag_name);
    
            //标题
            imagettftext($im, 16, 0, 35, 590, $color_red, $font_msyh, $text_title);
    
    
            $left_a = mb_strlen($tag_a) > 1 ? 35 : 53;
            $left_b = mb_strlen($tag_b) > 1 ? 35 : 53;
            $left_c = mb_strlen($tag_c) > 1 ? 310 : 330;
    
            imagettftext($im, 58, 0, $left_a, 95, $color_white, $font_msyh, $tag_a);
            imagettftext($im, 58, 0, $left_b, 510, $color_white, $font_msyh, $tag_b);
            imagettftext($im, 58, 0, $left_c, 510, $color_white, $font_msyh, $tag_c);
    
            //卡片描述--多行文本渲染
            $h = 390;
            $font_row = 11;    //一行显示多少字
            $fontSize = 14; //18号字体
            foreach (str_split($tag_content, $font_row * 3) as $key => $val) {
                $tmp_h = $h + $key * 20;
                imagettftext($im, 14, 0, 120, $tmp_h, $color_black, $font_simhei, $val);
            }
    
    
            //广告--多行文本渲染
            $font_row = 12;    //一行显示多少字
            $h = 620;
            foreach (str_split($text, $font_row * 3) as $key => $val) {
                $tmp_h = $h + $key * 20;
                imagettftext($im, 12, 0, 30, $tmp_h, $color_grey, $font_simhei, $val);
            }
    
    
        //最后处理 输出、销毁
            //Imagepng($im, $file_name);//保存到服务器
        header('Content-Type: image/png');
        Imagepng($im);      //浏览器直接显示
            ImageDestroy($img_code);
            ImageDestroy($img_star);
            ImageDestroy($img_bg);
            ImageDestroy($img_bgcard);
            ImageDestroy($im);
    
            return $file_name;
        }
    }
    $arr=[];
    $arr['name'] = '阿里山';
    $arr['content'] = "冲锋、突袭";
    $arr['shui']='20';
    $arr['xue']='19';
    $arr['gj']='33';
    $arr['title'] = '你想要的标题';
    $arr['info'] = '这里就是传说中的描述吧,看这里看这里...';
    echo (new hb)->make($arr);
    
    
    

    相关文章

      网友评论

          本文标题:PHP 图片合成

          本文链接:https://www.haomeiwen.com/subject/aouuwqtx.html