Barcode on form

22kaka22 12 Feb, 2009
Hi all,
Is it possible add a hidden field on form and, on submit email, convert it in BARCODE type 39?
tnx
GreyHead 12 Feb, 2009
Hi 22kaka22,

Hmmm . . . presumably it will need to be an image in the email. How are you going to create that? It might be better to create the image beforehand and add a link in the hidden field, then use the link to add the image into the email html. Both ways are possible - the tricky bit is creating the barcode image.

Bob
22kaka22 12 Feb, 2009
Hi Bob, thanks for reply
I'm looking for a autogenerator barcode image in php, because I need that any emailform get a unique code...
mybe I should find the php code but I don't know where I should put it in chronoform.

PS: sorry for my english

Marco
GreyHead 12 Feb, 2009
Hi Marco,

Generating a unique code is simple - let us know when you've found a barcode generator and we can help you use it with ChronoForms.

Bob
22kaka22 12 Feb, 2009
I have found this php code for generate a codebar images. What do you think about?
tnx 😀

<?
/*
	AUTHOR: Walter Cattebeke
	DATE: 08-July-2004
	EMAIL: cachiweb@telesurf.com.py
	LICENSE: This code is free. You can use it and/or modify it.
		I only want to be mentioned and notified if you intend to do either.

	Please read the notes.txt !
*/

	require("defs.inc");

