利用php合成图片

php的图片处理其实也挺方便实用,经常会用到例如图片合成或者插入文字等功能,下面就说一下php的image类处理图片是如何使用的吧。

//图片处理函数
    function bornshareqrode()
    {
    //这是背景图片的url
        $bannerimg = ROOT_PATH . '/upload/xcx_saleman_share/red_bag_bg.png';
        //这是需要插入到背景图的图片url
        $qrodeimg = ROOT_PATH . '/upload/xcx_saleman_share/2a2e719a0f23b0f448ee9a7c150f6b81.png';
        //用户信息
        $hb = Db::table('cs_hborders')->where('hb_id', $hb_id)->find();
        $user = Db::table('cs_hbusers')->where('user_id', $hb['user_id'])->find();
        //这是需要插入到背景图的图片url
        $head_img = $user['head_img'];
        //这是要插入到图片的文字
        $tip1 = "长按小程序码可以查看我的名片,";
        $tip2 = "即可领取红包!";
        $text = $user['nickname'] . "发了一个口令红包";
        if ($bannerimg) {
            //生成中间带logo的二维码
            //这是合成后的图片保存的路径
            $upload_dir = "/upload/xcx_saleman_share/";
            if (is_file($bannerimg)) {
            //创建画布
                $logo = imagecreatefromstring(file_get_contents($bannerimg));
                $head_img1 = imagecreatefromstring(file_get_contents($qrodeimg));
                $head_img2 = imagecreatefromstring(file_get_contents($head_img));

                //写入文字
                $msg = imagecolorallocate($logo, 255, 225, 177);
                $black = imagecolorallocate($logo, 255, 225, 177);
                $grey = imagecolorallocate($logo, 255, 225, 177);
                //写的文字用到的字体
                $font = ROOT_PATH . '/statics/font/black_font.ttf';  
                //将$qrodeimg插入到$bannerimg里
                imagecopyresampled($logo, $head_img1, 250, 300, 0, 0, 275, 275, imagesx($head_img1), imagesy($head_img1));
                //将$head_img插入到$bannerimg里
                imagecopyresampled($logo, $head_img2, 400, 30, 0, 0, 100, 100, imagesx($head_img2), imagesy($head_img2));

                //在图片里插入文字($msg,$black,$grey)
                imagettftext($logo, 24, 0, 280, 180, $msg,$font, $text);  
                imagettftext($logo, 30, 0, 170, 280, $black,$font, $tip1);  
                imagettftext($logo, 30, 0, 330, 340, $grey, $font, $tip2);    


                //生成图片
                imagepng($logo, ROOT_PATH . $upload_dir . '97243655qrcode' . '.png');
            //生成图片名字
                $twocode = $upload_dir . '97243655qrcode' . '.png'; //如果存在logo就生成带logo的
            }
            return $twocode;//返回结果图片url
        } else {
            return false;
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42824337/article/details/82356028