Tuesday 25 September 2018

How to generate barcode using PHP

Nowadays, the barcode is a common thing for any product tagged with its body. Barcode is used to store various kind of data such as product code, price, manufacturer details and so on. So, in this tutorial, I will show you how to generate barcode using PHP. The following example of PHP barcode generator is very easy to learn and implement. You may also like to read – How to generate QR code using PHP and Ajax.

File Structure

Before proceeding, have a look at the file structure. Apart from bootstrap folder this example contains only two files –
  • index.php
  • barcode.php

Barcode Generator Library (barcode.php)

In order to generate barcode using PHP, you need to download a PHP library from Github and place it within your project folder. But you don’t need to do that because it is already included in the complete source code. Download it from the below Download link.

Different Parameters to Generate Barcode

  • String: Whatever you want
  • Code Type:
    • Codabar
    • Code128 (Default)
    • Code2of5
    • Code39
  • Orientation: 
    • Horizontal (Default)
    • Vertical
  • Size:
    • 10 (Minimum)
    • 20 (Default)
    • 400 (Maximum)
  • Print:
    • True (Display the string below the barcode)
    • False

HTML – Barcode Generator Form (index.php)

  1. <div class="container-fluid">
  2. <div class="row">
  3. <div class="col-md-12">
  4. <h1 class="top-margin">Read the full article on -- <a href="http://www.mitrajit.com/generate-barcode-using-php/" target="_blank">How to generate barcode using PHP</a> in <a href="http://www.mitrajit.com/">Mitrajit's Tech Blog</a></h1>
  5. </div>
  6. </div>
  7. <form class="form-horizontal" method="POST" id="barcodeForm">
  8. <div class="row">
  9. <div class="col-md-12">
  10. <div class="form-group">
  11. <label>String</label>
  12. <input type="text" name="string" class="form-control" value="">
  13. </div>
  14. </div>
  15. </div>
  16. <div class="row">
  17. <div class="col-md-3">
  18. <div class="form-group">
  19. <label>Code Type</label>
  20. <select name="type" id="type" class="form-control">
  21. <option value="codebar">Codebar</option>
  22. <option value="code128" selected="selected">Code128</option>
  23. <option value="code2of5">Code2of5</option>
  24. <option value="code39">Code39</option>
  25. </select>
  26. </div>
  27. </div>
  28. <div class="col-md-3">
  29. <div class="form-group">
  30. <label>Orientation</label>
  31. <select name="orientation" class="form-control" required>
  32. <option value="horizontal" selected="selected">Horizontal</option>
  33. <option value="vertical">Vertical</option>
  34. </select>
  35. </div>
  36. </div>
  37. <div class="col-md-3">
  38. <div class="form-group">
  39. <label>Size</label>
  40. <input type="number" name="size" id="size" class="form-control" min="10" max="400" step="10" value="20" required>
  41. </div>
  42. </div>
  43. <div class="col-md-3">
  44. <div class="form-group">
  45. <label>Print</label>
  46. <select name="print" id="print" class="form-control" required>
  47. <option value="true" selected="selected">True</option>
  48. <option value="false">False</option>
  49. </select>
  50. </div>
  51. </div>
  52. </div>
  53. <div class="row text-center">
  54. <div class="col-md-12">
  55. <input type="submit" name="submit" class="btn btn-success text-center form-controll" id="" value="Generate Barcode">
  56. </div>
  57. </div>
  58. </form>
  59. </div>
The above HTML code for Barcode generator form is looking huge but don’t be afraid, it is simply a form using bootstrap.

PHP – Generator Barcode (index.php)

  1. <?php
  2. if(isset($_POST['submit'])) {
  3. $string = trim($_POST['string']);
  4. $type=$_POST['type'];
  5. $orientation=$_POST['orientation'];
  6. $size=$_POST['size'];
  7. $print=$_POST['print'];
  8. if($string != '') {
  9. echo '<h5>Generated Barcode</h5>';
  10. echo '<img class="barcode" alt="'.$string.'" src="barcode.php?text='.$string.'&codetype='.$type.'&orientation='.$orientation.'&size='.$size.'&print='.$print.'"/>';
  11. } else {
  12. echo 'Please enter a string!';
  13. }
  14. }
  15. ?>
Pass the form data as URL parameters to generate the barcode as an image.

Live demo and complete source code on Generate Barcode using PHP

Try the live demo on how to generate barcode using PHP by clicking on the Demo link. Download the complete source code from the below Download link. Please like and share this tutorial with others.



1 comment:

  1. This requires the use of barcode generate Asp.net technology, the author introduced in great detail

    ReplyDelete