php-gdlabel/src/Elements/BarcodeElement.php

98 lines
4.0 KiB
PHP

<?php
namespace NoccyLabs\GdLabel\Elements;
use NoccyLabs\GdLabel\Element;
use Picqer\Barcode\BarcodeGeneratorPNG;
class BarcodeElement extends Element
{
protected $image;
protected function draw($gd, int $x, int $y, int $width, int $height, array $params)
{
$pw = $this->getProp("width", $width);
$ph = $this->getProp("height", $height);
if (!$this->image) {
$this->renderBarcode($params);
}
$sx = imagesx($this->image);
$sy = imagesy($this->image);
$aspect = $sx/$sy;
if ($aspect>1) {
$dw = $width;
$dh = $width / $aspect;
} else {
$dw = $height * $aspect;
$dh = $height;
// printf("--%d--\n", $dw);
}
imagecopyresampled($gd, $this->image, $x, $y, 0, 0, $dw, $dh, $sx, $sy);
}
protected function renderBarcode(array $params)
{
if (($bound = $this->getProp("bind-value"))) {
$value = array_key_exists($bound, $params)?$params[$bound]:"?{$bound}?";
} else {
$value = $this->getProp("value");
}
$color = $this->getPropRgb("text-color");
$type = $this->getPropType("barcode-type");
$barcodeGenerator = new BarcodeGeneratorPNG();
$barcodeData = $barcodeGenerator->getBarcode($value, $type, 2, 30, [0,0,0]);
$barcode = imagecreatefromstring($barcodeData);
$this->image = $barcode;
}
protected function getPropType(string $name)
{
$typeString = $this->getProp($name);
switch ($typeString) {
case 'code39': $type = BarcodeGeneratorPNG::TYPE_CODE_39; break;
case 'code39-c': $type = BarcodeGeneratorPNG::TYPE_CODE_39_CHECKSUM; break;
case 'code39-e': $type = BarcodeGeneratorPNG::TYPE_CODE_39E; break;
case 'code39-ec': $type = BarcodeGeneratorPNG::TYPE_CODE_39E_CHECKSUM; break;
case 'code93': $type = BarcodeGeneratorPNG::TYPE_CODE_93; break;
case 'standard25': $type = BarcodeGeneratorPNG::TYPE_STANDARD_2_5; break;
case 'standard25-c': $type = BarcodeGeneratorPNG::TYPE_STANDARD_2_5_CHECKSUM; break;
case 'interleaved25': $type = BarcodeGeneratorPNG::TYPE_INTERLEAVED_2_5; break;
case 'interleaved25-c': $type = BarcodeGeneratorPNG::TYPE_INTERLEAVED_2_5_CHECKSUM; break;
case 'code128': $type = BarcodeGeneratorPNG::TYPE_CODE_128; break;
case 'code128a': $type = BarcodeGeneratorPNG::TYPE_CODE_128_A; break;
case 'code128b': $type = BarcodeGeneratorPNG::TYPE_CODE_128_B; break;
case 'code128c': $type = BarcodeGeneratorPNG::TYPE_CODE_128_C; break;
case 'ean2': $type = BarcodeGeneratorPNG::TYPE_EAN_2; break;
case 'ean5': $type = BarcodeGeneratorPNG::TYPE_EAN_5; break;
case 'ean8': $type = BarcodeGeneratorPNG::TYPE_EAN_8; break;
case 'ean13': $type = BarcodeGeneratorPNG::TYPE_EAN_13; break;
case 'upca': $type = BarcodeGeneratorPNG::TYPE_UPC_A; break;
case 'upce': $type = BarcodeGeneratorPNG::TYPE_UPC_E; break;
case 'msi': $type = BarcodeGeneratorPNG::TYPE_MSI; break;
case 'msi-c': $type = BarcodeGeneratorPNG::TYPE_MSI_CHECKSUM; break;
case 'postnet': $type = BarcodeGeneratorPNG::TYPE_POSTNET; break;
case 'planet': $type = BarcodeGeneratorPNG::TYPE_PLANET; break;
case 'rms4cc': $type = BarcodeGeneratorPNG::TYPE_RMS4CC; break;
case 'kix': $type = BarcodeGeneratorPNG::TYPE_KIX; break;
case 'imb': $type = BarcodeGeneratorPNG::TYPE_IMB; break;
case 'codabar': $type = BarcodeGeneratorPNG::TYPE_CODABAR; break;
case 'code11': $type = BarcodeGeneratorPNG::TYPE_CODE_11; break;
case 'pharma': $type = BarcodeGeneratorPNG::TYPE_PHARMA_CODE; break;
case 'pharma-2': $type = BarcodeGeneratorPNG::TYPE_PHARMA_CODE_TWO_TRACKS; break;
default: $type = BarcodeGeneratorPNG::TYPE_CODE_39; break;
}
return $type;
}
}