Do you want to be a good Web Developer?

Enter Your Email To Get Our Exclusive Tutorials Right In Your Email

Join TalkersCode Community And Recieve Exclusive Tutorials
On Web Development In Your Email

Generate Barcode Using PHP

Tuesday, Jan 24, 2017

Tags:- PHP HTML


A barcode is an optical, machine-readable, representation of data, the data usually describes something about the object that carries the barcode.Barcodes is used to store any kind of data of any object like product price, date of manufacture and other product related details. In this tutorial we will show you how to generate barcode using PHP.You may also like generate qr code using PHP.


See Demo
Generate Barcode Using PHP


To Generate Barcode It Takes Only Three Steps:-

  1. Make a HTML file and define markup
  2. Make a PHP file to generate barcode
  3. Make a CSS file and define styling


Step 1.Make a HTML file and define markup

We make a HTML file and save it with a name barcode.html

<html>
<head>
<link href="barcode_style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="wrapper">

<div id="barcode_div">
 <form method="post" action="generate_barcode.php">
  <input type="text" name="barcode_text">
  <input type="submit" name="generate_barcode" value="GENERATE">
 </form>
</div>

</div>
</body>
</html>

In this step we create a form to enter text which is going to be stored in barcode.



Step 2.Make a PHP file to generate barcode

We make a PHP file and save it with a name generate_barcode.php

<?php
if(isset($_POST['generate_barcode']))
{
 $text=$_POST['barcode_text'];
 echo "<img alt='testing' src='barcode/barcode.php?codetype=Code39&size=40&text=".$text."&print=true'/>";
}
?>

To generate barcode you have to download PHP - Barcode library.In this step we get the text entered by user to store in barcode and then we just send the data to barcode.php file which we downloaded to generate barcode and then display the barcode image.For more details of this library functions and query string variable visit there website.



Step 3.Make a CSS file and define styling

We make a CSS file and save it with a name barcode_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#F5EEF8;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#884EA0;
}
#wrapper h1 p
{
 font-size:18px;
}
#barcode_div input[type="text"]
{
 width:300px;
 height:35px;
 border:none;
 padding-left:10px;
 font-size:17px;
}
#barcode_div input[type="submit"]
{
 background-color:#884EA0;
 border:none;
 height:35px;
 color:white;
}

That's all, this is how to generate barcode using PHP and HTML.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email