2013-02-12 56 views
0

我試圖保存生成並在Excel中保存條碼。但是,我有問題以正確的格式保存圖像。圖像保存在最後一列。但是,我不知道如何保存它。在excel中保存Magento條碼圖像

希望有人能幫忙。代碼如下:

<?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=""; 

$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}'"); 
    } 
} 


?> 
+0

你在哪裏看到正確的圖像? – 2013-02-16 02:01:08

+0

當我將其打印到網頁中打開時,圖像正確顯示。然而,當我下載到excel時,輸出就是這樣,z <\û<üwÉi»œIÚÚ+lIšÍË콨æF¾ŠÙÊ)èØm]ë2í<í[¿i – 2013-02-16 06:39:01

回答

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條形碼,它是可掃描的。你可以用你自己的圖像代替代。這就是爲什麼我問你是否看到條形碼,因爲我以前沒有輸出過這樣的圖像。