生成ean13条形码并将其保存到png

时间:2016-02-10 14:04:06

标签: php prestashop ean-13

我正在使用Jacek Kowalski Kody kreskowe - EAN-13http://jacekk.info)来生成EAN13条形码并将其保存到图像文件中。我想在Prestashop的订购过程中动态生成条形码。将该图像保存在服务器上并将其放入new_order.html电子邮件模板中。

有完整的代码:https://jacekk.info/skrypty/ean13.phps

我通过将$_GET['kod']更改为$kod_in进行了轻微修改,并将$kod_in = 1234567891011放在文件的开头,将imagepng($i, $new_filename);放在最后

当我直接输入文件ean13.php或者我运行该代码(直接在网络浏览器中)时,一切运行良好(我看到生成的条形码和脚本创建新的图像文件并将其保存在服务器上):

$kod_in = 1234567891011;
include (dirname(__FILE__)."/ean13.php");

但是当我尝试将上面的代码包含在订单处理中时,完全在mailalerts.php中,它不显示条形码并生成空白(白色)图像文件。我认为它与生成的条形码没有显示在屏幕上然后被保存的事实有关。

请帮助修改代码以生成图像,即使它们没有显示在屏幕上。

1 个答案:

答案 0 :(得分:3)

在脚本结束时替换:

header('Content-type: image/gif');
imagegif($i);

有了这个:

$imageLocation = "where/you/want/to/save/your/file";
imagegif($i, $imageLocation);

这是imagegif Documentation

您可以通过以下方式将此库用作Class:

<?php
/***************************************************
 *             Kody kreskowe - EAN-13              *
 ***************************************************
 * Ostatnia modyfikacja: 01.11.2012                *
 * Autor: Jacek Kowalski (http://jacekk.info)      *
 *                                                 *
 * Strona WWW: http://jacekk.info/scripts/barcodes *
 *                                                 *
 * Utwór rozprowadzany na licencji                 *
 * http://creativecommons.org/licenses/by-nc/2.5/  *
 ***************************************************/

/* Kodowanie znaków UTF-8 */

class BarCode {

    public $kol = array(
        '0' => array('A', 'A', 'A', 'A', 'A', 'A'),
        '1' => array('A', 'A', 'B', 'A', 'B', 'B'),
        '2' => array('A', 'A', 'B', 'B', 'A', 'B'),
        '3' => array('A', 'A', 'B', 'B', 'B', 'A'),
        '4' => array('A', 'B', 'A', 'A', 'B', 'B'),
        '5' => array('A', 'B', 'B', 'A', 'A', 'B'),
        '6' => array('A', 'B', 'B', 'B', 'A', 'A'),
        '7' => array('A', 'B', 'A', 'B', 'A', 'B'),
        '8' => array('A', 'B', 'A', 'B', 'B', 'A'),
        '9' => array('A', 'B', 'B', 'A', 'B', 'A')
    );

    public $code = array(
        'start' => '101',
        'lewa' => array(
            'A' => array(
                '0' => '0001101',
                '1' => '0011001',
                '2' => '0010011',
                '3' => '0111101',
                '4' => '0100011',
                '5' => '0110001',
                '6' => '0101111',
                '7' => '0111011',
                '8' => '0110111',
                '9' => '0001011'
            ),
            'B' => array(
                '0' => '0100111',
                '1' => '0110011',
                '2' => '0011011',
                '3' => '0100001',
                '4' => '0011101',
                '5' => '0111001',
                '6' => '0000101',
                '7' => '0010001',
                '8' => '0001001',
                '9' => '0010111'
            )
        ),
        'srodek' => '01010',
        'prawa' => array(
            '0' => '1110010',
            '1' => '1100110',
            '2' => '1101100',
            '3' => '1000010',
            '4' => '1011100',
            '5' => '1001110',
            '6' => '1010000',
            '7' => '1000100',
            '8' => '1001000',
            '9' => '1110100'
        ),
        'stop' => '101'
    );

    public $b;

    public function __construct($barcode) {

        $len = strlen($barcode);
        if(trim($barcode, '0123456789')!='' OR ($len!=12 AND $len!=13)) {
            echo 'Znaki inne niż cyfry lub błędna długość ('.$len.')';
            die();
        }

        $kod = str_split(substr($barcode, 0, 12));
        $now = 1;
        $sum = 0;
        foreach($kod as $val) {
            if($now==1) {
                $sum += $val;
                $now = 3;
            }
            else
            {
                $sum += $val*3;
                $now = 1;
            }
        }
        $sum = 10-($sum%10);
        if($sum==10) $sum = 0;

        if($len==12) {
            $barcode .= $sum;
        }
        elseif(substr($barcode, -1)!=$sum) {
            echo 'Błędna suma kontrolna '.$sum;
            die();
        }

        unset($len, $kod, $now, $sum);

        $sys = substr($barcode, 0, 1);
        $lewa = substr($barcode, 1, 6);
        $prawa = substr($barcode, 7);

        $i = imagecreate(95, 40);
        $w = imagecolorallocate($i, 255, 255, 255);
        $this->b = imagecolorallocate($i, 0, 0, 0);

        $this->print_code($this->code['start'].$this->gen_binary($lewa, 0, $sys).$this->code['srodek'].$this->gen_binary($prawa, 1, $sys).$this->code['stop'], $i);

        imagegif($i, 'test.gif');
    }


    public function gen_binary($kod, $strona, $sys) {
        $kod = str_split($kod);
        $ret = '';
        if($strona==0) {
            foreach($kod as $key => $val) {
                $ret .= $this->code['lewa'][$this->kol[$sys][$key]][$val];
            }
        }
        else
        {
            foreach($kod as $val) {
                $ret .= $this->code['prawa'][$val];
            }
        }
        return $ret;
    }

    public function print_code($kod, $img) {
        $now = 0;
        $kod = str_split($kod);
        foreach($kod as $val) {
            if($val==1) {
                imageline($img, $now, 0, $now, 40, $this->b);
                $now++;
            }
            elseif($val==0) {
                $now++;
            }
        }
    }
}

您需要在课程声明之前将其包含在文件顶部一次:

 include_once('path/to/BarCode.php');

现在,您不必包含生成图片的脚本,而是必须创建新的BarCode对象。

 new BarCode('9780486425573');

测试并使用我的Prestashop 1.6。

相关问题