A few years ago I needed a way to generate barcodes on-the-fly using PHP, at the time there were several solutions available at a steep cost, so I wrote this script that generates barcodes in four barcode formats including Code 128, Code 39, Code 2of5, and Codabar. With a little over 100 lines of code you have the options of “vertical” or “horizontal” display, varying barcode heights, and one of four barcode formats. It does require the GD Library to be installed as a module in PHP.

This code is available for use under the MIT License

View the code here: https://github.com/davidscotttufts/php-barcode/

Example 1:


Text: “0” (Default)

Size: “20” (Default)

Code Type: “Code128” (Default)

Orientation: “Horizontal” (Default)

HTML Source Code:

<img alt="testing" src="/code/barcode.php" />



Example 2:


Text: “testing”

Size: “20” (Default)

Code Type: “Code128” (Default)

Orientation: “Horizontal” (Default)

Print: “true”

HTML Source Code:

<img alt="testing" src="/code/barcode.php?text=testing&print=true" />



Example 3:



Size: “40”

Code Type: “Code39”

Orientation: “Horizontal” (Default)

Print: “true”

HTML Source Code:

<img alt="TESTING" src="/code/barcode.php?codetype=Code39&size=40&text=TESTING&print=true" />



Example 4:


Text: “12345”

Size: “40”

Code Type: “Code25”

Orientation: “Horizontal” (Default)

HTML Source Code:

<img alt="12345" src="/code/barcode.php?codetype=Code25&size=40&text=12345" />



Example 5:


Text: “123ABC”

Size: “40”

Code Type: “Codabar”

Orientation: “Horizontal” (Default)

HTML Source Code:

<img alt="123ABC" src="/code/barcode.php?codetype=Codabar&size=40&text=123ABC" />



Example 6:


Text: “The Real David Tufts”

Size: “40”

Code Type: “Code128” (Default)

Orientation: “Vertical”

HTML Source Code:

<img alt="The Real David Tufts" src="/code/barcode.php?text=The%20Real%20David%20Tufts&orientation=vertical&size=40" />


The Real David Tufts

Please leave comments with additions or improvements to this script. One possible area of improvement for Code 128 is figuring out how to add keyboard commands (i.e. SHIFT, DEL, FNC 1, etc.) to the barcode.

Again, you can view the code here: https://github.com/davidscotttufts/php-barcode/


  1. Calos

    Hello, great examples. How would you make it so that the barcode is generated from a form. Or rather, have a user input the numbers and then the barcode generated from that?

    1. David Scott Tufts


      Here is a simple example of how to accomplish what you are looking for

      <form action=”http://www.davidscotttufts.com/code/barcode.php” method=”get” name=”barcode-form”>
      <input type=”text” id=”text” name=”text” value=”testing” />
      <input type=”submit” name=”barcode-button” />

      1. Robin Som

        nice sir ji i want to show this code (barcode.php)

      2. Brajbhooshan Singh Tomar

        thank you ….

  2. Essex web design

    Nice, going to give this a try.

  3. Nathans_iv

    Well done. I have been working with Oracle since the early days (circa 1988) and not seen any facility to produce barcodes with in Oracle. Now I am working with Oracle R12 E-Business an still see no improvement. I am hoping to get the barcoding integrated into producing barcoded letters. I am looking into adding UK Royal Mail postcode into each letter sent out to clients. Sadly Oracle have not made my job easy. Your work has given me some great Ideas. Thank you

  4. Jeff Richtman

    Is their a way to add the text “crc” under the barcode as well?

  5. new in php


    great code

    can i know the code to save image in a folder..not in a browser

  6. Faizur

    Hi, great post. Is there any way to put the alt value under the barcode? Thanks.

  7. Martin Boer

    Nice implementation without the need for any font files.

    To add text, use the GD library to inject text before the “// Draw barcode to the screen” comment. Or use the barcode image in a new GD image.

    To save the image to a folder, delete the row after the “// Draw barcode to the screen” comment. Then modify the imagepng() function on the next row, add a filename as second parameter.

  8. Ashok

    How to send the generated bar image to the godex barcode printer? Do anyone have any idea?

  9. Stephen K

    This worked like a charm!
    Thanks for the great work

  10. javed

    i apply the same concept I am not able to print this barcode in all browsers. what’s the reason?

  11. John Funk

    Great code, I will use this with FileMaker, inserting the resulting image into a container field using my web server for the PHP. The best part of this is there is no need to any plugins or fonts. I have years experience implementing barcodes and this is the simplest ever! THANKS

  12. nirav

    hi nice code its really helpfull to me

  13. Form Generator PHP Generator MySQL Generator

    Please update the USAGE syntax in the associated PHP file as the current syntax is incorrect. It is given as whereas it should have been You see the question mark (?) ? That’s what is important. You have correctly listed in your examples but it is incorrect in the source code of the file and someone who uses the code from the source file will NOT be able to run the code as is and get the output correctly. So please update the ampersand to question mark on line 9 in source code.


    1. David Scott Tufts

      fpmgonline, thanks for pointing that out, I have made the change.

      1. obaidul99

        Hello sir

        When i print this code then Barcode scanner can’t read it. But my barcode scanner read other barcode label. Please help.

  14. Andy

    Compliments, and thank you for this great barcode code, really simle and very effective.

    I have a queystion. How can i add the numbers and letters under the barcode. So the image will be complete, barcode image and barcode number.

    Thanks this is very helpfull

  15. Rijesh

    I need to add some other details along with the barcode. Such as barcode number, product weight and price. How can i do that.? How to edit this file to get an output as required?

  16. Rijesh

    Hii, This is very helpfull. But I have a question that how to print the barcode number and product price along with the barcode?

    1. daliainie

      Hi Rijesh. Have you find out how to do it? I am also seeking answer for the queston.

  17. Erwin

    Super cool men, this is what i was looking for ;)

  18. sanjay

    Thanks for code. It’s really very helpful for me. When i crate barcode with img src there is no name during barcode scanning. I want to print barcode with img src. Please help.

    1. David Scott Tufts

      Hi sanjay, I have not created any code yet for outputting the text, I don’t think it would be very difficult for you to add that functionality.

  19. Martin

    I normally don’t comment on sites but felt that you’ve given us a great piece of code here – and free of charge too. Many thanks. Works like a charm. To make sure it was working, I tested it with ‘QR Droid’ and it read it perfectly (obviously)

    1. David Scott Tufts

      Thanks Martin, glad to hear that you can use this code!

  20. Steve Vaughan

    This code works beautifully. Is there an easy way (i’m using code128) to increase the bar width? Two of my three scanners are having an issue and I think it’s the bar width that’s giving them a problem.

  21. Sastrawan Zakariya

    Really really good job… I’ll gve it a spin. thanks David