0

我正在尝试在 excel 中保存生成和保存条形码。但是,我在以正确格式保存图像时遇到问题。图像保存在最后一列。但是,我不确定如何保存它。

希望有人可以提供帮助。代码如下:

<?php

require_once '../app/Mage.php';
umask(0);
Mage::app();
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

header("Content-type:text/octect-stream");
header("Content-Disposition:attachment;filename=exportMyEANCodes.csv");

$eanPrefixCode="0123456";

$storeId    = Mage::app()->getStore()->getId();
$product    = Mage::getModel('catalog/product');
$products   = $product->getCollection()->addStoreFilter($storeId)->getAllIds();

echo '"sku","ean","barcode"'. "\n";

foreach($products as $productid)
{
$product = Mage::getModel('catalog/product')->load($productid);

$sku = $product->getSku();

$ean=ean13_check_digit($eanPrefixCode. str_pad($product->getId(), 5, "0", STR_PAD_LEFT));

$bc = new barCode();
//$bc->build($ean);

$output='"'. $sku. '","'. $ean. '","'.$bc->build($ean).'"';
echo $output. "\n";
}

$bc = new barCode();
$bc->build($ean);


function ean13_check_digit($digits){
$digits =(string)$digits;
$even_sum = $digits{1} + $digits{3} + $digits{5} + $digits{7} + $digits{9} + $digits{11};
$even_sum_three = $even_sum * 3;
$odd_sum = $digits{0} + $digits{2} + $digits{4} + $digits{6} + $digits{8} + $digits{10};
$total_sum = $even_sum_three + $odd_sum;
$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten - $total_sum;
return $digits . $check_digit;
}


class barCode
{
    public $bcHeight, $bcThinWidth, $bcThickWidth, $bcFontSize, $mode;

    function __construct($mode='gif', $height=50, $thin=2, $thick=3, $fSize=2)
    {
        $this->bcHeight = $height;
        $this->bcThinWidth = $thin;
        $this->bcThickWidth = $this->bcThinWidth * $thick;
        $this->fontSize = $fSize;
        $this->mode = $mode;
        $this->outMode = array('gif'=>'gif', 'png'=>'png', 'jpeg'=>'jpeg', 'wbmp'=>'vnd.wap.wbmp');
        $this->codeMap = array(
            '0'=>'010000101',   '1'=>'100100001',   '2'=>'001100001',   '3'=>'101100000',
            '4'=>'000110001',   '5'=>'100110000',   '6'=>'001110000',   '7'=>'000100101',   
            '8'=>'100100100',   '9'=>'001100100',   'A'=>'100001001',   'B'=>'001001001',
            'C'=>'101001000',   'D'=>'000011001',   'E'=>'100011000',   'F'=>'001011000',
            'G'=>'000001101',   'H'=>'100001100',   'I'=>'001001100',   'J'=>'000011100',
            'K'=>'100000011',   'L'=>'001000011',   'M'=>'101000010',   'N'=>'000010011',
            'O'=>'100010010',   'P'=>'001010010',   'Q'=>'000000111',   'R'=>'100000110',
            'S'=>'001000110',   'T'=>'000010110',   'U'=>'110000001',   'V'=>'011000001',
            'W'=>'111000000',   'X'=>'010010001',   'Y'=>'110010000',   'Z'=>'011010000',
            ' '=>'011000100',   '$'=>'010101000',   '%'=>'000101010',   '*'=>'010010100',
            '+'=>'010001010',   '-'=>'000110100',   '.'=>'110000100',   '/'=>'010100010'
            );        
    }

    public function build($text='', $showText=true, $fileName=null)
    {
        if (trim($text) <= ' ') 
            throw new exception('barCode::build - must be passed text to operate');
        if (!$fileType = $this->outMode[$this->mode]) 
            throw new exception("barCode::build - unrecognized output format ({$this->mode})");
        if (!function_exists("image{$this->mode}")) 
            throw new exception("barCode::build - unsupported output format ({$this->mode} - check phpinfo)");

        $text  =  strtoupper($text);

        $dispText = "* $text *";
        $text = "*$text*"; // adds start and stop chars
        $textLen  =  strlen($text); 
        $barcodeWidth  =  $textLen * (2 * $this->bcThinWidth + 3 * $this->bcThickWidth) - $this->bcThinWidth; 
        $im = imagecreate($barcodeWidth, $this->bcHeight); 
        $black = imagecolorallocate($im, 0, 0, 0); 
        $white = imagecolorallocate($im, 255, 255, 255); 
        imagefill($im, 0, 0, $white); 

        $xpos = 0;
        for ($idx=0; $idx<$textLen; $idx++)
        { 
            if (!$char = $text[$idx]) $char = '-';
            for ($ptr=0; $ptr<=8; $ptr++)
            { 
                $elementWidth = ($this->codeMap[$char][$ptr]) ? $this->bcThickWidth : $this->bcThinWidth; 
                if (($ptr + 1) % 2) 
                    imagefilledrectangle($im, $xpos, 0, $xpos + $elementWidth-1, $this->bcHeight, $black); 
                $xpos += $elementWidth; 
            }
            $xpos += $this->bcThinWidth; 
        }

        if ($showText)
        {
            $pxWid = imagefontwidth($this->fontSize) * strlen($dispText) + 10;
            $pxHt = imagefontheight($this->fontSize) + 2;
            $bigCenter = $barcodeWidth / 2;
            $textCenter = $pxWid / 2;
            imagefilledrectangle($im, $bigCenter - $textCenter, $this->bcHeight - $pxHt, $bigCenter + $textCenter, $this->bcHeight, $white);
            imagestring($im, $this->fontSize, ($bigCenter - $textCenter) + 5, ($this->bcHeight - $pxHt) + 1, $dispText, $black);
        }

        $badMode = false;
        if (!$fileName) header("Content-type:  image/{$fileType}");
        switch($this->mode)
        {
            case 'gif': 
                imagegif($im, $fileName);
                break;
            case 'png': 
                imagepng($im, $fileName);
                break;
            case 'jpeg': 
                imagejpeg($im, $fileName);
                break;
            case 'wbmp': 
                imagewbmp($im, $fileName);
                break;
            default:
                $badMode = true;
        }

        imagedestroy($im);      
        if ($badMode) 
            throw new Exception("barCode: Unknown Graphics Type '{$this->mode}'");
    }
}


?>
4

1 回答 1

0

我做了一些测试,我也看到了很多相同的角色!

也许另一种方法:让您的脚本输出所需的 CSV 文件,但在第三个字段中,创建一个 URL。您可以在以下位置制作一个小脚本var/export/sku.php

<?php
require_once '../../app/Mage.php';
umask(0);
Mage::app();
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);
if( isset($_GET['sku']) && strlen($_GET['sku']) > 5 ) {
    header('Content-Type: image/jpeg');
    $barcodeOptions = array('text' => $_GET['sku']); 
    $rendererOptions = array(); 
    $imageResource = Zend_Barcode::draw( 
        'code39', 'image', $barcodeOptions, $rendererOptions 
    ); 
    imagejpeg($imageResource);
} ?>

然后在您的脚本中,您(sku),ean,http://yoursite.com/var/export/sku.php?sku=(sku)有一种方法可以让电子表格加载图像——尽管我个人没有这样做,但我已经看到链接显示为嵌入图像。

我上面的示例输出了一个可扫描的 code39 条形码。您可以替换为您自己的图像生成。这就是我问你是否看到条形码的原因,因为我以前没有输出过这样的渲染图像。

于 2013-02-16T08:48:34.570 回答