← Go back to homepage

Convert Text to Barcode Image

Thanks to a skilled PHP programmer, the internet has a ready made PHP script that takes a text string and returns an image representation in barcode format.

Convert Text to Barcode Image

Here’s the complete script:

=1 to enable

    NOTE: You must have GD-1.8 or higher compiled into PHP
    in order to use PNG and JPEG. GIF images only work with
    GD-1.5 and lower. (http://www.boutell.com)


    ANOTHER NOTE: If you actually intend to print the barcodes 
    and scan them with a scanner, I highly recommend choosing 
    JPEG with a quality of 100. Most browsers can't seem to print 
    a PNG without mangling it beyond recognition. 


    USAGE EXAMPLES FOR ANY PLAIN OLD HTML DOCUMENT:
    -----------------------------------------------


    ![](barcode.php?barcode=HELLO&quality=75)


    ![](barcode.php?barcode=123456&width=320&height=200)
            
    

/ /=============================================================================*/

//----------------------------------------------------------------------------- // Startup code //-----------------------------------------------------------------------------

if(isset($_GET["text"])) $text=$_GET["text"]; if(isset($_GET["format"])) $format=$_GET["format"]; if(isset($_GET["quality"])) $quality=$_GET["quality"]; if(isset($_GET["width"])) $width=$_GET["width"]; if(isset($_GET["height"])) $height=$_GET["height"]; if(isset($_GET["type"])) $type=$_GET["type"]; if(isset($_GET["barcode"])) $barcode=$_GET["barcode"];

if (!isset ($text)) $text = 1; if (!isset ($type)) $type = 1; if (empty ($quality)) $quality = 100; if (empty ($width)) $width = 160; if (empty ($height)) $height = 80; if (!empty ($format)) $format = strtoupper ($format); else $format="PNG";

switch ($type) { default: $type = 1; case 1: Barcode39 ($barcode, $width, $height, $quality, $format, $text); break;
}

//----------------------------------------------------------------------------- // Generate a Code 3 of 9 barcode //----------------------------------------------------------------------------- function Barcode39 ($barcode, $width, $height, $quality, $format, $text) { switch ($format) { default: $format = "JPEG"; case "JPEG": header ("Content-type: image/jpeg"); break; case "PNG": header ("Content-type: image/png"); break; case "GIF": header ("Content-type: image/gif"); break; }

    $im = ImageCreate ($width, $height)
or die ("Cannot Initialize new GD image stream");
    $White = ImageColorAllocate ($im, 255, 255, 255);
    $Black = ImageColorAllocate ($im, 0, 0, 0);
    //ImageColorTransparent ($im, $White);
    ImageInterLace ($im, 1);


    $NarrowRatio = 20;
    $WideRatio = 55;
    $QuietRatio = 35;


    $nChars = (strlen($barcode)+2) * ((6 * $NarrowRatio) + (3 * $WideRatio) + ($QuietRatio));
    $Pixels = $width / $nChars;
    $NarrowBar = (int)(20 * $Pixels);
    $WideBar = (int)(55 * $Pixels);
    $QuietBar = (int)(35 * $Pixels);


    $ActualWidth = (($NarrowBar * 6) + ($WideBar*3) + $QuietBar) * (strlen ($barcode)+2);
    
    if (($NarrowBar == 0) || ($NarrowBar == $WideBar) || ($NarrowBar == $QuietBar) || ($WideBar == 0) || ($WideBar == $QuietBar) || ($QuietBar == 0))
    {
            ImageString ($im, 1, 0, 0, "Image is too small!", $Black);
            OutputImage ($im, $format, $quality);
            exit;
    }
    
    $CurrentBarX = (int)(($width - $ActualWidth) / 2);
    $Color = $White;
    $BarcodeFull = "*".strtoupper ($barcode)."*";
    settype ($BarcodeFull, "string");
    
    $FontNum = 3;
    $FontHeight = ImageFontHeight ($FontNum);
    $FontWidth = ImageFontWidth ($FontNum);
    if ($text != 0)
    {
            $CenterLoc = (int)(($width-1) / 2) - (int)(($FontWidth * strlen($BarcodeFull)) / 2);
            ImageString ($im, $FontNum, $CenterLoc, $height-$FontHeight, "$BarcodeFull", $Black);
    }
    else
    {
        $FontHeight=-2;
    }


    for ($i=0; $i<strlen($BarcodeFull); $i++)
    {
            $StripeCode = Code39 ($BarcodeFull[$i]);


            for ($n=0; $n 
← Go back to homepage