PHP生成小程序二維碼合成圖片生成文字


這部分代碼是寫在項目上的代碼,THINKPHP3.1如果遷移到其他的地方應該要稍稍改動一下以適合自己的項目

 

    function get_bbox($text,$fsize,$ffile){
        return imagettfbbox($fsize, 0, $ffile, $text);
    }


    function text_height ($text ,$fsize,$ffile) {
        $box = $this->get_bbox($text,$fsize,$ffile);
        $height = $box[3] - $box[5];
        return $height;
    }

    //上傳到aliyun
    function uploadtooss($bucket, $localfilepath, $objname) {
        $uploadoss = new Aliyunoss($bucket);
        return $uploadoss->upload_by_file($objname, $localfilepath);
    }


    function getalluser(){

        $data=M('kuaidati_user')->order('id desc')->select();
        dump($data);
    }

    function ifexsitobj($bucket, $objname) {
        $uploadoss = new Aliyunoss($bucket);
        return $uploadoss->is_object_exist($objname);
    }



    function autowrap($fontsize, $angle, $fontface, $string, $width) {
        // 這幾個變量分別是 字體大小, 角度, 字體名稱, 字符串, 預設寬度
        $content = "";

        // 將字符串拆分成一個個單字 保存到數組 letter 中
        for ($i=0;$i<mb_strlen($string,'utf8');$i++) {
            $letter[] = mb_substr($string, $i, 1,'utf-8');
        }
        foreach ($letter as $l) {

            $teststr = $content." ".$l;
            $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);//這個里面是磅值
            // 判斷拼接后的字符串是否超過預設的寬度
            if ((($testbox[2]-$testbox[0]) > $width) && ($content !== "")) {
                $content .= "\n";

            }
            $content .= $l;
        }

        return $content;
    }






    function api_notice_increment($url, $data){
        $ch = curl_init();
        $header = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($url, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOdCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);

        if (curl_errno($ch)) {
            return false;
        }else{
            return $tmpInfo;
        }
    }


    public function get_random_key($len=8) {

        $key = "";
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
        for ($i = 0; $i < $len; $i++) {
            $key.= $pattern{mt_rand(0, 35)};    //生成php隨機數
        }
        return $key;
    }



    public function circle($url,$path){
        $w = 430;  $h=430; // original size

        $dest_path=$path;

        $src = imagecreatefromstring($url);

        $newpic = imagecreatetruecolor($w,$h);
        imagealphablending($newpic,false);
        $transparent = imagecolorallocatealpha($newpic, 233, 40, 59, 0);
        $r=$w/2;
        for($x=0;$x<$w;$x++)
            for($y=0;$y<$h;$y++){
                $c = imagecolorat($src,$x,$y);
                $_x = $x - $w/2;
                $_y = $y - $h/2;
                if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
                    imagesetpixel($newpic,$x,$y,$c);
                }else{
                    imagesetpixel($newpic,$x,$y,$transparent);
                }
            }
        imagesavealpha($newpic, true);
        // header('Content-Type: image/png');
        imagepng($newpic, $dest_path);
        imagedestroy($newpic);
        imagedestroy($src);
        unlink($url);
