davidscotttufts / php-barcode Goto Github PK
View Code? Open in Web Editor NEWSource code for the article "How To Create Barcodes in PHP" found at http://davidscotttufts.com/2009/03/31/how-to-create-barcodes-in-php/
License: MIT License
Source code for the article "How To Create Barcodes in PHP" found at http://davidscotttufts.com/2009/03/31/how-to-create-barcodes-in-php/
License: MIT License
I'm not sure if this is a general thing with barcodes. When I generate a barcode for "123/456" and read it with my barcode reader on an OS with the keyboard layout set to German, I get "123-456".
On the German keyboard there's a hyphen instead of the slash on the key to the left of the right shift key.
So do the barcodes actually encode keyboard codes instead of characters?
I'm using CodeIgniter for my project, and I put this code under Controller as a function like this :
`
public function barcode($barcode_data)
{
// print_r($this->input->get());
/*
* Author David S. Tufts
* Company davidscotttufts.com
*
* Date: 05/25/2003
* Usage:
*/
// For demonstration purposes, get pararameters that are passed in through $_GET or set to the default value
$filepath = (!empty($barcode_data["filepath"])?$barcode_data["filepath"]:"");
$text = (!empty($barcode_data["text"])?$barcode_data["text"]:"0");
$size = (!empty($barcode_data["size"])?$barcode_data["size"]:"20");
$orientation = (!empty($barcode_data["orientation"])?$barcode_data["orientation"]:"horizontal");
$code_type = (!empty($barcode_data["codetype"])?$barcode_data["codetype"]:"code128");
$print = (!empty($barcode_data["print"])&&$barcode_data["print"]=='true'?true:false);
$sizefactor = (!empty($barcode_data["sizefactor"])?$barcode_data["sizefactor"]:"1");
// This function call can be copied into your project and can be made from anywhere in your code
$this->barcode_generator( $filepath, $text, $size, $orientation, $code_type, $print, $sizefactor );
}
public function barcode_generator( $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(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","@"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","\`"=>"111422","a"=>"121124","b"=>"121421","c"=>"141122","d"=>"141221","e"=>"112214","f"=>"112412","g"=>"122114","h"=>"122411","i"=>"142112","j"=>"142211","k"=>"241211","l"=>"221114","m"=>"413111","n"=>"241112","o"=>"134111","p"=>"111242","q"=>"121142","r"=>"121241","s"=>"114212","t"=>"124112","u"=>"124211","v"=>"411212","w"=>"421112","x"=>"421211","y"=>"212141","z"=>"214121","{"=>"412121","|"=>"111143","}"=>"111341","~"=>"131141","DEL"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","FNC 4"=>"114131","CODE A"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
$code_keys = array_keys($code_array);
$code_values = array_flip($code_keys);
for ( $X = 1; $X <= 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(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","@"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","NUL"=>"111422","SOH"=>"121124","STX"=>"121421","ETX"=>"141122","EOT"=>"141221","ENQ"=>"112214","ACK"=>"112412","BEL"=>"122114","BS"=>"122411","HT"=>"142112","LF"=>"142211","VT"=>"241211","FF"=>"221114","CR"=>"413111","SO"=>"241112","SI"=>"134111","DLE"=>"111242","DC1"=>"121142","DC2"=>"121241","DC3"=>"114212","DC4"=>"124112","NAK"=>"124211","SYN"=>"411212","ETB"=>"421112","CAN"=>"421211","EM"=>"212141","SUB"=>"214121","ESC"=>"412121","FS"=>"111143","GS"=>"111341","RS"=>"131141","US"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","CODE B"=>"114131","FNC 4"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
$code_keys = array_keys($code_array);
$code_values = array_flip($code_keys);
for ( $X = 1; $X <= 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"=>"111221211","1"=>"211211112","2"=>"112211112","3"=>"212211111","4"=>"111221112","5"=>"211221111","6"=>"112221111","7"=>"111211212","8"=>"211211211","9"=>"112211211","A"=>"211112112","B"=>"112112112","C"=>"212112111","D"=>"111122112","E"=>"211122111","F"=>"112122111","G"=>"111112212","H"=>"211112211","I"=>"112112211","J"=>"111122211","K"=>"211111122","L"=>"112111122","M"=>"212111121","N"=>"111121122","O"=>"211121121","P"=>"112121121","Q"=>"111111222","R"=>"211111221","S"=>"112111221","T"=>"111121221","U"=>"221111112","V"=>"122111112","W"=>"222111111","X"=>"121121112","Y"=>"221121111","Z"=>"122121111","-"=>"121111212","."=>"221111211"," "=>"122111211","$"=>"121212111","/"=>"121211121","+"=>"121112121","%"=>"111212121","*"=>"121121211");
// Convert to uppercase
$upper_text = strtoupper($text);
for ( $X = 1; $X<=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 <= strlen($text); $X++ ) {
for ( $Y = 0; $Y < count($code_array1); $Y++ ) {
if ( substr($text, ($X-1), 1) == $code_array1[$Y] )
$temp[$X] = $code_array2[$Y];
}
}
for ( $X=1; $X<=strlen($text); $X+=2 ) {
if ( isset($temp[$X]) && isset($temp[($X + 1)]) ) {
$temp1 = explode( "-", $temp[$X] );
$temp2 = explode( "-", $temp[($X + 1)] );
for ( $Y = 0; $Y < 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<=strlen($upper_text); $X++ ) {
for ( $Y = 0; $Y<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 <= 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 <= 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);
// }
return "<img src=".imagepng($image)."/>";
// return imagepng($image);
// imagedestroy($image);
}
`
and I call the function like this :
$barcode_data = [ 'filepath' => null, 'text' => '1234567', 'size' => 60, 'orientation' => null, 'code_type' => 'code128', 'print' => true, 'sizefactor' => null ]; $barcode = $this->barcode($barcode_data);
and used it in here
`
Please Save This Code
Hi! Code 128 doesn't match with universal barcode maker and I was using a barcode reader machine for code 128 and it only capture the given barcode image by universal barcode maker software. Please check the attachment, there is a significant difference between both barcode [check the red mark section in image] for same data. one is from "php-barcode" and another one is from "universal barcode maker".
Hi, it would be nice if you add the function that you can print a custom text below the barcode
hi how can show the text down the barcode
thanks
The current code works fine. But i do want the coding for EAN13. This coding could I not find and how can we add this? Regards,
Hi mate,
Can we use this code on our commercial projects.
Regards,
Hello
is there any way to change color for barcode ? :D
For example: https://domain.com/?codetype=Code128&size=20&text=barcode123&color=blue
Hi,
I need to put some ITF14 BarCode and calculate the Check Digit. Is possoble?
Sincerely,
MF
barcode is not show in https
I am finding on rare occasions that the encoded result does not match the input string. Example encountered today was:
/barcode.php?text=kqk7
Resulting string read from the barcode by a Datalogic QBT2400 scanner was 'kqk&'. I have tried each of the encodings, though the scanner appears not to be able to read any other than the code128* family, all of which appear to suffer the same issue.
Could you please tell me how can I use Code128B and other format like Code128A,Code128C
This is an awesome plugin that took no time to implement, and I'll be giving it a test later to see if it works as expected (in terms of the scanners reading the barcode properly, not so much the barcode itself not being correct).
I do have a question, though: Is there a way to change the width of the barcode?
I'm using this in an application developed to be used solely from a mobile phone. I have quite a high-resolution screen (1440px wide), but even for the 720px screen, it would be nice to widen it so that it remains clear and scanner-readable.
For the time being, I'm just resizing the image to fit 95% of the screen, but if there is a way to do it in code so it renders sharply, that'd be better.
Thanks!
Hello,
I guess that the both START/STOP symbols for CODABAR are 'A' in this code, but it seems that the string for STOP symbol is different from for 'A'.
Please refer the attached file barcode.php.diff.txt
How about, if we need the image to keep in disc, in some folder, so we can later get a reference to it ?
Thanks.
Seems like the last digit in a row just disappear line after line.
https://drive.google.com/file/d/0BxY5Ee55kW2YQjNjTm5MODg2ODQ/view?usp=sharing
Originally by Abbas on http://davidscotttufts.com/2009/03/31/how-to-create-barcodes-in-php/?replytocom=6120 here is the updated code, may need to merge this.
<?php
/*
* Author David S. Tufts
* Company davidscotttufts.com
*
* Date: 05/25/2003
* Usage: <img src="/barcode.php?text=testing" alt="testing" />
*/
// 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"])&&$_GET["print"]=='true'?true:false);
// 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 );
function barcode( $filepath="", $text="0", $size="30", $orientation="horizontal", $code_type="code128", $print=false ) {
$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(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","@"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","\`"=>"111422","a"=>"121124","b"=>"121421","c"=>"141122","d"=>"141221","e"=>"112214","f"=>"112412","g"=>"122114","h"=>"122411","i"=>"142112","j"=>"142211","k"=>"241211","l"=>"221114","m"=>"413111","n"=>"241112","o"=>"134111","p"=>"111242","q"=>"121142","r"=>"121241","s"=>"114212","t"=>"124112","u"=>"124211","v"=>"411212","w"=>"421112","x"=>"421211","y"=>"212141","z"=>"214121","{"=>"412121","|"=>"111143","}"=>"111341","~"=>"131141","DEL"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","FNC 4"=>"114131","CODE A"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
$code_keys = array_keys($code_array);
$code_values = array_flip($code_keys);
for ( $X = 1; $X <= 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(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","@"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","NUL"=>"111422","SOH"=>"121124","STX"=>"121421","ETX"=>"141122","EOT"=>"141221","ENQ"=>"112214","ACK"=>"112412","BEL"=>"122114","BS"=>"122411","HT"=>"142112","LF"=>"142211","VT"=>"241211","FF"=>"221114","CR"=>"413111","SO"=>"241112","SI"=>"134111","DLE"=>"111242","DC1"=>"121142","DC2"=>"121241","DC3"=>"114212","DC4"=>"124112","NAK"=>"124211","SYN"=>"411212","ETB"=>"421112","CAN"=>"421211","EM"=>"212141","SUB"=>"214121","ESC"=>"412121","FS"=>"111143","GS"=>"111341","RS"=>"131141","US"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","CODE B"=>"114131","FNC 4"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
$code_keys = array_keys($code_array);
$code_values = array_flip($code_keys);
for ( $X = 1; $X <= 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"=>"111221211","1"=>"211211112","2"=>"112211112","3"=>"212211111","4"=>"111221112","5"=>"211221111","6"=>"112221111","7"=>"111211212","8"=>"211211211","9"=>"112211211","A"=>"211112112","B"=>"112112112","C"=>"212112111","D"=>"111122112","E"=>"211122111","F"=>"112122111","G"=>"111112212","H"=>"211112211","I"=>"112112211","J"=>"111122211","K"=>"211111122","L"=>"112111122","M"=>"212111121","N"=>"111121122","O"=>"211121121","P"=>"112121121","Q"=>"111111222","R"=>"211111221","S"=>"112111221","T"=>"111121221","U"=>"221111112","V"=>"122111112","W"=>"222111111","X"=>"121121112","Y"=>"221121111","Z"=>"122121111","-"=>"121111212","."=>"221111211"," "=>"122111211","$"=>"121212111","/"=>"121211121","+"=>"121112121","%"=>"111212121","*"=>"121121211");
// Convert to uppercase
$upper_text = strtoupper($text);
for ( $X = 1; $X<=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 <= strlen($text); $X++ ) {
for ( $Y = 0; $Y < count($code_array1); $Y++ ) {
if ( substr($text, ($X-1), 1) == $code_array1[$Y] )
$temp[$X] = $code_array2[$Y];
}
}
for ( $X=1; $X<=strlen($text); $X+=2 ) {
if ( isset($temp[$X]) && isset($temp[($X + 1)]) ) {
$temp1 = explode( "-", $temp[$X] );
$temp2 = explode( "-", $temp[($X + 1)] );
for ( $Y = 0; $Y < 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<=strlen($upper_text); $X++ ) {
for ( $Y = 0; $Y<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) {
if ( strtolower($orientation) == "horizontal" ) {
$text_width = 0;
$text_height = 20;
}else{
$text_width = 20;
$text_height = 0;
}
} else {
$text_width = 0;
$text_height = 0;
}
for ( $i=1; $i <= strlen($code_string); $i++ ){
$code_length += (integer)(substr($code_string,($i-1),1));
}
if ( strtolower($orientation) == "horizontal" ) {
$img_width = $code_length;
$img_height = $size;
} else {
$img_width = $size;
$img_height = $code_length;
}
$image = imagecreate($img_width + $text_width, $img_height + $text_height);
$black = imagecolorallocate ($image, 0, 0, 0);
$white = imagecolorallocate ($image, 255, 255, 255);
imagefill( $image, 0, 0, $white );
if ( $print ) {
if ( strtolower($orientation) == "horizontal" ) {
imagestring($image, 5, 31, $img_height, $text, $black );
}else if ( strtolower($orientation) == "vertical" ) {
imagestringup ( $image, 2, $img_width, $img_height-40, $text, $black );
}
}
$location = 10;
for ( $position = 1 ; $position <= strlen($code_string); $position++ ) {
$cur_size = $location + ( substr($code_string, ($position-1), 1) );
if ( strtolower($orientation) == "horizontal" )
imagefilledrectangle( $image, $location, 0, $cur_size, $img_height, ($position % 2 == 0 ? $white : $black) );
else
imagefilledrectangle( $image, 0, $location, $img_width, $cur_size, ($position % 2 == 0 ? $white : $black) );
$location = $cur_size;
}
// Draw barcode to the screen or save in a file
if ( $filepath=="" || $filepath==null ) {
header ('Content-type: image/png');
imagepng($image);
}else{
imagepng($image,$filepath);
}
imagedestroy($image);
}
?>
i want to use carry return in my barcode. but i don't know how to use it.
i try to use \r ,\n ,CR,%0D,%0A,chr(13) but result is wrong.
<img alt="testing" src="http://localhost/php-barcode/barcode.php?text=|1234436775\r43255\r3000&print=true" />
help me please.
thank you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.