<?php
namespace Frontend\Controller;

use Think\Controller;
use Common\Logic\Wechat\WechatWebApi;

class IndexController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        session_start();
        if (!$_SESSION["headimgurl"]) {
            redirect('http://mallapp.guoguodz.com/test_h5/appwww/index.php/WxAuthorize');
            return;
        }
        if($_SERVER['QUERY_STRING']){
            $url = '?'.$_SERVER['QUERY_STRING'];
        }
        $link = 'http://'.$_SERVER["HTTP_HOST"].'/test_h5/appwww/index.php'.$url;

        $this->assign('link',$link);
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $nonceStr = "";
        for ($i = 0; $i < 16; $i++) {
            $nonceStr .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        $weChatApi = WechatWebApi::getInstance();
        $wxToken = S('wx_token');
        if (!$wxToken) {
            $_data = $weChatApi->getToken();
            if (!$_data) $this->error(var_export($weChatApi->getError(), true));
            S('wx_token', $_data['access_token'], 6800);

            $wxToken = $_data['access_token'];
        }

        $jsapi_ticket = S('jsapi_ticket');
        if(!$jsapi_ticket){
            $_data = $weChatApi->getJsTicket($wxToken);
            if (!$_data) $this->error(var_export($weChatApi->getError(), true));
            S('jsapi_ticket', $_data['ticket'], 6800);
            $jsapi_ticket = $_data['ticket'];
        }
        $argu = array();
        $argu['appId'] = $weChatApi->getAppId();
        $argu['url'] = $link;
        $argu['nonceStr'] = $nonceStr;
        $argu['headimg'] = $_SESSION["headimgurl"];
        $argu['wx_name'] = $_SESSION["wx_name"];
        $argu['timestamp'] = NOW_TIME;
        $wxToken = $wxToken;
        $argu['jsapi_ticket'] = $jsapi_ticket;
        $string = "jsapi_ticket=".$argu['jsapi_ticket']."&noncestr=".$argu['nonceStr']."&timestamp=".$argu['timestamp']."&url=".$argu['url'];
        $argu['signature'] = sha1(trim($string));
        $this->assign($argu);
        $this->display('index');
    }


    //http://www.test_h5.net/index.php/Index/get_pic.html 访问路径

    //生成图片
    public function get_pic (){
        session_start();
        $num = $_SESSION['num'] += 1;
        $link = C('DOMAIN').STATIC_COMEON_ASSETS.'/images/zhutu.jpg';
        //主图
        $pic_url = $link;

        //二维码
        $qr_code = 'http://mallapp.guoguodz.com/test_h5/appwww/Assets/Admin/images/qr_code.png';
//        $qr_code = 'http://www.test_h5.net/Assets/Admin/images/qr_code.png';

        //用户头像
        $user_pic = $_SESSION["headimgurl"];
//        $user_pic ='https://wx.qlogo.cn/mmopen/vi_32/ibkgI6UoU3u5Wl8ibzzTzVHEuwuqUpzicQhJDsB1gh9VrZLriaHzaickPjFXibaPvDpn8HsEndfHNqoJ9uXRgyKF16bw/132';

        $im = imagecreatefromjpeg($pic_url);
        $oldW = imagesx($im);
        $oldH = imagesy($im);
        $bg = imagecreatetruecolor($oldW, $oldH);
        imagefill($bg,0,0,0);
        imagecopyresampled($bg, $im, 0, 0, 0, 0, $oldW, $oldH, $oldW, $oldH);

        //二维码水印
        $code = imagecreatefrompng($qr_code);
        imagecopymerge($bg, $code, 1300, 2550, 0, 0, 170, 170, 100);
        //用户头像水印
        $this->thumb($user_pic,$num);
        //缩略图
        $thumb = 'http://mallapp.guoguodz.com/test_h5/appwww/H5/pimg/'.'thumb'.$num.'.png';
        $us_img = imagecreatefrompng($thumb);
        imagecopymerge($bg, $us_img, 1140, 555, 0, 0, 270, 270, 100);

        $text_str = PROJECT_PATH.'appwww/FZY4JW.TTF';
        $textcolor = imagecolorallocate($bg, 0, 0, 0);
        $a = $this->get_array_rand();
        $fontBox_0 = imagettfbbox(38, 0, $text_str, $a[0]);//文字水平居中实质
        $fontBox_1 = imagettfbbox(38, 0, $text_str, $a[1]);//文字水平居中实质
        $fontBox_2 = imagettfbbox(38, 0, $text_str, $a[2]);//文字水平居中实质
        $fontBox_3 = imagettfbbox(38, 0, $text_str, $a[3]);//文字水平居中实质
        $fontBox_4 = imagettfbbox(38, 0, $text_str, $a[4]);//文字水平居中实质
        $fontBox_5 = imagettfbbox(38, 0, $text_str, $a[5]);//文字水平居中实质
        $fontBox_6 = imagettfbbox(38, 0, $text_str, $a[6]);//文字水平居中实质
        $fontBox_7 = imagettfbbox(38, 0, $text_str, $a[7]);//文字水平居中实质
        imagettftext($bg, 58, 0, ceil(($oldW - $fontBox_0[2]) / 5), 1300, $textcolor, $text_str, mb_convert_encoding($a[0], "utf-8"));
        imagettftext($bg, 58, 0, ceil(($oldW - $fontBox_1[2]) / 5), 1700, $textcolor, $text_str, mb_convert_encoding($a[1], "utf-8"));
        imagettftext($bg, 58, 0, ceil(($oldW - $fontBox_2[2]) / 5), 2000, $textcolor, $text_str, mb_convert_encoding($a[2], "utf-8"));
        imagettftext($bg, 58, 0, ceil(($oldW - $fontBox_3[2]) / 5), 2300, $textcolor, $text_str, mb_convert_encoding($a[3], "utf-8"));
        imagettftext($bg, 58, 0, ceil(($oldW - $fontBox_4[2]) / 1.4), 1300, $textcolor, $text_str, mb_convert_encoding($a[4], "utf-8"));
        imagettftext($bg, 58, 0, ceil(($oldW - $fontBox_5[2]) / 1.5), 1550, $textcolor, $text_str, mb_convert_encoding($a[5], "utf-8"));
        imagettftext($bg, 58, 0, ceil(($oldW - $fontBox_6[2]) / 1.4), 1850, $textcolor, $text_str, mb_convert_encoding($a[6], "utf-8"));
        imagettftext($bg, 58, 0, ceil(($oldW - $fontBox_7[2]) / 1.4), 2200, $textcolor, $text_str, mb_convert_encoding($a[7], "utf-8"));

        $path = "H5/pic/";
        if (!is_dir($path)){
            mkdir($path,0777,true);
        }

        imagepng($bg,$path.$num.'.png');
        imagedestroy($bg);

        //返回处理后的水印图片
        $url = 'http://mallapp.guoguodz.com/test_h5/appwww/H5/pic/'.$num.'.png';
        $this->assign('url',$url);
        $this->display();
    }

    //圆图
    public function yuan_img($user_pic,$num){
        $ext     = pathinfo($user_pic);
        $src_img = null;
        switch ($ext['extension']) {
            case 'jpg':
                $src_img = imagecreatefromjpeg($user_pic);
                break;
            case 'png':
                $src_img = imagecreatefrompng($user_pic);
                break;
        }
        $wh  = getimagesize($user_pic);
        $w   = $wh[0];
        $h   = $wh[1];
        $w   = min($w, $h);
        $h   = $w;
        $img = imagecreatetruecolor($w, $h);
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r   = $w / 2; //圆半径
        $y_x = $r; //圆心X坐标
        $y_y = $r; //圆心Y坐标
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }

        $path = "H5/pimg/";
        if (!is_dir($path)){
            mkdir($path,0777,true);
        }

        imagepng($img,$path.$num.'.png');
    }

    //缩略图
    public function thumb($user_pic,$num){

        $src_img = imagecreatefromjpeg($user_pic);/*创建源图像*/
        imagesavealpha($src_img,true);
        $src_w = imagesx($src_img);/*源图像宽*/
        $src_h = imagesy($src_img);/*源图像高*/
        $dst_w = $src_w*2.0;/*目标图像宽*/
        $dst_h = $src_h*2.0;/*目标图像高*/

        $dst_img =imagecreatetruecolor($dst_w,$dst_h);/*创建目标图像*/
        imagealphablending($dst_img,false);
        imagesavealpha($dst_img,true);
        /*生成缩略图*/
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
        /*保存图片*/
        $path = "H5/pimg/";
        if (!is_dir($path)){
            mkdir($path,0777,true);
        }
        imagepng($dst_img,$path.'thumb'.$num.'.png');
        /*删除资源*/
        imagedestroy($dst_img);
    }

    //随机取
    public function get_array_rand(){
        $a_2019 = ["迷茫","单身","痛苦","遗憾","啤酒肚"];
        $a_2019_1 = ["苦涩","劳累","九九六","脱发","贫穷"];
        $a_2019_2 = ["多余的赘肉","忧愁","烦恼失意","伤心","压力",];
        $a_2019_3 = ["寂寞","孤独","各种加班","不值得的人","失眠","病痛"];
        $a_2020 =array("被疯狂告白","脱单","脱胖","脱贫","绩效翻倍","今年暴富","干啥啥成功","甜甜的爱情","狂吃不胖","身体棒棒","18岁的容颜","赚到一亿","加薪升职","做领导","考试不挂科","人生巅峰","被人疼爱","年会中大奖","买的起房","一觉天亮","燕窝自由","信用卡自由","有时间做饭");
        $r_1=array_rand($a_2019);
        $r_2=array_rand($a_2019_1);
        $r_3=array_rand($a_2019_2);
        $r_4=array_rand($a_2019_3);
        $r_5=array_rand($a_2020);
        $r_6=array_rand($a_2020);
        $r_7=array_rand($a_2020);
        $r_8=array_rand($a_2020);
        $arr_text = [
            $a_2019[$r_1],
            $a_2019_1[$r_2],
            $a_2019_2[$r_3],
            $a_2019_3[$r_4],
            $a_2020[$r_5],
            $a_2020[$r_6],
            $a_2020[$r_7],
            $a_2020[$r_8],
        ];

        return $arr_text;

    }
}
