CI中加入生成一维码的lib

给朋友做的一个项目,其中用到生成条形码,主要用到了http://www.barcodephp.com/这个开源的类库,把它集成到了CI里面,代码和集成方法稍后给出来
1.到http://www.barcodephp.com/en/download下载包
2.解压后,将class文件夹放到application/libraries下面,可以改名为barcode,如果需要用他字体的话,font也放到你放字体的地方
3.application/libraries下面新建Barcode.php

<?php
include 'barcode/BCGFontFile.php';
include 'barcode/BCGColor.php';
include 'barcode/BCGDrawing.php';

include 'barcode/BCGcode39.barcode.php';

class Barcode {
    public $colorFront;
    public $colorBlack;
    public $font;

    function __construct(){
        $this->colorBlack = new BCGColor(255,255,255);
        $this->colorFront = new BCGColor(0,0,0);
//        $this->font = new BCGFontFile('/assets/font/Arial.ttf', 18);//需要字体在这里指定
    }

    public function genBarcode($text) {
        $code = new BCGcode39();

        $code->setScale(2); // Resolution
        $code->setThickness(30); // Thickness
        $code->setForegroundColor($this->colorFront); // Color of bars
        $code->setBackgroundColor($this->colorBlack); // Color of spaces
//        $code->setFont($this->font); // Font (or 0)
        $code->parse($text); // Text

        $drawing = new BCGDrawing('', $this->colorBlack);
        $drawing->setBarcode($code);
        $drawing->draw();

        header('Content-Type: image/png');


        $drawing->finish((BCGDrawing::IMG_FORMAT_PNG));
    }
}

调用的时候,直接load这个barcode,然后调用genBarcode即可

计算web访客的一个会话识别算法的php实现

QQ截图20140506173210

最近在做公司的BI系统,由于很多东西都是探索阶段,做起来遇到很多的问题,下面这个是其中一个计算一次访问会话的实现,还待优化和改进

/**
 * @param $data array(key=>array((int)view_time,url,referer)) order by value asc
 * @param $threshold
 * @param $delta
 * @return boolean
 */
function uvAlgorithm($data, $threshold, $delta) {
    $stack = array();
    $rangeDown = $data[0]['view_time'];
    $vid = md5($rangeDown);
    $rangeUp = $data[0]['view_time'] + $threshold;
//    echo $rangeDown . '~' . $rangeUp . "\n";
//    print_r($data);
    foreach ($data as $k=>$v) {
        if ($k == 0) {
            updateVisitId($v['id'], $vid);
            $stack[] = $v['url'];
            continue;
        }
        if ($v['view_time'] >= $rangeDown && $v['view_time'] < $rangeUp) {
            if (in_array($v['referer'], $stack) || ($data[$k]['view_time'] - $data[$k - 1]['view_time'] < $delta)) {
                $stack[] = $v['url'];
                updateVisitId($v['id'], $vid);
            }
            else {
                updateVisitId($v['id'], md5($v['view_time']));
            }

        }
        else {
            $temp = array_splice($data, $k);
//            print_r($temp);
//            print_r($data);
//            print_r($stack);
            $stack = array();
            if (empty($temp)) {
                return false;
            }
            else {
                uvAlgorithm($temp, $threshold, $delta);
            }
        }
    }
}