PHP 圖片、文字合成

vilson發表於2019-02-16
//查詢使用者資訊
$client = $this->where(array(`id` => $client_id))->find();
        $client_avatar = $client[`avatar`];
        $mine_code = $client[`mine_code`];
        $have_code = is_file(SYS_ROOT_PATH . "uploadfiles/qrcode/{$mine_code}.png");
        //已經合成的不再重複生成
        if (!$have_code) {
        //生成原始二維碼圖
            $qrcode = sys_get_qrcode(SYS_ROOT . "index.php/Webservice/V100/register/mine_code/{$mine_code}", "uploadfiles/qrcode/{$mine_code}.png");//網路路徑
            //原始二維碼圖的絕對路徑
            $codeRealPath = SYS_ROOT_PATH . "uploadfiles/qrcode/{$mine_code}.png";
            $logo = SYS_ROOT_PATH . "uploadfiles/qrcode-min.png";//準備好的模板圖片
            if ($qrcode) {
                $QR = $codeRealPath;//已經生成的原始二維碼圖
                $QR = imagecreatefromstring(file_get_contents($QR));
                $logo = imagecreatefromstring(file_get_contents($logo));
                $QR_width = imagesx($QR);//二維碼圖片寬度
                $QR_height = imagesy($QR);//二維碼圖片高度

                //圖片合成
                imagecopyresampled($logo, $QR, 320, 1948, 0, 0, 445, 445, $QR_width, $QR_height);
                //儲存到codeRealPath(路徑)
                imagepng($logo, $codeRealPath);

                $path = $codeRealPath;

                if ($client[`nickname`]) {
                    $img = imagecreatefromstring(file_get_contents($path));
                    //字型型別
                    $font = SYS_ROOT_PATH . "uploadfiles/font/wenrui.otf";
                    //顯示的文字
                    $text = `我是` . $client[`nickname`];           
                    //設定字型顏色
                    $black = imagecolorallocate($img, 255, 255, 255);
                    imagettftext($img, 26, 0, 470, 1890, $black, $font, $text);
                    imagepng($img, $path);
                }

                //合成頭像
                if ($client_avatar && $client_avatar != ``) {
                    $member_avatar = imagecreatefromstring(file_get_contents($client_avatar));
                } else {
                    $member_avatar = imagecreatefromstring(file_get_contents(SYS_ROOT_PATH . "uploadfiles/default_avatar.png"));
                }

                $img = imagecreatefromstring(file_get_contents($path));
                imagecopyresampled($img, $member_avatar, 500, 2125, 0, 0, 85, 85, imagesx($member_avatar), imagesx($member_avatar));
                imagepng($img, $path);
            }
        }

相關文章