BarcodeGeneratorHTML.php
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace Picqer\Barcode;
class BarcodeGeneratorHTML extends BarcodeGenerator
{
/**
* Return an HTML representation of barcode.
*
* @param string $code code to print
* @param string $type type of barcode
* @param int $widthFactor Width of a single bar element in pixels.
* @param int $totalHeight Height of a single bar element in pixels.
* @param int|string $color Foreground color for bar elements (background is transparent).
* @return string HTML code.
* @public
*/
public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = 'black')
{
$barcodeData = $this->getBarcodeData($code, $type);
$html = '<div style="font-size:0;position:relative;width:' . ($barcodeData['maxWidth'] * $widthFactor) . 'px;height:' . ($totalHeight) . 'px;">' . "\n";
$positionHorizontal = 0;
foreach ($barcodeData['bars'] as $bar) {
$barWidth = round(($bar['width'] * $widthFactor), 3);
$barHeight = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3);
if ($bar['drawBar']) {
$positionVertical = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3);
// draw a vertical bar
$html .= '<div style="background-color:' . $color . ';width:' . $barWidth . 'px;height:' . $barHeight . 'px;position:absolute;left:' . $positionHorizontal . 'px;top:' . $positionVertical . 'px;"> </div>' . "\n";
}
$positionHorizontal += $barWidth;
}
$html .= '</div>' . "\n";
return $html;
}
}