public function getBarcodePNG($code, $type, $w = 2, $h = 30, $color = array(0, 0, 0))
{
if (!$this->store_path) {
$this->setStorPath(app('config')->get("barcode.store_path"));
}
$this->setBarcode($code, $type);
// calculate image size
$width = $this->barcode_array['maxw'] * $w;
$height = $h;
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$png = imagecreate($width, $height);
$bgcol = imagecolorallocate($png, 255, 255, 255);
imagecolortransparent($png, $bgcol);
$fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;
$bgcol = new \imagickpixel('rgb(255,255,255');
$fgcol = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
$png = new \Imagick();
$png->newImage($width, $height, 'none', 'png');
$bar = new \imagickdraw();
$bar->setfillcolor($fgcol);
} else {
return false;
}
// print bars
$x = 0;
foreach ($this->barcode_array['bcode'] as $k => $v) {
$bw = round($v['w'] * $w, 3);
$bh = round($v['h'] * $h / $this->barcode_array['maxh'], 3);
if ($v['t']) {
$y = round($v['p'] * $h / $this->barcode_array['maxh'], 3);
// draw a vertical bar
if ($imagick) {
$bar->rectangle($x, $y, $x + $bw, $y + $bh);
} else {
imagefilledrectangle($png, $x, $y, $x + $bw - 1, $y + $bh, $fgcol);
}
}
$x += $bw;
}
ob_start();
// get image out put
if ($imagick) {
$png->drawimage($bar);
echo $png;
} else {
imagepng($png);
imagedestroy($png);
}
$image = ob_get_clean();
$image = base64_encode($image);
//$image = 'data:image/png;base64,' . base64_encode($image);
return $image;
}