Create barcode with imagecreate and html

Asked

Viewed 811 times

0

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

Example of how this my page:

<?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);

2 answers

1

You can use the library https://github.com/picqer/php-barcode-generator, if you are using Composer just install in your project folder:

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: $generated = new Picqer\Barcode\BarcodeGeneratorJPG();
  • HTML: $generated = new Picqer\Barcode\BarcodeGeneratorHTML();

Accepted types of codes: https://github.com/picqer/php-barcode-generator#accepted-types


Alternatively you can use the TCPDF (php-Barcode is based on it) following the examples of https://github.com/tecnickcom/TCPDF/tree/master/examples/barcodes, no need to Composer, just download the TCPDF from the repository and do the include, thus:

<?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));

0

You could use the 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’s a solution to think about.

Can get here html2ps

There is this class here too, it makes it very easy to write barcode in image format Class Bar code

  • I understand, it’s a solution but I want something very simple with the code I posted.

  • 1

    Another solution is to write everything with php GD.

  • But how do I put other images? With it comes a barcode that is mounted to priests of 2 gif images. There is some example of how to do this?

  • @Hugoborges The easiest is to draw the code bars one by one with imagerectangle, in the same logic that you use to mount HTML.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.