How to generate Bar Code with PHP
Most of the time while developing e-commerce applications we need to generate Bar Code for items we are selling, we need to create a script which can generate bar codes for all items or products, most commonly it holds item number which is primary key of that item in our database, but we can also store other information too.
I have also seen Bar Codes on employee cards, cold drinks, item tags etc. I don’t know in detail about its different type but a basic demonstration is given below for how to generate Bar Code with PHP.
Here is the live demo of generate any barcode online Generate bar code
How to generate Bar Code
We need to download a php class from Github, download barcode.php file and place it your project directory.
in your file write an image tag and in src attribute type barcode.php as below.
1 |
<img alt="my bar code" src="path/to/barcode.php" /> |
Different parameters of Bar Code
- Text: “Coding-sips”
- Size: “20” (Default)
- Code Type: “Code128” (Default)
- Code 128
- Code 39
- Code 2of5
- Codabar
- Orientation: “Horizontal” (Default)
- Print: “true” show the text blow barcode
Examples
Make sure your barcode.php path is correct, the following examples shows that barcode.php resides on the same path where we are pasting these example codes.
If you do not want to download barcode.php your can try this URL http://davidscotttufts.com/code/barcode.php just for testing purpose but for real application you must have to download barcode.php from link provided above.
1 |
<img alt="Coding Sips" src="barcode.php?text=Coding-sips-item-no-786&print=true" /> |
1 |
<img alt="Coding Sips" src="barcode.php?codetype=Code39&size=40&text=Coding-sips-item-no-786&print=true" /> |
1 |
<img alt="Coding Sips" src="barcode.php?text=Coding-sips-item-no-786&orientation=vertical&size=40" /> |
To generate Bar Codes for all items in our shop database we need to loop through all items we want to generate Bar code for, And echo and image for each item then cut and stick it on the item box or tag.
We should also write another script where we can generate Bar Code for separate items one by one.
You can try other other examples changing listed parameters above. Let me know if you have any issue via comments below, help me and other developers if you have some other optimized solution.
Comments
This is an easy to use, non-bloated, framework independent, barcode generator in PHP.
This is an easy to use, non-bloated, framework independent, barcode generator in PHP.
When Printing through Javascript window.open barcode is not printing
I am very happy to use it. you make it very easy. thanks.
I was looking for some information on TOPIC today when i came across your web page. Great stuff!
Hope it helps users.
Thanks
how to change text size?
Barcode generated but output incorrect (Not scan)