Barcode in PHP
An example how to use StrokeScribe barcode generator to produce barcodes in PHP. Barcodes shown here are GIF images.
How to Proceed
- Download and install the barcode generator on your web server.
- Create a php file in your website folder.
- Paste the following PHP code into the file.
- Open the page in a browser.
You may also need to enable the COM extension in the php.ini (see more info in the PHP:COM Manual):
[PHP_COM_DOTNET]
extension=php_com_dotnet.dll
extension_dir = "ext"
The Code Example
<?php
$ss = new COM("STROKESCRIBE.StrokeScribeClass.1");
$ss->Text = "ABCD123";
$ss->Alphabet = 25; //25=QRCODE
$w = $ss->BitmapW;
$h = $ss->BitmapH;
$arr = $ss->GetPictureArray(1, $w*5, $h*5); //1=GIF
if($ss->Error)
{
echo $ss->ErrorDescription;
return;
}
header("Content-Type: image/gif");
foreach ($arr as $byte)
{
echo (chr($byte));
}
?>
Some words about choosing the image's width/height:
- The BitmapW/BitmapH properties show the minimum picture size that fits the barcode.
- For square barcodes (QR Code, Data Matrix), specify the same value for width and height: GetPictureArray(1, w, w) or GetPictureArray(1, h, h).
- For linear barcodes (CODE-128, EAN-13), you need to use the value received from BitmapW (and it's multiplies) for the picture width and absolutely any value for height: GetPictureArray(1, barcode.BitmapW*3, 100).
Code Snippets by Barcode Type
All Barcode Types
How to Encode a Byte Array
$ss->CodePage = -1;
$a = array(0xce, 0x01, 0x02, 0x03);
$ss->Text = $a;
Mobile-friendly Barcodes with National Characters
The first way is to create/edit your php file in UTF-8:
$ss = new COM("STROKESCRIBE.StrokeScribeClass.1", NULL, CP_UTF8); //tell the COM library you are using UTF-8 in your data strings
//You also may use the com.code_page ini setting.
$ss->Alphabet = 25;
$ss->CodePage = 65001; // = UTF-8
$ss->Text = "ΞΟΠΡΣΤΥΦ"; //directly use UTF-8 in you PHP code
The second way is to pass a pre-encoded data in a byte array:
$ss = new COM("STROKESCRIBE.StrokeScribeClass.1");
$ss->Alphabet = 25;
$ss->CodePage = -1; //disables code page translation"
$a = array(0xce, 0x9e, 0xce, 0x9f, 0xce, 0xa0, 0xce, 0xa1, 0xce, 0xa3, 0xce, 0xa4, 0xce, 0xa5, 0xce, 0xa6);
$ss->Text = $a;
CODE 128
To create a Code 128, modify your code in the following way:
$ss = new COM("STROKESCRIBE.StrokeScribeClass.1");
$ss->Alphabet = 5; //CODE128
$ss->Text = "ABCDEF";
//if you want to specify an alternative text below the barcode:
$ss->TextBelow = "AB CD EF";
$w = $ss->BitmapW;
$h = $ss->BitmapH;
$arr = $ss->GetPictureArray(1, $w*2, $w); //1=GIF
QR Code
To create a QR Code, modify your code in the following way:
$ss->Alphabet = 25;
$ss->Text = "ABCDEF";
How to Modify the Error Correction Level
$ss->QrECL = 2;
How to Force the Minimum Matrix Size
$ss->QrMinVersion = 11;
PDF417
To create a PDF417 barcode, modify your code in the following way:
$ss->Alphabet = 6; //PDF417
$ss->Text = "123ABC";
$w = $ss->BitmapW;
$h = $ss->BitmapH;
$arr = $ss->GetPictureArray(1, $w*2, $w); //1=GIF
To create a Compact PDF417:
$ss->Alphabet = 6;
$ss->CompactPDF417 = true;
How to Set the Non-default Error Correction Level
$ss->PDF417ErrLevel = 2;
How to Fix the Number of Rows or Columns
$ss->PDF417Cols = 5;
$ss->PDF417Rows = 10;
DATA MATRIX
To create a Data Matrix barcode, modify your code in the following way:
$ss->Alphabet = 8; //DATAMATRIX
$ss->Text = "ABCDEF";
$ss->DataMatrixMinSize = 132;
$w = $ss->BitmapW;
$h = $ss->BitmapH;
$arr = $ss->GetPictureArray(1, $w, $h);
How to Force the Minimum Matrix Size
$ss->DataMatrixMinSize = 132;
How to Modify the Quiet Zone Size
$ss->QuietZone2D = 0;
© 2026 StrokeScribe. All rights reserved. Use of any portion of this site constitutes acceptance of our Terms of Use and Privacy Policy. The website material may not be reproduced, except with the prior written permission.