<?php
namespace FMS\eLibreria\CmsBundle\Service;
use Com\Tecnick\Barcode\Barcode;
class BarcodeGenerator {
const CODE_TYPE = "C128";
const FOREGROUND_COLOR = "black";
const BACKGROUND_COLOR = "white";
const PREFIX_BASE64 = "data:image/png;base64, ";
/**
* A QR Code with error correction level M, which the Orden asks the invoice QR for (art. 21.1). The library
* silently turns any level it does not know into L, so `BarcodeGeneratorQrTest` reads the level back from the
* symbol instead of trusting this string (change verifactu-11, design decision 3).
*/
const QR_CODE_TYPE = "QRCODE,M";
/**
* Pixels per module of the QR image: enough for 300 dpi at the 35 mm of the A4 block for every URL an invoice
* prints. The printed size is set in millimetres by the print stylesheets, never by these pixels.
*/
const QR_MODULE_UNITS = 12;
/** The PNG carries no quiet zone of its own: the stylesheets give it in millimetres. */
const QR_PADDING = array(0, 0, 0, 0);
/** @var Barcode */
private $barcode;
public function __construct() {
$this->barcode = new Barcode();
}
public function getC128Binary($code, $width = -1, $height = -30){
// generate a barcode
$bobj = $this->barcode->getBarcodeObj(
self::CODE_TYPE, // barcode type and additional comma-separated parameters
$code, // data string to encode
$width, // bar width (use absolute or negative value as multiplication factor)
$height, // bar height (use absolute or negative value as multiplication factor)
self::FOREGROUND_COLOR, // foreground color
array(0,0,0,0) // padding (use absolute or negative values as multiplication factors)
)->setBackgroundColor(self::BACKGROUND_COLOR); // background color
return $bobj->getPngData();
}
public function getC128Base64($code, $width = -1, $height = -30){
return base64_encode($this->getC128Binary($code, $width, $height));
}
public function getC128Base64ForImageTag($code, $width = -1, $height = -30){
return self::PREFIX_BASE64 . $this->getC128Base64($code, $width, $height);
}
/**
* The PNG of the QR of a verification URL, base64-encoded (change verifactu-11, design decision 3). A
* negative size is the size of each module, so every module is the same whole number of pixels.
*/
public function getQrPngBase64(string $url): string {
$qr = $this->barcode->getBarcodeObj(
self::QR_CODE_TYPE,
$url,
-self::QR_MODULE_UNITS,
-self::QR_MODULE_UNITS,
self::FOREGROUND_COLOR,
self::QR_PADDING
)->setBackgroundColor(self::BACKGROUND_COLOR);
return base64_encode($qr->getPngData());
}
}