/*
	Core function. It draws a barcode representation of a string passed as parameter. Support for
	some image formats. Some formats work, others don't.
	Image manipulation through GD libraries.
*/

	function barCode(
		$p_barcodeType, // Type of barcode to be generated
		$p_origText, // Text to be generated as barcode
		$p_xDim, // smallest ellement width
		$p_w2n, // wide to narrow factor
		$p_charGap, // Intercharacter gap width. usually the same as xDim
		$p_invert, // Whether or not invert starting bar colors 
		$p_charHeight, // height in pixels of a single character
		$p_imgType, // image type output
		$p_drawLabel, // Whether or not include a text label below barcode
		$p_rotationAngle, // Barcode Image rotation angle 
		$p_check, // Whether or not include check digit 
		$p_toFile, // Whether or not write to file
		$p_fileName // File name to use in case of writing to file
	) {

		if ($p_rotationAngle < BC_ROTATE_0 || $p_rotationAngle > BC_ROTATE_270){
			$p_rotationAngle = BC_ROTATE_0;
		}
		$p_rotationAngle = $p_rotationAngle * 90;

		$font = 5; // font type. GD dependent

		$p_w2n = checkWideToNarrow($p_barcodeType, $p_w2n);
		$p_charGap = checkCharGap($p_barcodeType, $p_charGap);
		$p_check = checkCheckDigit($p_barcodeType, $p_check);
		$quietZone = 10 * $p_xDim; // safe white zone before and after the barcode

		if ($p_check) {
			$textCheck = getCheckDigit($p_barcodeType, $p_origText);
		}
		else {
			$textCheck = $p_origText;
		}
		$text2bar = getBarcodeText($p_barcodeType, $textCheck); // format text 
		$charCount = getCharCount($p_barcodeType, $text2bar); // number of symbols

		// image height & width
		$imgWidth = getBarcodeLength($p_barcodeType, $text2bar, $p_xDim, $p_w2n, $quietZone, $p_charGap);
		$imgHeight = $p_charHeight ;

		$hMidHeight = floor($p_charHeight / 2);
		$hTrackWidth = floor($p_charHeight / 4);
		if (($p_charHeight - $hTrackWidth) % 2 != 0){
			$hTrackWidth = $hTrackWidth + 1;
		}
		$hAscWidth = floor(($p_charHeight - $hTrackWidth) / 2);

		if ($p_drawLabel) { // increase image height when adding label
			$imgHeight = $imgHeight + imagefontheight($font);
		}

		$extraWidth = imagefontwidth($font) * strlen($p_origText) - $imgWidth;
		if ($extraWidth > 0) {
			$quietZone = $quietZone + $extraWidth / 2 + 1;
			$imgWidth = getBarcodeLength($p_barcodeType, $text2bar, $p_xDim, $p_w2n, $quietZone, $p_charGap);
		}

		$im = @imagecreate($imgWidth, $imgHeight)
			or die("Cannot Initialize new GD image stream");

		$xPos = $quietZone; // starting bar X position
		$bgColor = imagecolorallocate($im, 255, 255, 255); // white background
		$blackColor = imagecolorallocate($im, 0, 0, 0);
		$whiteColor = imagecolorallocate($im, 255, 255, 255);

		$black = !$p_invert; // what color is the first bar?

		for($j=0;$j<$charCount;$j++){ // traverse string
			$currChar = getSpec($p_barcodeType, $text2bar, $j); // get symbol spec.
			for ($i=0;$i<strlen($currChar);$i++) { // traverse symbol spec.
				if ($black){ // what color is next bar?
					$barColor = $blackColor;
				}
				else {
					$barColor = $whiteColor;
				}
				if ($currChar[$i] == "n"){ // draw a narrow bar
					$xPos1 = $xPos + $p_xDim - 1;
					$yPos = 0;
					$yPos1 = $p_charHeight - 1;
				} elseif ($currChar[$i] == "w") { // draw a wide bar
					$xPos1 = $xPos + $p_xDim * $p_w2n - 1;
					$yPos = 0;
					$yPos1 = $p_charHeight - 1;
				} elseif ($currChar[$i] == "1") { // draw a narrow black bar
					$xPos1 = $xPos + $p_xDim - 1;
					$barColor = $p_invert?$whiteColor:$blackColor;
					$yPos = 0;
					$yPos1 = $p_charHeight - 1;
				} elseif ($currChar[$i] == "0") { // draw a narrow white space
					$xPos1 = $xPos + $p_xDim - 1;
					$barColor = $p_invert?$blackColor:$whiteColor;
					$yPos = 0;
					$yPos1 = $p_charHeight - 1;
				} elseif ($currChar[$i] == "f") { // draw a full vertical bar
					$xPos1 = $xPos + $p_xDim - 1;
					$yPos = 0;
					$yPos1 = $p_charHeight - 1;
				} elseif ($currChar[$i] == "u") { // draw a mid upper vertical bar
					$xPos1 = $xPos + $p_xDim - 1;
					$yPos = 0;
					$yPos1 = $hMidHeight - 1;
				} elseif ($currChar[$i] == "l") { // draw a mid lower vertical bar
					$xPos1 = $xPos + $p_xDim - 1;
					$yPos = $hMidHeight;
					$yPos1 = $p_charHeight - 1;
				} elseif ($currChar[$i] == "t") { // draw a track only vertical bar
					$xPos1 = $xPos + $p_xDim - 1;
					$yPos = $hAscWidth;
					$yPos1 = $hAscWidth + $hTrackWidth - 1;
				} elseif ($currChar[$i] == "a") { // draw a track & ascender vertical bar
					$xPos1 = $xPos + $p_xDim - 1;
					$yPos = 0;
					$yPos1 = $hAscWidth + $hTrackWidth - 1;
				} elseif ($currChar[$i] == "d") { // draw a track & descender vertical bar
					$xPos1 = $xPos + $p_xDim - 1;
					$yPos = $hAscWidth;
					$yPos1 = $p_charHeight - 1;
				}
				if ($currChar[$i] != " ") {
					imagefilledrectangle($im, $xPos , $yPos, $xPos1, $yPos1, $barColor);
					$black = !$black;
					$xPos = $xPos1 + 1;
				}
			}
			// draw intercharacter gap if gap lenght > 0
			if ($j < $charCount - 1 && $p_charGap > 0) { // do not draw last gap
				if ($black){ // it is supposed to be always false but you never know
					$barColor = $blackColor;
				}
				else {
					$barColor = $whiteColor;
				}
				$xPos1 = $xPos + $p_charGap - 1;
				$yPos = 0;
				$yPos1 = $p_charHeight - 1;
				imagefilledrectangle($im, $xPos, $yPos, $xPos1, $yPos1, $barColor);
				$black = !$black;
				$xPos = $xPos1 + 1;
			}

		}
		if ($p_drawLabel) { // draw text label
			$imgTextWidth = imagefontwidth($font) * strlen($p_origText);
			$xText = ($imgWidth - $imgTextWidth) / 2;
			imagestring($im, $font, $xText, $p_charHeight, $p_origText, $blackColor );
		}

		$functionName = barcodeImgFunction($p_imgType); // get php image output function
		if ($p_toFile){
			$fileExt = barcodeFileExt($p_imgType); // get file extension
			$functionName(imagerotate($im, $p_rotationAngle, $whiteColor), $p_fileName . "." . $fileExt); // Automatic image type output
		} else {
			$headerContent = barcodeHeaderContent($p_imgType); // get header type
			header("Content-type: $headerContent"); // Automatic content type output
			$functionName(imagerotate($im, $p_rotationAngle, $whiteColor)); // Automatic image type output
		}
		imagedestroy($im); // free image resource

	}
	
	// uncoment below for direct testing this file
	// will not work as a module if left uncommented !!!!

	// Writing to file "test" a PNG barcode image of "1234567890" with label
	// barCode(BC_TYPE_CODE39, "1234567890", 1, 3, 1, FALSE, 100, BC_IMG_TYPE_PNG, TRUE, BC_ROTATE_0, TRUE, TRUE, "test");