//        return $dest_path;

    }





    public function getimage(){

        $id=145;

        $Qsubject="測試PHP在圖片上面文字生成";

        $answer=array(
            array('answer_key'=>'測試選項1'),
            array('answer_key'=>'測試選項2'),
            array('answer_key'=>'測試選項3'),
            array('answer_key'=>'測試選項4')
        );

        $customermessage=new  WechatSendCustomerMessage("VQSKmPFVS1532925182");
        //內部方法獲取微信的access_token

        $access_token=$customermessage->get_token();

        //獲取二維碼

        $path="pages/userceter/userceter?id=".$id;

        $width=430;

        $post_data='{"path":"'.$path.'","width":'.$width.'}';

        //$url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;

        //獲取小程序碼
        $url="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token;

        $result=$this->api_notice_increment($url,$post_data);

        if($result){
            //服務器上面的路徑
            $returnpath = dirname(__FILE__) . '/' . '../../../../wemarkdata/updatedata/tmp/';

            $itype = 'jpg';

            $filename = time() . $this->get_random_key(8) . '.' . $itype;

            $filepath = $returnpath . $filename; //文件路徑

            $urlprefix = 'xcximg.xxx.cn'; //域名

            $bucket = 'cardzan'; //阿里雲的bucket

//            $file_name = time() . $this->get_random_key(8) . '.png';

            if (!file_exists($filepath)) {

                $this->circle($result,$filepath); //生成圓形的二維碼圖片,放在服務器上面


                $bg2="https://www.xxx.cn/themes/Xcxdati/bg2.png"; //背景圖片路徑

                $a="https://www.xxx.cn/themes/Xcxdati/a.png";
                $b="https://www.xxx.cn/themes/Xcxdati/b.png";
                $c="https://www.xxx.cn/themes/Xcxdati/c.png";
                $d="https://www.xxx.cn/themes/Xcxdati/d.png";

                $txturl="http://test.xxx.cn/themes/Xcxdati/txt.png"; //文字圖片路徑

                $txt = imagecreatefromstring(file_get_contents($txturl));

                $answeroption=array(
                    imagecreatefromstring(file_get_contents($a)),
                    imagecreatefromstring(file_get_contents($b)),
                    imagecreatefromstring(file_get_contents($c)),
                    imagecreatefromstring(file_get_contents($d))
                );


                $tempfilename=time().$this->get_random_key(8).'.png';

                $tempurl = $returnpath.$tempfilename;

                $BGT = imagecreatefromstring(file_get_contents($bg2));

                $code = imagecreatefromstring(file_get_contents($filepath));

                //注意字體文件的位置,自己文件路徑錯誤文字顯示不出來
                $font = dirname(__FILE__)."/./../Xcxdati/fonts/msyh.ttc"; // 字體文件

                $color = imagecolorallocate($BGT,102,102,102); // 文字顏色

                $content=$this->autowrap(28,0,$font,$Qsubject,620);

                //autowrap($fontsize, $angle, $fontface, $string, $width)
                // 這幾個變量分別是 字體大小, 角度, 字體名稱, 字符串, 預設寬度

                $posY = $this->text_height($content,28,$font); //問題的高度

                imagettftext($BGT,28,0,70,300,$color,$font,$content); // 創建文字

                $posY+=300;

                for($k=0;$k<count($answer);$k++){

                    $text = $this->autowrap(28,0,$font,$answer[$k]['answer_key'],620);
                    $posY =$posY+$this->text_height($answer[$k]['answer_key'],28,$font)+40;
                    imagettftext($BGT,28,0,130,$posY,$color,$font,$text); // 創建文字
                    imagecopyresampled($BGT, $answeroption[$k], 70,($posY-35), 0, 0,42,42,42,42);

                }


                $logo_width = imagesx($code);//二維碼圖片寬度

                $logo_height = imagesy($code);//二維碼圖片高度


                imagecopyresampled($BGT, $code, 290,1030, 0, 0,170,170,$logo_width,$logo_height);

                imagecopyresampled($BGT, $txt, 160,1245, 0, 0,418,37,836,74);

                imagepng($BGT, $tempurl); //合成圖片

                $filename = time() . $this->get_random_key(8) . '.png';

                $ossfilename = 'xcximg/pic/' . $filename;

                $status = $this->uploadtooss($bucket, $tempurl, $ossfilename); //上傳到阿里雲

                $src= 'https://'.$urlprefix .'/'.$ossfilename;

                if($status){

//                    $res['status']=1;
//                    $res['data']=array('src'=>$src);
//                    echo json_encode($res);

                    echo '<img src="'.$src.'">';
                }

            }else{
                echo 0;
            }

        }else{
            echo 0;
        }












    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM