Create barcode with imagecreate and html

0

I have a page that mounts a barcode, I want to know how to do it instead of returning an html page, it returns an image.

Example of how my page is:

<?php

$fino = 1;
$largo = 3;
$altura = 50;
$text = null;

$barcodes[0] = '00110';
$barcodes[1] = '10001';
$barcodes[2] = '01001';
$barcodes[3] = '11000';
$barcodes[4] = '00101';
$barcodes[5] = '10100';
$barcodes[6] = '01100';
$barcodes[7] = '00011';
$barcodes[8] = '10010';
$barcodes[9] = '01010';

for ($f1 = 9; $f1 >= 0; $f1--) {
    for ($f2 = 9; $f2 >= 0; $f2--) {
        $f = ($f1 * 10) + $f2;
        $texto = '';
        for ($i = 1; $i < 6; $i++) {
            $texto .= substr($barcodes[$f1], ($i - 1), 1) . substr($barcodes[$f2], ($i - 1), 1);
        }
        $barcodes[$f] = $texto;
    }
}

$text .= '<img src="../Img/p.gif" style="width: ' . $fino . 'px; height: ' . $altura . 'px; border: 0; padding: 0; margin: 0;">';
$text .= '<img src="../Img/b.gif" style="width: ' . $fino . 'px; height: ' . $altura . 'px; border: 0; padding: 0; margin: 0;">';
$text .= '<img src="../Img/p.gif" style="width: ' . $fino . 'px; height: ' . $altura . 'px; border: 0; padding: 0; margin: 0;">';
$text .= '<img src="../Img/b.gif" style="width: ' . $fino . 'px; height: ' . $altura . 'px; border: 0; padding: 0; margin: 0;">';
$text .= '<img ';

// Recebe os dados
$texto = addslashes(filter_input(INPUT_GET, 'linha', FILTER_SANITIZE_NUMBER_INT));

if ((strlen($texto) % 2) <> 0) {
    $texto = '0' . $texto;
}

while (strlen($texto) > 0) {
    $i = round(substr($texto, 0, 2));
    $texto = substr($texto, strlen($texto) - (strlen($texto) - 2), (strlen($texto) - 2));

    if (isset($barcodes[$i])) {
        $f = $barcodes[$i];
    }

    for ($i = 1; $i < 11; $i += 2) {
        if (substr($f, ($i - 1), 1) == '0') {
            $f1 = $fino;
        } else {
            $f1 = $largo;
        }

        $text .= 'src="../Img/p.gif" style="width: ' . $f1 . 'px; height: ' . $altura . 'px; border: 0; padding: 0; margin: 0;">';
        $text .= '<img ';

        if (substr($f, $i, 1) == '0') {
            $f2 = $fino;
        } else {
            $f2 = $largo;
        }

        $text .= 'src="../Img/b.gif" style="width: ' . $f2 . 'px; height: ' . $altura . 'px; border: 0; padding: 0; margin: 0;">';
        $text .= '<img ';
    }
}
$text .= 'src="../Img/p.gif" style="width: ' . $largo . 'px; height: ' . $altura . 'px; border: 0; padding: 0; margin: 0;">';
$text .= '<img src="../Img/b.gif" style="width: ' . $fino . 'px; height: ' . $altura . 'px; border: 0; padding: 0; margin: 0;">';
$text .= '<img src="../Img/p.gif" style="width: 1px; height: ' . $altura . 'px; border: 0; padding: 0; margin: 0;">';



$img = imagecreate(410, 55);

// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);

// Red text
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, 5, 0, 0, $text, $red);

header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
    
asked by anonymous 04.08.2017 / 19:48

2 answers

1

You can use the link library, if you are using composer just install it in the folder of your project:

composer require picqer/php-barcode-generator

Generating a .png :

$barcodes = '000101010100010';

$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
$generated = $generator->getBarcode($barcodes, $generator::TYPE_CODE_128);

Other formats:

  • SVG: $generated = new Picqer\Barcode\BarcodeGeneratorSVG();
  • JPEG:% with%
  • HTML: $generated = new Picqer\Barcode\BarcodeGeneratorJPG();

Accepted code types: link

Alternatively you can use TCPDF (the php-barcode is based on it) by following the examples from link , you do not need composer, just download the TCPDF from the repository and do $generated = new Picqer\Barcode\BarcodeGeneratorHTML(); , like this:

<?php
require_once 'tcpdf_barcodes_1d_include.php';

$barcodeobj = new TCPDFBarcode('http://www.tcpdf.org', 'C128');

// exibe no navegador a imagem
$barcodeobj->getBarcodePNG(2, 30, array(0,0,0));
    
04.08.2017 / 20:41
0

You could use html2ps is the solution I know to generate an image of your html.

 html2ps index.html index.ps 
 convert index.ps index.png 

You will need to install on your server and run with exec in the background but it is a solution to think about.

You can get here html2ps

There is this class here too, it makes it much easier to write the barcode in image format Class Barcode

    
04.08.2017 / 20:04