php验证码类,phpquery页面解析封装

class Captcha
{   
    //验证码的session的下标  
    public static $seKey = 'verify_py';   //验证码关键字
    public static $expire = 3000;     // 验证码过期时间(s)    
    //验证码中使用的字符,01IO容易混淆,不用  
    public static $codeSet = 'ABCDEFGHJKLMNPQRTUVWXY';
    public static $fontSize = 26;     // 验证码字体大小(px)   
    public static $useCurve = true;   // 是否画混淆曲线   
    public static $useNoise = true;   // 是否添加杂点    
    public static $imageH = 0;        // 验证码图片宽   
    public static $imageL = 0;        // 验证码图片长   
    public static $length = 4;        // 验证码位数   
    public static $bg = array(243, 251, 254);  // 背景
    public static $ttf;               // 字体(绝对路径)

    protected static $_image = null;     // 验证码图片实例   
    protected static $_color = null;     // 验证码字体颜色   

    /**  
     * 输出验证码并把验证码的值保存的session中  
     */  
    public static function entry() {
        // 图片宽(px)   
        self::$imageL || self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize*1.5;    
        // 图片高(px)   
        self::$imageH || self::$imageH = self::$fontSize * 2;   
        // 建立一幅 self::$imageL x self::$imageH 的图像   
        self::$_image = imagecreate(self::$imageL, self::$imageH);    
        // 设置背景         
        imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]);    
        // 验证码字体随机颜色   
        self::$_color = imagecolorallocate(self::$_image, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120));   

        if (self::$useNoise) {   
            // 绘杂点   
            self::_writeNoise();   
        }    
        if (self::$useCurve) {   
            // 绘干扰线   
            self::_writeCurve();   
        }   

        // 绘验证码   
        $code = array(); // 验证码   
        $codeNX = 0; // 验证码第N个字符的左边距   
        for ($i = 0; $i<self::$length; $i++) {   
            $code[$i] = self::$codeSet[mt_rand(0, strlen(self::$codeSet) - 1)];   
            $codeNX += mt_rand(self::$fontSize*1.2, self::$fontSize*1.6);   
            // 写一个验证码字符   
            imagettftext(self::$_image, self::$fontSize, mt_rand(-35, 60), $codeNX, self::$fontSize*1.5, self::$_color, self::$ttf, $code[$i]);   
        }   

        // 保存验证码   
        isset($_SESSION) || @session_start(); 
        // 把验证码保存到session, 验证时注意是大写 
        $_SESSION[self::$seKey]['code'] = join('', $code); 
        // 验证码有效时间
        $_SESSION[self::$seKey]['time'] = time() + self::$expire;     

        header('Pragma: no-cache');        
        header("content-type: image/JPEG");
        // 输出图像   
        imageJPEG(self::$_image);    
        imagedestroy(self::$_image);   
    }

    /**
     * 校验验证码
     * @param  string $code  验证码值
     * @param  string $seKey 验证码存储session中的key
     * @return string 验证通过返回null,失败返回错误消息
     */
    public static function verifyCode($code, $seKey = '')
    {
        $seKey = $seKey ?: self::$seKey;
        if (!isset($_SESSION[$seKey])) {
            return 'sekey no exists.';
        }
        if (strcmp(strtolower($_SESSION[$seKey]['code']), strtolower($code))) {
            return '图片验证码错误';
        }
        if ($_SESSION[$seKey]['time'] < time()) {
            return '图片验证码已过期';
        }
        return null;
    }

    /**   
     * 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数)   
     *      正弦型函数解析式:y=Asin(ωx+φ)+b  
     *      各常数值对函数图像的影响:  
     *        A:决定峰值(即纵向拉伸压缩的倍数)  
     *        b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)  
     *        φ:决定波形与X轴位置关系或横向移动距离(左加右减)  
     *        ω:决定周期(最小正周期T=2π/∣ω∣)  
     */  
    protected static function _writeCurve() {   
        $A = mt_rand(1, self::$imageH/2);                  // 振幅   
        $b = mt_rand(-self::$imageH/4, self::$imageH/4);   // Y轴方向偏移量   
        $f = mt_rand(-self::$imageH/4, self::$imageH/4);   // X轴方向偏移量   
        $T = mt_rand(self::$imageH*1.5, self::$imageL*2);  // 周期   
        $w = (2* M_PI)/$T;   

        $px1 = 0;  // 曲线横坐标起始位置   
        $px2 = mt_rand(self::$imageL/2, self::$imageL * 0.667);  // 曲线横坐标结束位置              
        for ($px=$px1; $px<=$px2; $px=$px+ 0.9) {   
            if ($w!=0) {   
                $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2;  // y = Asin(ωx+φ) + b   
                $i = (int) ((self::$fontSize - 6)/4);   
                while ($i > 0) {    
                    imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color);  
                    //这里画像素点比imagettftext和imagestring性能要好很多                     
                    $i--;   
                }   
            }   
        }   

        $A = mt_rand(1, self::$imageH/2);                  // 振幅           
        $f = mt_rand(-self::$imageH/4, self::$imageH/4);   // X轴方向偏移量   
        $T = mt_rand(self::$imageH*1.5, self::$imageL*2);  // 周期   
        $w = (2* M_PI)/$T;         
        $b = $py - $A * sin($w*$px + $f) - self::$imageH/2;   
        $px1 = $px2;   
        $px2 = self::$imageL;   
        for ($px=$px1; $px<=$px2; $px=$px+ 0.9) {   
            if ($w!=0) {   
                $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2;  // y = Asin(ωx+φ) + b   
                $i = (int) ((self::$fontSize - 8)/4);   
                while ($i > 0) {            
                    imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); 
                    //这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出
                    //的(不用while循环)性能要好很多       
                    $i--;   
                }   
            }   
        }   
    }   

    /**  
     * 画杂点  
     * 往图片上写不同颜色的字母或数字  
     */  
    protected static function _writeNoise() {   
        for($i = 0; $i < 10; $i++){   
            //杂点颜色   
            $noiseColor = imagecolorallocate(   
                              self::$_image,    
                              mt_rand(150,225),    
                              mt_rand(150,225),    
                              mt_rand(150,225)   
                          );   
            for($j = 0; $j < 5; $j++) {   
                // 绘杂点   
                imagestring(   
                    self::$_image,   
                    5,    
                    mt_rand(-10, self::$imageL),    
                    mt_rand(-10, self::$imageH),    
                    self::$codeSet[mt_rand(0, strlen(self::$codeSet) - 1)], // 杂点文本为随机的字母或数字   
                    $noiseColor  
                );   
            }   
        }   
    }   
} 

