<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>barcode in php Archives - Cody Paste</title>
	<atom:link href="https://www.codypaste.com/tag/barcode-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codypaste.com/tag/barcode-in-php/</link>
	<description>THE WEB PLAYGROUND</description>
	<lastBuildDate>Wed, 19 Apr 2023 12:02:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.codypaste.com/wp-content/uploads/2022/12/cropped-codypaste-32x32.png</url>
	<title>barcode in php Archives - Cody Paste</title>
	<link>https://www.codypaste.com/tag/barcode-in-php/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Generate Barcode in PHP as Image</title>
		<link>https://www.codypaste.com/generate-barcode-in-php-as-image/</link>
					<comments>https://www.codypaste.com/generate-barcode-in-php-as-image/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[Admin]]></dc:creator>
		<pubDate>Tue, 27 Dec 2022 09:22:46 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[barcode in php]]></category>
		<category><![CDATA[generate barcode in php]]></category>
		<category><![CDATA[PHP Barcode]]></category>
		<guid isPermaLink="false">https://www.codypaste.com/?p=816</guid>

					<description><![CDATA[<p>Generating bar code in PHP is very simple, You need a barcode.php library for this. Follow the steps given below. Step 1: Download barcode library OR Create a file barcode.php and paste code given below [crayon-6985e8374b431310114441/] &#160; &#160; Step 2: Write code in your PHP file, where you want to display Barcode. Example given below. &#8230; </p>
<p class="link-more"><a href="https://www.codypaste.com/generate-barcode-in-php-as-image/" class="more-link">read more<span class="screen-reader-text"> "Generate Barcode in PHP as Image"</span></a></p>
<p>The post <a href="https://www.codypaste.com/generate-barcode-in-php-as-image/">Generate Barcode in PHP as Image</a> appeared first on <a href="https://www.codypaste.com">Cody Paste</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Generating bar code in PHP is very simple, You need a barcode.php library for this. Follow the steps given below.</p>
<p><strong>Step 1:</strong><br />
<span style="color: #ff6600;"><strong><a style="color: #ff6600;" href="https://github.com/davidscotttufts/php-barcode/" target="_blank" rel="noopener">Download barcode</a></strong></span> library</p>
<p>OR</p>
<p>Create a file <strong>barcode.php</strong> and paste code given below</p><pre class="urvanov-syntax-highlighter-plain-tag">&lt;?php

/*
 *  Author	David S. Tufts
 *  Company	davidscotttufts.com
 *	  
 *  Date:	05/25/2003
 *  Usage:	&lt;img src="/barcode.php?text=testing" alt="testing" /&gt;
 */

// For demonstration purposes, get pararameters that are passed in through $_GET or set to the default value
$filepath = (isset($_GET["filepath"])?$_GET["filepath"]:"");
$text = (isset($_GET["text"])?$_GET["text"]:"0");
$size = (isset($_GET["size"])?$_GET["size"]:"20");
$orientation = (isset($_GET["orientation"])?$_GET["orientation"]:"horizontal");
$code_type = (isset($_GET["codetype"])?$_GET["codetype"]:"code128");
$print = (isset($_GET["print"])&amp;&amp;$_GET["print"]=='true'?true:false);
$sizefactor = (isset($_GET["sizefactor"])?$_GET["sizefactor"]:"1");

// This function call can be copied into your project and can be made from anywhere in your code
barcode( $filepath, $text, $size, $orientation, $code_type, $print, $sizefactor );

function barcode( $filepath="", $text="0", $size="20", $orientation="horizontal", $code_type="code128", $print=false, $SizeFactor=1 ) {
	$code_string = "";
	// Translate the $text into barcode the correct $code_type
	if ( in_array(strtolower($code_type), array("code128", "code128b")) ) {
		$chksum = 104;
		// Must not change order of array elements as the checksum depends on the array's key to validate final code
		$code_array = array(" "=&gt;"212222","!"=&gt;"222122","\""=&gt;"222221","#"=&gt;"121223","$"=&gt;"121322","%"=&gt;"131222","&amp;"=&gt;"122213","'"=&gt;"122312","("=&gt;"132212",")"=&gt;"221213","*"=&gt;"221312","+"=&gt;"231212",","=&gt;"112232","-"=&gt;"122132","."=&gt;"122231","/"=&gt;"113222","0"=&gt;"123122","1"=&gt;"123221","2"=&gt;"223211","3"=&gt;"221132","4"=&gt;"221231","5"=&gt;"213212","6"=&gt;"223112","7"=&gt;"312131","8"=&gt;"311222","9"=&gt;"321122",":"=&gt;"321221",";"=&gt;"312212","&lt;"=&gt;"322112","="=&gt;"322211","&gt;"=&gt;"212123","?"=&gt;"212321","@"=&gt;"232121","A"=&gt;"111323","B"=&gt;"131123","C"=&gt;"131321","D"=&gt;"112313","E"=&gt;"132113","F"=&gt;"132311","G"=&gt;"211313","H"=&gt;"231113","I"=&gt;"231311","J"=&gt;"112133","K"=&gt;"112331","L"=&gt;"132131","M"=&gt;"113123","N"=&gt;"113321","O"=&gt;"133121","P"=&gt;"313121","Q"=&gt;"211331","R"=&gt;"231131","S"=&gt;"213113","T"=&gt;"213311","U"=&gt;"213131","V"=&gt;"311123","W"=&gt;"311321","X"=&gt;"331121","Y"=&gt;"312113","Z"=&gt;"312311","["=&gt;"332111","\\"=&gt;"314111","]"=&gt;"221411","^"=&gt;"431111","_"=&gt;"111224","`"=&gt;"111422","a"=&gt;"121124","b"=&gt;"121421","c"=&gt;"141122","d"=&gt;"141221","e"=&gt;"112214","f"=&gt;"112412","g"=&gt;"122114","h"=&gt;"122411","i"=&gt;"142112","j"=&gt;"142211","k"=&gt;"241211","l"=&gt;"221114","m"=&gt;"413111","n"=&gt;"241112","o"=&gt;"134111","p"=&gt;"111242","q"=&gt;"121142","r"=&gt;"121241","s"=&gt;"114212","t"=&gt;"124112","u"=&gt;"124211","v"=&gt;"411212","w"=&gt;"421112","x"=&gt;"421211","y"=&gt;"212141","z"=&gt;"214121","{"=&gt;"412121","|"=&gt;"111143","}"=&gt;"111341","~"=&gt;"131141","DEL"=&gt;"114113","FNC 3"=&gt;"114311","FNC 2"=&gt;"411113","SHIFT"=&gt;"411311","CODE C"=&gt;"113141","FNC 4"=&gt;"114131","CODE A"=&gt;"311141","FNC 1"=&gt;"411131","Start A"=&gt;"211412","Start B"=&gt;"211214","Start C"=&gt;"211232","Stop"=&gt;"2331112");
		$code_keys = array_keys($code_array);
		$code_values = array_flip($code_keys);
		for ( $X = 1; $X &lt;= strlen($text); $X++ ) {
			$activeKey = substr( $text, ($X-1), 1);
			$code_string .= $code_array[$activeKey];
			$chksum=($chksum + ($code_values[$activeKey] * $X));
		}
		$code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];

		$code_string = "211214" . $code_string . "2331112";
	} elseif ( strtolower($code_type) == "code128a" ) {
		$chksum = 103;
		$text = strtoupper($text); // Code 128A doesn't support lower case
		// Must not change order of array elements as the checksum depends on the array's key to validate final code
		$code_array = array(" "=&gt;"212222","!"=&gt;"222122","\""=&gt;"222221","#"=&gt;"121223","$"=&gt;"121322","%"=&gt;"131222","&amp;"=&gt;"122213","'"=&gt;"122312","("=&gt;"132212",")"=&gt;"221213","*"=&gt;"221312","+"=&gt;"231212",","=&gt;"112232","-"=&gt;"122132","."=&gt;"122231","/"=&gt;"113222","0"=&gt;"123122","1"=&gt;"123221","2"=&gt;"223211","3"=&gt;"221132","4"=&gt;"221231","5"=&gt;"213212","6"=&gt;"223112","7"=&gt;"312131","8"=&gt;"311222","9"=&gt;"321122",":"=&gt;"321221",";"=&gt;"312212","&lt;"=&gt;"322112","="=&gt;"322211","&gt;"=&gt;"212123","?"=&gt;"212321","@"=&gt;"232121","A"=&gt;"111323","B"=&gt;"131123","C"=&gt;"131321","D"=&gt;"112313","E"=&gt;"132113","F"=&gt;"132311","G"=&gt;"211313","H"=&gt;"231113","I"=&gt;"231311","J"=&gt;"112133","K"=&gt;"112331","L"=&gt;"132131","M"=&gt;"113123","N"=&gt;"113321","O"=&gt;"133121","P"=&gt;"313121","Q"=&gt;"211331","R"=&gt;"231131","S"=&gt;"213113","T"=&gt;"213311","U"=&gt;"213131","V"=&gt;"311123","W"=&gt;"311321","X"=&gt;"331121","Y"=&gt;"312113","Z"=&gt;"312311","["=&gt;"332111","\\"=&gt;"314111","]"=&gt;"221411","^"=&gt;"431111","_"=&gt;"111224","NUL"=&gt;"111422","SOH"=&gt;"121124","STX"=&gt;"121421","ETX"=&gt;"141122","EOT"=&gt;"141221","ENQ"=&gt;"112214","ACK"=&gt;"112412","BEL"=&gt;"122114","BS"=&gt;"122411","HT"=&gt;"142112","LF"=&gt;"142211","VT"=&gt;"241211","FF"=&gt;"221114","CR"=&gt;"413111","SO"=&gt;"241112","SI"=&gt;"134111","DLE"=&gt;"111242","DC1"=&gt;"121142","DC2"=&gt;"121241","DC3"=&gt;"114212","DC4"=&gt;"124112","NAK"=&gt;"124211","SYN"=&gt;"411212","ETB"=&gt;"421112","CAN"=&gt;"421211","EM"=&gt;"212141","SUB"=&gt;"214121","ESC"=&gt;"412121","FS"=&gt;"111143","GS"=&gt;"111341","RS"=&gt;"131141","US"=&gt;"114113","FNC 3"=&gt;"114311","FNC 2"=&gt;"411113","SHIFT"=&gt;"411311","CODE C"=&gt;"113141","CODE B"=&gt;"114131","FNC 4"=&gt;"311141","FNC 1"=&gt;"411131","Start A"=&gt;"211412","Start B"=&gt;"211214","Start C"=&gt;"211232","Stop"=&gt;"2331112");
		$code_keys = array_keys($code_array);
		$code_values = array_flip($code_keys);
		for ( $X = 1; $X &lt;= strlen($text); $X++ ) {
			$activeKey = substr( $text, ($X-1), 1);
			$code_string .= $code_array[$activeKey];
			$chksum=($chksum + ($code_values[$activeKey] * $X));
		}
		$code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];

		$code_string = "211412" . $code_string . "2331112";
	} elseif ( strtolower($code_type) == "code39" ) {
		$code_array = array("0"=&gt;"111221211","1"=&gt;"211211112","2"=&gt;"112211112","3"=&gt;"212211111","4"=&gt;"111221112","5"=&gt;"211221111","6"=&gt;"112221111","7"=&gt;"111211212","8"=&gt;"211211211","9"=&gt;"112211211","A"=&gt;"211112112","B"=&gt;"112112112","C"=&gt;"212112111","D"=&gt;"111122112","E"=&gt;"211122111","F"=&gt;"112122111","G"=&gt;"111112212","H"=&gt;"211112211","I"=&gt;"112112211","J"=&gt;"111122211","K"=&gt;"211111122","L"=&gt;"112111122","M"=&gt;"212111121","N"=&gt;"111121122","O"=&gt;"211121121","P"=&gt;"112121121","Q"=&gt;"111111222","R"=&gt;"211111221","S"=&gt;"112111221","T"=&gt;"111121221","U"=&gt;"221111112","V"=&gt;"122111112","W"=&gt;"222111111","X"=&gt;"121121112","Y"=&gt;"221121111","Z"=&gt;"122121111","-"=&gt;"121111212","."=&gt;"221111211"," "=&gt;"122111211","$"=&gt;"121212111","/"=&gt;"121211121","+"=&gt;"121112121","%"=&gt;"111212121","*"=&gt;"121121211");

		// Convert to uppercase
		$upper_text = strtoupper($text);

		for ( $X = 1; $X&lt;=strlen($upper_text); $X++ ) {
			$code_string .= $code_array[substr( $upper_text, ($X-1), 1)] . "1";
		}

		$code_string = "1211212111" . $code_string . "121121211";
	} elseif ( strtolower($code_type) == "code25" ) {
		$code_array1 = array("1","2","3","4","5","6","7","8","9","0");
		$code_array2 = array("3-1-1-1-3","1-3-1-1-3","3-3-1-1-1","1-1-3-1-3","3-1-3-1-1","1-3-3-1-1","1-1-1-3-3","3-1-1-3-1","1-3-1-3-1","1-1-3-3-1");

		for ( $X = 1; $X &lt;= strlen($text); $X++ ) {
			for ( $Y = 0; $Y &lt; count($code_array1); $Y++ ) {
				if ( substr($text, ($X-1), 1) == $code_array1[$Y] )
					$temp[$X] = $code_array2[$Y];
			}
		}

		for ( $X=1; $X&lt;=strlen($text); $X+=2 ) {
			if ( isset($temp[$X]) &amp;&amp; isset($temp[($X + 1)]) ) {
				$temp1 = explode( "-", $temp[$X] );
				$temp2 = explode( "-", $temp[($X + 1)] );
				for ( $Y = 0; $Y &lt; count($temp1); $Y++ )
					$code_string .= $temp1[$Y] . $temp2[$Y];
			}
		}

		$code_string = "1111" . $code_string . "311";
	} elseif ( strtolower($code_type) == "codabar" ) {
		$code_array1 = array("1","2","3","4","5","6","7","8","9","0","-","$",":","/",".","+","A","B","C","D");
		$code_array2 = array("1111221","1112112","2211111","1121121","2111121","1211112","1211211","1221111","2112111","1111122","1112211","1122111","2111212","2121112","2121211","1121212","1122121","1212112","1112122","1112221");

		// Convert to uppercase
		$upper_text = strtoupper($text);

		for ( $X = 1; $X&lt;=strlen($upper_text); $X++ ) {
			for ( $Y = 0; $Y&lt;count($code_array1); $Y++ ) {
				if ( substr($upper_text, ($X-1), 1) == $code_array1[$Y] )
					$code_string .= $code_array2[$Y] . "1";
			}
		}
		$code_string = "11221211" . $code_string . "1122121";
	}

	// Pad the edges of the barcode
	$code_length = 20;
	if ($print) {
		$text_height = 30;
	} else {
		$text_height = 0;
	}
	
	for ( $i=1; $i &lt;= strlen($code_string); $i++ ){
		$code_length = $code_length + (integer)(substr($code_string,($i-1),1));
        }

	if ( strtolower($orientation) == "horizontal" ) {
		$img_width = $code_length*$SizeFactor;
		$img_height = $size;
	} else {
		$img_width = $size;
		$img_height = $code_length*$SizeFactor;
	}

	$image = imagecreate($img_width, $img_height + $text_height);
	$black = imagecolorallocate ($image, 0, 0, 0);
	$white = imagecolorallocate ($image, 255, 255, 255);

	imagefill( $image, 0, 0, $white );
	if ( $print ) {
		imagestring($image, 5, 31, $img_height, $text, $black );
	}

	$location = 10;
	for ( $position = 1 ; $position &lt;= strlen($code_string); $position++ ) {
		$cur_size = $location + ( substr($code_string, ($position-1), 1) );
		if ( strtolower($orientation) == "horizontal" )
			imagefilledrectangle( $image, $location*$SizeFactor, 0, $cur_size*$SizeFactor, $img_height, ($position % 2 == 0 ? $white : $black) );
		else
			imagefilledrectangle( $image, 0, $location*$SizeFactor, $img_width, $cur_size*$SizeFactor, ($position % 2 == 0 ? $white : $black) );
		$location = $cur_size;
	}
	
	// Draw barcode to the screen or save in a file
	if ( $filepath=="" ) {
		header ('Content-type: image/png');
		imagepng($image);
		imagedestroy($image);
	} else {
		imagepng($image,$filepath);
		imagedestroy($image);		
	}
}

?&gt;</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>Step 2:</strong><br />
Write code in your PHP file, where you want to display Barcode. Example given below.</p><pre class="urvanov-syntax-highlighter-plain-tag">&lt;?php
$text ="123456";
?&gt;
&lt;img alt="Serial No." src='barcode.php?codetype=Code39&amp;size=40&amp;text=&lt;?php echo $text; ?&gt;&amp;print=true'/&gt;</pre><p>&nbsp;</p>
<p>The post <a href="https://www.codypaste.com/generate-barcode-in-php-as-image/">Generate Barcode in PHP as Image</a> appeared first on <a href="https://www.codypaste.com">Cody Paste</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codypaste.com/generate-barcode-in-php-as-image/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