?>
GreyHead 12 Feb, 2009
Hi Marco,

Looks possible - try it stand-alone and see if it works OK.

Bob
22kaka22 12 Feb, 2009
thanks bob you are very kind.

Marco
GreyHead 12 Feb, 2009
Hi Marco,

There's another one here with an on-line demo

Bob
22kaka22 12 Feb, 2009
hi Bob
Sorry for my stupid questions
where I should put the code in chronoform?
I think in: Form Code > On Submit code - before sending email. is OK?

I need that when I submit the form, the data stored in db are in alphanumeric code but mailed a codebar image (like the exemple you linked)
Marco
GreyHead 12 Feb, 2009
Hi Marco,

Yes that's correct. Let me know how it goes please.

Bob
22kaka22 17 Feb, 2009
HI Bob
Sorry but I am lost 😟
Please can you help me with a simple tutorial or example code?
thanks
Marco
Max_admin 17 Feb, 2009
Hi Marco,

the image is created dynamically and so it needs to be saved first then emailed or you may email a link to dynamic image with some parameters which i think will be blocked by most email servers.

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 18 Feb, 2009
Hi Marco,

This is on my 'to-do' list but it will take more than a few minutes to write and the forums here and my clients have been very busy the last few days. Soon I hope.

Bob
GreyHead 20 Feb, 2009
Hi Marco,

I have this working 95% with the script from here.

Download the php script* and put it in a folder on your site - I used components/com_chronocontact/includes/test_form_4/barcode.php

Use code like this in your Form HTML
<?php
/* ensure that this file is called by another file */
defined('_JEXEC') or die('Restricted access');
$code = generateIdent();

?>
<div class="form_item">
  <div class="form_element cf_text"> <span class="cf_text">Your serial number: </span><?php echo $code; ?></div>
  <div class="clear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label">Email:</label>
    <input class="cf_inputbox" maxlength="150" size="30" id="text_3" name="text_3" type="text" />

  </div>
  <div class="clear"> </div>
</div>

<!-- <div class="form_item">
  <div class="form_element cf_captcha">
    <label class="cf_label">Click Me to Edit</label>
    <span>{imageverification}</span>

    </div>
  <div class="clear"> </div>
</div> -->

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Submit" type="submit" />
  </div>
  <div class="clear"> </div>
</div>
<input type='hidden' name='barcode' value='<?php echo $code; ?>' />
<img src="<?php echo JURI::base().'components/com_chronocontact/includes/test_form_4/'; ?>barcode.php?barcode=<?php echo $code; ?>&width=280&height=100">
<?php
	/*
	 * function to generate a random alpha-numeric code
	 * using a specified pattern
	 *
	 * @param $pattern string
	 *
	 * @return string
	 */
	function generateIdent($pattern='AA9999A')
	{
	    $alpha = array("A","B","C","D","E","F","G","H",
        	"J","K","L","M","N","P","Q","R","S","T","U","V","W",
            "X","Y","Z");
	    $digit = array("1","2","3","4","5","6","7","8","9");
	    // :: TODO :: add check in table for duplicates
	    $return = "";
	    $pattern_array = str_split($pattern, 1);
	    foreach ( $pattern_array as $v ) {
	        if ( is_numeric($v) ) {
	            $return .= $digit[array_rand($digit)];
	        } elseif ( in_array(strtoupper($v), $alpha) ) {
	            $return .= $alpha[array_rand($alpha)];
	        } else {
	            $return .= " ";
	        }
	    }
	    return $return;
	}
?>
Change the img src to the url of your version of barcode.php (you can also leave out the generateIdent() function if you already have your id).

Add code like this in your Email template
<div>Your code: {barcode}</div>
<img src="<?php echo JURI::base().'components/com_chronocontact/includes/test_form_4/'; ?>barcode.php?barcode={barcode}&width=280&height=100">
You will need to disable the HTML Editor in the Email setup. And, again you will need to change the url.

That's it really.

The missing 5% is that this includes a link to the image in the Email, not a png or jpg itself. But it works OK - I was able to print out the email and re-scan the barcode successfully.

Bob

* Don't try to copy the View Source version, it's missing a few lines at the end.
22kaka22 05 Mar, 2009
Hi Bob,
thank you very much for your disponibility.
I m testing your code for my purpose.

Marco
This topic is locked and no more replies can be posted.