PHP 生成二维码条形码等(CI框架)

PHPQRcode下载地址:http://sourceforge.net/projects/phpqrcode/

代码:

<?php
/**
 * Created by PhpStorm.
 * User: 18110
 * Date: 2019/2/21
 * Time: 11:40
 */
 
require_once str_replace('\\' , '/' , APPPATH).'libraries/tools/phpqrcode/phpqrcode.php';
 
/**
 * 二维码
 * Class Qrcode
 */
class Qrcode_image
{
    public $CI_instance;
    public function __construct()
    {
        $this->CI_instance =& get_instance();
        $this->CI_instance->config->load('business/code' , true);
    }
 
    /**
     * 1. 生成原始的二维码(生成图片文件)
     * 如果已经存在就不刷新了
     */
 
    public function generateQrcodeFile($url){
            $errorCorrectionLevel = 'L';	//容错级别
            $matrixPointSize = 5;			//生成图片大小
            //生成二维码图片
            //microtime();//返回当前Unix时间戳微秒数
            $filename = $this->fileName();
            QRcode::png($url,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
            return $this->getValidPath($filename);
        }
 
    //2. 在生成的二维码中加上logo(生成图片文件)
    public function generateQrcodeFileLogo($url=''){
        $value = $url;					//二维码内容
        $errorCorrectionLevel = 'H';	//容错级别
        $matrixPointSize = 6;			//生成图片大小
        //生成二维码图片
        $filename = $this->fileName();
        QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
        $filename = $this->addLogo($filename);
        return $this->getValidPath($filename);
    }
 
 
    //3. 生成原始的二维码(不生成图片文件)
    public function generateQrcode($url=''){
        $errorCorrectionLevel = 'L';	//容错级别
        $matrixPointSize = 5;			//生成图片大小
        //生成二维码图片
        return QRcode::png($url,false,$errorCorrectionLevel, $matrixPointSize, 2);
    }
 
    /**
     * 给图片加logo
     * @param $filename
     * @return bool
     */
    private function addLogo($filename){
        $logo = $this->CI_instance->config->item('qin_logo' , 'business/code'); 	//准备好的logo图片
        $QR = $filename;			//已经生成的原始二维码图
        if (!file_exists($logo)) {
            return $filename;
        }
        $QR = imagecreatefromstring(file_get_contents($QR));   		//目标图象连接资源。
        $logo = imagecreatefromstring(file_get_contents($logo));   	//源图象连接资源。
        $QR_width = imagesx($QR);			//二维码图片宽度
        $QR_height = imagesy($QR);			//二维码图片高度
        $logo_width = imagesx($logo);		//logo图片宽度
        $logo_height = imagesy($logo);		//logo图片高度
        $logo_qr_width = $QR_width / 4;   	//组合之后logo的宽度(占二维码的1/5)
        $scale = $logo_width/$logo_qr_width;   	//logo的宽度缩放比(本身宽度/组合后的宽度)
        $logo_qr_height = $logo_height/$scale;  //组合之后logo的高度
        $from_width = ($QR_width - $logo_qr_width) / 2;   //组合之后logo左上角所在坐标点
        //重新组合图片并调整大小
        //imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
        //输出图片
        imagepng($QR, $filename);
        imagedestroy($QR);
        imagedestroy($logo);
        return $filename;
    }
 
    /**
     * 获取相对路径
     * @param $filename
     * @return bool|string
     */
    private function getValidPath($filename){
        return substr($filename , strpos($filename , 'uploads'));
    }
 
    /**
     * 文件存储目录
     * @return string
     */
    private function fileDirectory(){
        $file_directory = str_replace('\\','/',FCPATH).'uploads/'.date('Y').'/'.date('m').'/'.date('d');
        Directory($file_directory);
        return $file_directory;
    }
 
    /**
     * 获取文件名称
     * @return string
     */
    private function fileName(){
        return $this->fileDirectory() . '/' . uniqid() . '.png';
    }
}

猜你喜欢

转载自blog.csdn.net/han_cui/article/details/94720493