调用

Captcha::$expire = 300;
Captcha::$seKey = 'registerVerifyCode';
Captcha::$useNoise = false;
Captcha::$bg = [255, 255, 255];
Captcha::$ttf = __DIR__ . '/font/COUR.TTF';     
die(Captcha::entry());

附(phpquery封装方法,采集匹配很实用,懂jquery语法就行)

1.首先下载phpQuery插件
2.封装方法

require_once(DIR . ‘/phpquery-master/phpQuery/phpQuery.php’);

/**
 * 匹配dom节点数据 for example :
 * 
 *     $reviewReg = array(
 *          'reply_url' => array('.review-title', 'href'),
 *          'title' => array('.review-title', 'html'),
 *          'buyer_url' => array('.review-byline .author', 'href'),
 *          'buyer' => array('.review-byline .author', 'html'),
 *          'review_date' => array(
 *              '.review-date', 
 *              'html', 
 *              function($date) {
 *                  // 格式化日期
 *                  $date = ltrim($date, 'on');
 *                  return date('Y-m-d', strtotime($date));
 *              }
 *          ),
 *          'review_info' => array('.review-text', 'html'),
 *          'star' => array('.a-icon-star', 'class', function ($starClass) {
 *              $starClass = preg_replace('/\s+/', ' ', $starClass);
 *              $arr = explode(' ', $starClass);
 *              foreach ($arr as $one) {
 *                  if (strpos($one, 'a-star-') !== false) {
 *                      list(,,$star) = explode('-', $one);
 *                      return $star;
 *                  }
 *              }
 *          }),
 *      );
 *      QueryList::query($reviewReg, '#cm_cr-review_list .review', $html)
 *      
 * @param  array  $reg  规则
 * @param  string $rang 父节点(跟jquery语法一样)
 * @param  string $webpage 采集获取到的页面
 * @return array
 * @author ouyangfeng
 */
public function query(array $reg, $rang, $webPage)
{
    $datas = [];

    phpQuery::newDocumentHTML($webPage);
    $lists = pq($rang);
    foreach ($lists as $list)
    {
        $list = pq($list);
        $data = [];
        foreach ($reg as $column => $rule) {
            $noAttr = ['html', 'text'];
            if (in_array($rule[1], $noAttr)) {
                $value = $list->find($rule[0])->$rule[1]();
            } else {
                $value = $list->find($rule[0])->attr($rule[1]);
            }

            $callback = isset($rule[2]) ? $rule[2] : null;
            if (is_callable($callback)) {
                $value = call_user_func_array($callback, [$value]);
            }

            $data[$column] = trim($value);
        }

        $datas[] = $data;
    }

    // 清内存
    phpQuery::$documents = array();
    return $datas;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值