PHP生成条形码

最近在做一个功能就是类似于手机支付宝上的付款码的功能,支付宝上的付款码包括条形码和二维码。在这里我们就通过PHP来生条形码。生成条形码的例子可以到官网上去下载

http://www.barcodephp.com/en/download ,下载最新的版本,然后解压到你自己的Apache的的DocumentRoot 目录,就可以生成条形码了。

现在主要介绍一下这个功能在ThinkPHP3.0中如何使用:

首先把下载下来的文件解压,吧class文件夹拷贝到TP的 ORG目录下,在拷贝之前建议先创建一个文件Barcode,因为ORG目录下可能还会有其它的组件,目录结构如下:

其中barcode.clss.php中的代码如下:

<?php
// Including all required classes
require_once('class/BCGFont.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');

/**
 * author cherry
 * 生成条形码类
 * Class BarCode
 *
 *
 */
class BarCode
{
    private $font = "";

    public function __construct()
    {
        $this->font = new BCGFont('./class/font/Arial.ttf', 10);
    }

    /**
     * 生成条形码
     * @param $text
     * @param string $codebar
     */
    public function genBarcode($text,$codebar='BCGcode128')
    {
        include('class/'.$codebar.'.barcode.php');
        $color_black = new BCGColor(0, 0, 0);
        $color_white = new BCGColor(255, 255, 255);
        /*
         * 'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93',
            'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi',
            'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'
        */
        $code = new $codebar();
        $code->setScale(2); // Resolution
        $code->setThickness(30); // Thickness
        $code->setForegroundColor($color_black); // Color of bars
        $code->setBackgroundColor($color_white); // Color of spaces
        $code->setFont($this->font); // Font (or 0)
        $code->parse($text);

        /* Here is the list of the arguments
        1 - Filename (empty : display on screen)
        2 - Background color */
        $drawing = new BCGDrawing('', $color_white);
        $drawing->setBarcode($code);
        $drawing->draw();

        // Header that says it is an image (remove it if you save the barcode to a file)
        header('Content-Type: image/png');
        // Draw (or save) the image into PNG format.
        $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

    }


}

?>

这个class类是我自己封装用的,方便在Thinkphp中调用。那么当前端需要显示条形码时,例如我们在action中有提供一个方法getBarcode,代码如下:

    public function getBarcode(){
        import("@.ORG.Barcode.barcode");
        $barcode = new BarCode();
        $barcode->genBarcode("012345");
    }

你可以直接在浏览器上访问http://localhost/demo/index.php?s=Index/getBarcode 在页面就能生条形码。这种输入一般不会显示在你提前预设的位置。我们在前端利用

img标签就可以做到,代码如下:

<img src='http://localhost/demo/index.php?s=Index/getBarcode' width="158" height="56"/>

如果你用的Apache ,那么到这里条形码已经生成,也可以使用,如果你要验证生成出来的条形码是否正确,那么就到http://www.onlinebarcodereader.com/  验证。

如果你的PHP是部署在微软公司的IIS服务器上的话,那么这种方式有些问题,图片可能出不来,但让这个问题主要的原因是请求头的问题,

第一种解决方案是修改IIS的部分配置,就是把请求头的类型添加进去,这样IIS就会识别,

第二种方法就是我们改进我们的code,用另外一种方式生成。

下面就是改进后的代码,首先看看barcode.class.php的代码:

<?php
// Including all required classes
require_once('class/BCGFont.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');

/**
 * author cherry
 * 生成条形码类
 * Class BarCode
 *
 *
 */
class BarCode
{
    private $font = "";

    public function __construct()
    {
        $this->font = new BCGFont('./class/font/Arial.ttf', 10);
    }

    /**
     * 生成条形码
     * @param $text
     * @param string $codebar
     */
    public function genBarcode($text,$codebar='BCGcode128')
    {
        include('class/'.$codebar.'.barcode.php');
        $color_black = new BCGColor(0, 0, 0);
        $color_white = new BCGColor(255, 255, 255);
        /*
         * 'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93',
            'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi',
            'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'
        */
        $code = new $codebar();
        $code->setScale(2); // Resolution
        $code->setThickness(30); // Thickness
        $code->setForegroundColor($color_black); // Color of bars
        $code->setBackgroundColor($color_white); // Color of spaces
        $code->setFont($this->font); // Font (or 0)
        $code->parse($text);

        /* Here is the list of the arguments
        1 - Filename (empty : display on screen)
        2 - Background color */
        $drawing = new BCGDrawing('', $color_white);
        $drawing->setBarcode($code);
        $drawing->draw();


        ob_start();
        // Header that says it is an image (remove it if you save the barcode to a file)
        header('Content-Type: image/png');
        // Draw (or save) the image into PNG format.
        $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
        $stream = ob_get_clean();
        return $stream;

    }


}

?>


修改之处就是在 header 之前 用了 ob_start() , 在 finish 之后 ,调用了ob_get_clean() 函数,将生成的图片流(stream)从缓冲区中取得,并返回出去。

action 中的代码修改如下:

  public function getBarcode(){
        import("@.ORG.Barcode.barcode");
        $barcode = new BarCode();
        $text = $barcode->genBarcode("012345")
        $this->assign('code',$text);
        $this->display();
 }

前端html的代码如下:

<img width="240" height="100" src="data:image/png;base64,{$text|base64_encode}" style="display:block;width:100%;"/>

这种方式是在浏览器中解析Base64编码图像,详细请参考 http://raylinn.iteye.com/blog/790473

到这里就结束了。

 

posted @ 2014-08-09 10:04  大胡子码农  阅读(1315)  评论(0编辑  收藏  举报