Created
July 12, 2017 10:01
-
-
Save robertripoll/eb102068fd83c176172fdbb2327d2624 to your computer and use it in GitHub Desktop.
Barcode generator in PNG format with text in the bottom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = array(0, 0, 0)) | |
{ | |
$barcodeData = $this->getBarcodeData($code, $type); | |
// calculate image size | |
$width = ($barcodeData['maxWidth'] * $widthFactor); | |
$height = $totalHeight; | |
if (function_exists('imagecreate')) { | |
// GD library | |
$imagick = false; | |
$png = imagecreate($width, $height + 20); // +20 (+) | |
$colorBackground = imagecolorallocate($png, 255, 255, 255); | |
imagecolortransparent($png, $colorBackground); | |
$colorForeground = imagecolorallocate($png, $color[0], $color[1], $color[2]); | |
} elseif (extension_loaded('imagick')) { | |
$imagick = true; | |
$colorForeground = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); | |
$png = new \Imagick(); | |
$png->newImage($width, $height + 20, 'none', 'png'); // +20 (+) | |
$imageMagickObject = new \imagickdraw(); | |
$imageMagickObject->setFillColor($colorForeground); | |
} else { | |
return false; | |
} | |
// print bars | |
$positionHorizontal = 0; | |
foreach ($barcodeData['bars'] as $bar) { | |
$bw = round(($bar['width'] * $widthFactor), 3); | |
$bh = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3); | |
if ($bar['drawBar']) { | |
$y = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3); | |
// draw a vertical bar | |
if ($imagick && isset($imageMagickObject)) { | |
$imageMagickObject->rectangle($positionHorizontal, $y, ($positionHorizontal + $bw), ($y + $bh)); | |
} else { | |
imagefilledrectangle($png, $positionHorizontal, $y, ($positionHorizontal + $bw) - 1, ($y + $bh), | |
$colorForeground); | |
} | |
} | |
$positionHorizontal += $bw; | |
} | |
if ($imagick && isset($imageMagickObject)) { | |
$draw = new ImagickDraw(); | |
$draw->setFillColor('black'); | |
/* Font properties */ | |
$draw->setFont('Bookman-DemiItalic'); | |
$draw->setFontSize(5); | |
// Write the barcode's code, change $code to write other text | |
$imageMagickObject->annotateImage($draw, 0, $height + 5, 0, $code); | |
} | |
else | |
{ | |
// Detect center position | |
$font = 7; | |
$font_width = ImageFontWidth($font); | |
$font_height = ImageFontHeight($font); | |
$text_width = $font_width * strlen($code); | |
$position_center = ceil(($width - $text_width) / 2); | |
// Default font | |
// Write the barcode's code, change $code to write other text | |
imagestring($png, 7, $position_center, $height + 5, $code, imagecolorallocate($png, 0, 0, 0)); | |
// For custom font specify path to font file | |
/*$fontPath = '..\font.ttf'; | |
imagettftext($png, 12, 0, $position_center, $height + 5, imagecolorallocate($png, 0, 0, 0), $fontPath, $code);*/ | |
} | |
ob_start(); | |
if ($imagick && isset($imageMagickObject)) { | |
$png->drawImage($imageMagickObject); | |
echo $png; | |
} else { | |
imagepng($png); | |
imagedestroy($png); | |
} | |
$image = ob_get_clean(); | |
return $image; | |
} |
I think something updated.
$barcodeData = $this->getBarcodeData($code, $type);
this function returning an object not array
One more thing in this object there is no
if ($bar['drawBar']) {
drawbar method. I just dump it that have only
#width: 1
#height: 1
#positionVertical: 0
#type: 1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what do i put for the type parameter