woocommerce-pos
/
vendor_prefixed
/
picqer
/
php-barcode-generator
/
src
/
BarcodeGeneratorDynamicHTML.php
BarcodeGeneratorDynamicHTML.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/picqer/php-barcode-generator/src/BarcodeGeneratorDynamicHTML.php
| 1 | <?php |
| 2 | |
| 3 | namespace WCPOS\Vendor\Picqer\Barcode; |
| 4 | |
| 5 | class BarcodeGeneratorDynamicHTML extends BarcodeGenerator |
| 6 | { |
| 7 | private const WIDTH_PRECISION = 6; |
| 8 | /** |
| 9 | * Return an HTML representation of barcode. |
| 10 | * This 'dynamic' version uses percentage based widths and heights, resulting in a vector-y qualitative result. |
| 11 | * |
| 12 | * @param string $barcode code to print |
| 13 | * @param BarcodeGenerator::TYPE_* $type (string) type of barcode |
| 14 | * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). |
| 15 | * @return string HTML code. |
| 16 | */ |
| 17 | public function getBarcode(string $barcode, $type, string $foregroundColor = 'black') : string |
| 18 | { |
| 19 | $barcodeData = $this->getBarcodeData($barcode, $type); |
| 20 | $html = '<div style="font-size:0;position:relative;width:100%;height:100%">' . \PHP_EOL; |
| 21 | $positionHorizontal = 0; |
| 22 | /** @var BarcodeBar $bar */ |
| 23 | foreach ($barcodeData->getBars() as $bar) { |
| 24 | $barWidth = $bar->getWidth() / $barcodeData->getWidth() * 100; |
| 25 | $barHeight = \round($bar->getHeight() / $barcodeData->getHeight() * 100, 3); |
| 26 | if ($bar->isBar() && $barWidth > 0) { |
| 27 | $positionVertical = \round($bar->getPositionVertical() / $barcodeData->getHeight() * 100, 3); |
| 28 | // draw a vertical bar |
| 29 | $html .= '<div style="background-color:' . $foregroundColor . ';width:' . \round($barWidth, self::WIDTH_PRECISION) . '%;height:' . $barHeight . '%;position:absolute;left:' . \round($positionHorizontal, self::WIDTH_PRECISION) . '%;top:' . $positionVertical . ($positionVertical > 0 ? '%' : '') . '"> </div>' . \PHP_EOL; |
| 30 | } |
| 31 | $positionHorizontal += $barWidth; |
| 32 | } |
| 33 | $html .= '</div>' . \PHP_EOL; |
| 34 | return $html; |
| 35 | } |
| 36 | } |
| 37 |