1

Has anybody had any experience with generating 2D Barcodes for Royal Mail via PHP? I've spent a while attempting to get my own routines to write a valid datamatrix sadly to no avail.

I do have working conversion routines for ASCII to C40 and Luhn 16 checksum makers but just can't get anywhere with the graphical representation, or the ECC200 byte creation for that matter.

Are there any pre-written libraries out there with documentation that would help take away a lot of further legwork?

I do need to be able to generate this within the server environment, without using external sites ofr image generation ideally.

RefreshCarts
  • 126
  • 1
  • 10

2 Answers2

1

We use Zint Barcode Generator Unix packages for QR and PDF417 code generation. Royal Mail is supported as well. (on CentOS dnf install zint, Ubuntu takes more work).

Zint documentation: http://www.zint.org.uk/

In PHP use the system method, example:

$targetFilePath = dirname(__FILE__).'/test.png';
$contents = 'ABC123';
system('zint ...params... -o"' . $targetFilePath . '" -d"' . $contents . '"');
var_dump(file_exists($targetFilePath));

It will generate an image on the requested $targetFilePath.

Wesley Abbenhuis
  • 692
  • 13
  • 19
  • Ohh, fantstic news! Thanks DiceXQ for the info, that looks so much simpler than the god knows how many lines I've written to... somewhat unsuccessfully, re-invent the wheel! I'll forward this onto our server bods in a moment or two, thanks again! Will update with progress for anyone else who has this difficulty :) – RefreshCarts Apr 12 '16 at 14:56
  • 1
    Yup, to confirm, this answer solved the problem and I can now generate 2D barcodes with a lot more ease than I was expecting! – RefreshCarts Apr 12 '16 at 15:52
0

For ECC200 Datamatrix Generation in PHP we successfully used:

sudo apt install dmtx-utils

To output a PNG file from the server, with normal apache2 settings you would get the barcode in PNG when you enter in the browser: http://yourserver.com/datamatrix/?in=yourbarcodetext

<?php
ob_start();
$old_path = getcwd();

$infile = "/var/www/html/datamatrix/message2.txt";
$image = "/var/www/html/datamatrix/image.png";

file_put_contents($infile,$_GET["in"]);

$ex = "export HOME=/tmp && /usr/bin/dmtxwrite {$infile} -o {$image}";

echo "<b>$ex</b>";
$output = shell_exec($ex);

echo var_export($output, TRUE);

echo "done";
chdir($old_path);

$im = imagecreatefrompng($image);

ob_end_clean();
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);