Wechat--小开发

最近在做微信开发 , 把一些小功能分享给大家 :

微信生成 [二维码] [缩略图] [图片水印]

<?php

define("APPID", "您的APPID");
define("APPSECRET", "您的APPSECRET");
define("OPENID","您的OPENID");
define("METHOD","POST"); //定义传输方式
define("FPID","123"); //场景值ID

class Wechat{
    //构造方法

    //获取Ticket链接
    static $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?"; 

    //获取Token链接
    static $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";

    //获取二维码
    static $qrcode_get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?";

    //获取关注列表
    static $wechatlist_url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=";

    //生成二维码
    function getQRcode(){

        $token=$this->getToken(); //获取token值

        $codeUrl=$this->getQrcodeurl($token,123);//获取二维码

        return $this->DownLoadQr($codeUrl);//下载二维码到本地
    }

    //获取token
     function getToken(){

        $ACCESS_TOKEN = file_get_contents(self::$token_url."appid=".APPID."&secret=".APPSECRET);

        $ACCESS_TOKEN = json_decode($ACCESS_TOKEN);

        $ACCESS_TOKEN = $ACCESS_TOKEN->access_token;

        return $ACCESS_TOKEN;
    }

    //获取二维码链接
    function getQrcodeurl($ACCESS_TOKEN,$fpid){


        //生成永久二维码
        $qrcode= '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_str": '.$fpid.'}}}';

        $result =$this->curlPosts($url,$qrcode,METHOD);

        $arr=json_decode($result,true);

        $ticket=$arr['ticket'];

        $ticket1=urlencode($ticket);

        if(!$ticket1){

            $this->ErrorLogger('getQrcodeurl falied. Error Info: getQrcodeurl get failed');

            exit();

        }

        $url = self::$qrcode_get_url.'ticket='.$ticket1.'';

        return $url;

    }
        //cur上传
        protected function curlPosts($url,$data,$method){

        $ch = curl_init();   //1.初始化
        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
        //4.参数如下
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

        if($method=="POST"){//5.post方式的时候添加数据

            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $tmpInfo = curl_exec($ch);//6.执行

        if (curl_errno($ch)) {//7.如果出错

            return curl_error($ch);
        }
        curl_close($ch);//8.关闭

        return $tmpInfo;
    }

    //下载二维码图片,生成活动图
    protected function DownLoadQr($url){

        if($url == ""){

            return false;

        }

        $filename =OPENID .'.jpg';

        $picPath='./codePic/'.date('Y-m-d',time()).'/pic/'; //生成图片目录

        $file=$picPath."$filename"; //二维码原图片地址

        $get=file_get_contents($url);

        if(!file_exists($picPath)){  //判断原图片地址

            @mkdir('./codePic/');
            @mkdir('./codePic/'.date('Y-m-d',time()));
            @mkdir($picPath,0777);

        }
        $put=file_put_contents($file,$get);

        if($put === false){

            $this->ErrorLogger('dolwload image falied. Error Info: 无法写入图片');
            exit();

        }
        $imgPath='./codePic/'.date('Y-m-d',time()).'/img/';

        if(!file_exists($imgPath)){  //判断缩略图片地址

            @mkdir('./codePic/');
            @mkdir('./codePic/'.date('Y-m-d',time()));
            @mkdir($imgPath,0777);

        }
        $file1=$imgPath."$filename"; //二维码缩略图片地址

       //修改二维码缩略图的大小
        $img=$this->img_create_small($file,170,170,$file1);

        $dst="底层图片的地址.jpg";

        //图片水印
        $hdImg=$this->makeImg($dst,$file1,0,10);

    }

    //图片水印
    protected  function makeImg($dst_path,$src_path,$dst_w,$dst_h){
//创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($src_path));

//获取水印图片的宽高
        list($src_w, $src_h) = getimagesize($src_path);

//修改二维码在原图上的位置
        imagecopymerge($dst, $src, 450, 1110, 0, 0, $src_w, $src_h, 100); 

//如果水印图片本身带透明色,则使用imagecopy方法
//imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);

//输出图片
        list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
        switch ($dst_type) {
            case 1://GIF
                header('Content-Type: image/gif');
                imagegif($dst);
                break;
            case 2://JPG
                header('Content-Type: image/jpeg');
                imagejpeg($dst);
                break;
            case 3://PNG
                header('Content-Type: image/png');
                imagepng($dst);
                break;
            default:
                break;
        }

        imagedestroy($dst);
        imagedestroy($src);
    }

    //二维码缩略图
    function img_create_small($big_img, $width, $height, $small_img) {//原始大图地址,缩略图宽度,高度,缩略图地址

        $imgage = getimagesize($big_img); //得到原始大图片
        switch ($imgage[2]) { // 图像类型判断
            case 1:
                $im = imagecreatefromgif($big_img);
                break;
            case 2:
                $im = imagecreatefromjpeg($big_img);
                break;
            case 3:
                $im = imagecreatefrompng($big_img);
                break;
        }
        $src_W = $imgage[0]; //获取大图片宽度
        $src_H = $imgage[1]; //获取大图片高度
        $tn = imagecreatetruecolor($width, $height); //创建缩略图
        imagecopyresampled($tn, $im, 0, 0, 0, 0, $width, $height, $src_W, $src_H); //复制图像并改变大小
        return imagejpeg($tn, $small_img); //输出图像
    }

    //获取code
    function getCode(){

        $re_uri="http://你的回调地址/index.php";
        $re_uri=urlencode($re_uri);
        $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".
              $re_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
        //echo $url;
        $code=$_GET['code'];
        echo $code;
;    }

    //获取openid
    function getOpenid($token){
       $url="https://api.weixin.qq.com.cgi-bin/user/get?access_token=".$token ;
        $res=$this->https_request($url);
        $jsoninfo=json_decode($res,true);
        $user=($jsoninfo['data']['openid']);
        print_r($user);

    }

    //获取关注列表
    function getWechatlist(){
        $access_token=$this->getToken();
    //获取关注量
        $url = self::$wechatlist_url.$access_token;
        $result =$this->https_request($url);
        $jsoninfo = json_decode($result, true);
        return $jsoninfo['total'];

    }

    //https请求(支持GET和POST)
    function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data))
        {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

//报错日志
    private function ErrorLogger($errMsg){

        $logger = fopen('./ErrorLog.txt', 'a+');

        fwrite($logger, date('Y-m-d H:i:s')." Error Info : ".$errMsg."rn");

    }
}
  • 希望这些能帮助到你!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值