2012-01-31 25 views
2

條形碼生成器(從這裏取出http://www.barcodephp.com/en/userguide)的php代碼看起來像這樣。哪部分代碼必須在foreach循環中

我不知道哪個部分必須在foreach循環內從數組生成多個條形碼? $code->parse('1'); // Text該行獲得1個值 - 在本例中爲編號1並生成條形碼。我需要生成數量陣列中的所有元素條碼

// Including all required classes 
require_once('incl/class/BCGFontFile.php'); 
require_once('incl/class/BCGColor.php'); 
require_once('incl/class/BCGDrawing.php'); 

// Including the barcode technology 
require_once('incl/class/BCGcode39.barcode.php'); 

// Loading Font 
$font = new BCGFontFile('./incl/class/font/Arial.ttf', 18); 

// The arguments are R, G, B for color. 
$color_black = new BCGColor(0, 0, 0); 
$color_white = new BCGColor(255, 255, 255); 

$drawException = null; 
try { 
    $code = new BCGcode39(); 
    $code->setScale(2); // Resolution 
    $code->setThickness(30); // Thickness 
    $code->setForegroundColor($color_black); // Color of bars 
    $code->setBackgroundColor($color_white); // Color of spaces 
    $code->setFont($font); // Font (or 0) 
    $code->parse('1'); // Text 
} catch(Exception $exception) { 
    $drawException = $exception; 
} 

/* Here is the list of the arguments 
1 - Filename (empty : display on screen) 
2 - Background color */ 
$drawing = new BCGDrawing('', $color_white); 
if($drawException) { 
    $drawing->drawException($drawException); 
} else { 
    $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); 

回答

1

雖然我從來沒有使用以前的圖書館,它看起來像你只需要做到以下幾點:

foreach ($barcodes as $barcode) { 
    $code->parse($barcode); 
    $drawing->setBarcode($code); 
    $drawing->draw(); 
} 

其中$條形碼是一個包含您的條形碼數字的數組,並且該foreach位於您當前的$drawing->draw();部分的位置

0

那麼,您當然可以提取只有1個部分放入循環,但我不會推薦它,因爲如果其他東西會根據輸入而崩潰,那麼你將無法恢復或知道發生了什麼。

我建議您將$ code寫入數組。爲每個人新增一個新的BCGcode39。

與BCGDrawing一樣,它是一個繪圖表面。除非您更改它們的位置,否則不能爲所有條形碼使用相同的繪圖表面。

您可能想要生成多個文件?爲此,您需要更改BCGDrawing的第一個參數。

所以基本上

foreach($barcodes as $basicString) { 
    $drawException = null; 
    ... 
    $code->parse($basicString); 
    ... 
    $drawing = new BCGDrawing($basicString . '.png', $color_white); 
    ... 
    $drawing->draw(); 
    ... 
    // NO header() 
    $drawing->finish(BCGDrawing::IMG_FORMAT_PNG); 
}