<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Qrcodecreate extends CI_Controller{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('Qrcode');
    }
    public function index($code)
    {  
        $path = 'qrcode/';
        $filename = $path.microtime(TRUE).'.png';
        //remove old image
        $this->removeimg($path,6);
        
        $errorCorrectionLevel = 'L';//容错级别   
        $matrixPointSize = 6;//生成图片大小   
        //生成二维码图片   
        $this->qrcode->png($code,$filename,$errorCorrectionLevel,$matrixPointSize, 2);
        echo "<img src='".base_url($filename)."' />";
    }
    /**
     * [removeimg 删除旧图片]
     * @return [type] [description]
     */
    /**
     * [removeimg 删除旧图片]
     * @param  [type] $path       [文件夹路径]
     * @param  [type] $expiration [缓存时间]
     * @return [type]             []
     */
    public function removeimg($path,$expiration)
    {
        $now = microtime(TRUE);

        $current_dir = @opendir($path);
        while ($filename = @readdir($current_dir))
        {
            if (substr($filename, -4) === '.png' && (str_replace('.png', '', $filename) + $expiration) < $now)
            {
                @unlink($path.$filename);
            }
        }

        @closedir($current_dir);        
    }
}