| 1 |
<?php |
| 2 |
|
| 3 |
namespace WCPOS\Vendor\Picqer\Barcode; |
| 4 |
|
| 5 |
class BarcodeGeneratorHTML extends BarcodeGenerator |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Return an HTML representation of barcode. |
| 9 |
* This original version uses pixel based widths and heights. Use Dynamic HTML version for better quality representation. |
| 10 |
* |
| 11 |
* @param string $barcode code to print |
| 12 |
* @param BarcodeGenerator::TYPE_* $type (string) type of barcode |
| 13 |
* @param int $widthFactor Width of a single bar element in pixels. |
| 14 |
* @param int $height Height of a single bar element in pixels. |
| 15 |
* @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). |
| 16 |
* @return string HTML code. |
| 17 |
*/ |
| 18 |
public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black') : string |
| 19 |
{ |
| 20 |
$barcodeData = $this->getBarcodeData($barcode, $type); |
| 21 |
$html = '<div style="font-size:0;position:relative;width:' . $barcodeData->getWidth() * $widthFactor . 'px;height:' . $height . 'px;">' . \PHP_EOL; |
| 22 |
$positionHorizontal = 0; |
| 23 |
/** @var BarcodeBar $bar */ |
| 24 |
foreach ($barcodeData->getBars() as $bar) { |
| 25 |
$barWidth = \round($bar->getWidth() * $widthFactor, 3); |
| 26 |
$barHeight = \round($bar->getHeight() * $height / $barcodeData->getHeight(), 3); |
| 27 |
if ($bar->isBar() && $barWidth > 0) { |
| 28 |
$positionVertical = \round($bar->getPositionVertical() * $height / $barcodeData->getHeight(), 3); |
| 29 |
// draw a vertical bar |
| 30 |
$html .= '<div style="background-color:' . $foregroundColor . ';width:' . $barWidth . 'px;height:' . $barHeight . 'px;position:absolute;left:' . $positionHorizontal . 'px;top:' . $positionVertical . ($positionVertical > 0 ? 'px' : '') . '"> </div>' . \PHP_EOL; |
| 31 |
} |
| 32 |
$positionHorizontal += $barWidth; |
| 33 |
} |
| 34 |
$html .= '</div>' . \PHP_EOL; |
| 35 |
return $html; |
| 36 |
} |
| 37 |
} |
| 38 |
|