dlow128 Posted August 1, 2009 Share Posted August 1, 2009 Can someone pls help with barcode php script? I am currently working on a barcode php script, in the example shown below the script will generate a barcode based on a ramdom mysql query. The problem i have is I need to be able to display the same image multiple times based on a variable. To elaborate, mysql will run a query and fetch a row which will contain a unque code to be used as the barcode and a number to indicate how many barcodes need to be displayed on page. Is there a way i can use this number as a variable to be able to display the same barcode multiple times in a column one underneath the other. Thanking you in advance script: <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $query = mysql_fetch_row(mysql_query("select codes from list order by rand() limit 1")); $barcodeheight=40; $barcodethinwidth=2; $barcodethickwidth=$barcodethinwidth*3; $codingmap = Array( "0"=> "000110100", "1"=> "100100001", "2"=> "001100001", "3"=> "101100000", "4"=> "000110001", "5"=> "100110000", "6"=> "001110000", "7"=> "000100101", "8"=> "100100100", "9"=> "001100100", "A"=> "100001001", "B"=> "001001001", "C"=> "101001000", "D"=> "000011001", "E"=> "100011000", "F"=> "001011000", "G"=> "000001101", "H"=> "100001100", "I"=> "001001100", "J"=> "000011100", "K"=> "100000011", "L"=> "001000011", "M"=> "101000010", "N"=> "000010011", "O"=> "100010010", "P"=> "001010010", "Q"=> "000000111", "R"=> "100000110", "S"=> "001000110", "T"=> "000010110", "U"=> "110000001", "V"=> "011000001", "W"=> "111000000", "X"=> "010010001", "Y"=> "110010000", "Z"=> "011010000", " "=> "011000100", "$"=> "010101000", "%"=> "000101010", "*"=> "010010100", "+"=> "010001010", "-"=> "010000101", "."=> "110000100", "/"=> "010100010"); $text = strtoupper($query[0]); $textlen = strlen($text); $barcodewidth = ($textlen)*(7*$barcodethinwidth + 3*$barcodethickwidth)-$barcodethinwidth; $im = ImageCreate($barcodewidth,$barcodeheight); $black = ImageColorAllocate($im,0,0,0); $white = ImageColorAllocate($im,255,255,255); imagefill($im,0,0,$white); $xpos=0; for ($idx=0;$idx<$textlen;$idx++) { $char = substr($text,$idx,1); // make unknown chars a '-'; if (!isset($codingmap[$char])) $char = "-"; for ($baridx=0;$baridx<=8;$baridx++) { $elementwidth = (substr($codingmap[$char],$baridx,1)) ? $barcodethickwidth : $barcodethinwidth; if (($baridx+1)%2) imagefilledrectangle($im,$xpos,0,$xpos + $elementwidth-1,$barcodeheight,$black); $xpos+=$elementwidth; } $xpos+=$elementwidth; } Header( "Content-type: image/gif"); ImageGif($im); ImageDestroy($im); return; ?> Link to comment https://forums.phpfreaks.com/topic/168404-can-someone-pls-help-with-barcode-php-script/ Share on other sites More sharing options...
GingerRobot Posted August 1, 2009 Share Posted August 1, 2009 You could either save the image and output multiple HTML image tags, or you'll have to create a larger canvas and copy the image onto it multiple times using imagecopyresampled Link to comment https://forums.phpfreaks.com/topic/168404-can-someone-pls-help-with-barcode-php-script/#findComment-888319 Share on other sites More sharing options...
dlow128 Posted August 1, 2009 Author Share Posted August 1, 2009 You could either save the image and output multiple HTML image tags, or you'll have to create a larger canvas and copy the image onto it multiple times using imagecopyresampled Thank you for your quick response but im a bit new at this is there any way you could elaborate or provide brief example. again Many thanks for your response Link to comment https://forums.phpfreaks.com/topic/168404-can-someone-pls-help-with-barcode-php-script/#findComment-888325 Share on other sites More sharing options...
GingerRobot Posted August 1, 2009 Share Posted August 1, 2009 Using the first option, the imagegif function takes an optional second parameter: a filename to save the image to. So, you could do this for example: ImageGif($im,"barcode.gif"); $barcodesRequired = 3; $output = str_repeat("<img src='barcode.gif' /><br/>",$barcodesRequired); echo $output; If you take this approach, then you'll have to remove this line: Header( "Content-type: image/gif"); As the content you're serving is no longer a gif image, it's HTML. It might be that you specifically wanted to output a single image with the barcode appearing multiple times though. In which case, you'd want the second option: //after creating the barcode $barcodesRequired = 4; $im2 = imageCreate($barcodewidth,$barcodeheight*$barcodesRequired); for($x = 1; $x< $barcodesRequired; $x ++){ imagecopy($im2,$im,0,$barcodeheight*$x,0,0,$barcodewidth,$barcodeheight); } Header( "Content-type: image/gif"); ImageGif($im2); ImageDestroy($im); ImageDestroy($im2); return; If you're easy either way, the first option is probably better...it'll use less resources. Link to comment https://forums.phpfreaks.com/topic/168404-can-someone-pls-help-with-barcode-php-script/#findComment-888327 Share on other sites More sharing options...
dlow128 Posted August 1, 2009 Author Share Posted August 1, 2009 Using the first option, the imagegif function takes an optional second parameter: a filename to save the image to. So, you could do this for example: ImageGif($im,"barcode.gif"); $barcodesRequired = 3; $output = str_repeat("<img src='barcode.gif' /><br/>",$barcodesRequired); echo $output; If you take this approach, then you'll have to remove this line: Header( "Content-type: image/gif"); As the content you're serving is no longer a gif image, it's HTML. It might be that you specifically wanted to output a single image with the barcode appearing multiple times though. In which case, you'd want the second option: //after creating the barcode $barcodesRequired = 4; $im2 = imageCreate($barcodewidth,$barcodeheight*$barcodesRequired); for($x = 1; $x< $barcodesRequired; $x ++){ imagecopy($im2,$im,0,$barcodeheight*$x,0,0,$barcodewidth,$barcodeheight); } Header( "Content-type: image/gif"); ImageGif($im2); ImageDestroy($im); ImageDestroy($im2); return; If you're easy either way, the first option is probably better...it'll use less resources. wow this .phtml doc is definetly moving in the right direction thanks to you! ive been trying to upload it to a webserver but im having tech problems. The only thing I wanted to show you was that it is definetly showing more barcodes now but 1. it is showing the first barcode as completly black and 2. the rest are all in one block underneath each other is it possible to separate them. Your the man! I deeply appreciate your help Link to comment https://forums.phpfreaks.com/topic/168404-can-someone-pls-help-with-barcode-php-script/#findComment-888358 Share on other sites More sharing options...
GingerRobot Posted August 1, 2009 Share Posted August 1, 2009 There was one mistake in there...the for loop should be initialized with x as 0, not 1. The following generates 4 google logos in one image, using the code i provided: <?php $im = imagecreatefromgif("http://www.google.co.uk/intl/en_uk/images/logo.gif"); $barcodewidth = imagesx($im); $barcodeheight = imagesy($im); //after creating the barcode $barcodesRequired = 4; $im2 = imageCreate($barcodewidth,$barcodeheight*$barcodesRequired); for($x = 0; $x< $barcodesRequired; $x ++){ imagecopy($im2,$im,0,$barcodeheight*$x,0,0,$barcodewidth,$barcodeheight); } Header( "Content-type: image/gif"); ImageGif($im2); ImageDestroy($im); ImageDestroy($im2); So it seems to be working for me. Perhaps you could post the actual code you're using now? Link to comment https://forums.phpfreaks.com/topic/168404-can-someone-pls-help-with-barcode-php-script/#findComment-888378 Share on other sites More sharing options...
dlow128 Posted August 1, 2009 Author Share Posted August 1, 2009 There was one mistake in there...the for loop should be initialized with x as 0, not 1. The following generates 4 google logos in one image, using the code i provided: <?php $im = imagecreatefromgif("http://www.google.co.uk/intl/en_uk/images/logo.gif"); $barcodewidth = imagesx($im); $barcodeheight = imagesy($im); //after creating the barcode $barcodesRequired = 4; $im2 = imageCreate($barcodewidth,$barcodeheight*$barcodesRequired); for($x = 0; $x< $barcodesRequired; $x ++){ imagecopy($im2,$im,0,$barcodeheight*$x,0,0,$barcodewidth,$barcodeheight); } Header( "Content-type: image/gif"); ImageGif($im2); ImageDestroy($im); ImageDestroy($im2); So it seems to be working for me. Perhaps you could post the actual code you're using now? no black bardode now! I am planning to display delivery address underneath each barcode from mysql query, can the image be split to accomodate. Many thanks Link to comment https://forums.phpfreaks.com/topic/168404-can-someone-pls-help-with-barcode-php-script/#findComment-888386 Share on other sites More sharing options...
GingerRobot Posted August 1, 2009 Share Posted August 1, 2009 I would imagine it would be easier to go with my first method if you intend to display text as well. But to answer the question, yes you could write text to the image and have the barcode displayed below. Link to comment https://forums.phpfreaks.com/topic/168404-can-someone-pls-help-with-barcode-php-script/#findComment-888421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.