php 图片合成

php 图片合成
public function shareimg()
    {
        $id = $_POST['uid'];
//二维码图片
        $qrodeimg = $this->getWxappQrCode($id);
//背景图片
        $bannerimg = ROOT_PATH . '/public/wxcode/back_one.png';
        //这是需要插入到背景图的图片url
//        $qrodeimg = ROOT_PATH . '/public/wxcode/qrcode1.png';
        //用户信息
        //这是需要插入到背景图的图片url
//        $head_img = $user['head_img'];
        //这是要插入到图片的文字
//        $tip1 = "长按小程序码可以查看我的名片,";
//        $tip2 = "即可领取红包!";
//        $text = $user['nickname'] . "发了一个口令红包";
        if ($bannerimg) {
            //生成中间带logo的二维码
            //这是合成后的图片保存的路径
            $upload_dir = "/public/wxcode/";
            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, 80, 80, 0, 0, 200, 200, 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);


                $rans = rand(100,900).time().'code'.'.png';
                //生成图片
                imagepng($logo, ROOT_PATH . $upload_dir . $rans);
                //生成图片名字
//                $twocode = $upload_dir . '97243655qrcode' . '.png'; //如果存在logo就生成带logo的
                $twocode = $rans; //如果存在logo就生成带logo的
            }
            //返回结果图片url
            throw new Success(['data' => 'http://'.$_SERVER['HTTP_HOST'].'/wxcode/'.$twocode]);
        } else {
            return false;
        }
    }

posted @ 2020-07-18 17:12  办公魔盒  阅读(206)  评论(0)    收藏  举报