/**
  * Generates the barcode and outputs it as header image/jpg
  *
  * @return  string
  */
 public function getBarcode()
 {
     $code = JFactory::getApplication()->input->get("code", '');
     if (empty($code)) {
         echo "No code given";
         return;
     }
     $text = 1;
     $width = 300;
     $height = 50;
     header("Content-type: image/png");
     $im = ImageCreate($width, $height) or die("Cannot Initialize new GD image stream");
     $White = ImageColorAllocate($im, 255, 255, 255);
     $Black = ImageColorAllocate($im, 0, 0, 0);
     ImageInterLace($im, 1);
     $NarrowRatio = 20;
     $WideRatio = 55;
     $QuietRatio = 35;
     $nChars = (strlen($code) + 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($code) + 2);
     if ($NarrowBar == 0 || $NarrowBar == $WideBar || $NarrowBar == $QuietBar || $WideBar == 0 || $WideBar == $QuietBar || $QuietBar == 0) {
         ImageString($im, 1, 0, 0, "Image is too small!", $Black);
         ImagePNG($im);
         exit;
     }
     $CurrentBarX = (int) (($width - $ActualWidth) / 2);
     $Color = $White;
     $BarcodeFull = "*" . strtoupper($code) . "*";
     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 = MatukioHelperUtilsBooking::getCode99($BarcodeFull[$i]);
         for ($n = 0; $n < 9; $n++) {
             if ($Color == $White) {
                 $Color = $Black;
             } else {
                 $Color = $White;
             }
             switch ($StripeCode[$n]) {
                 case '0':
                     ImageFilledRectangle($im, $CurrentBarX, 0, $CurrentBarX + $NarrowBar, $height - 1 - $FontHeight - 2, $Color);
                     $CurrentBarX += $NarrowBar;
                     break;
                 case '1':
                     ImageFilledRectangle($im, $CurrentBarX, 0, $CurrentBarX + $WideBar, $height - 1 - $FontHeight - 2, $Color);
                     $CurrentBarX += $WideBar;
                     break;
             }
         }
         $Color = $White;
         ImageFilledRectangle($im, $CurrentBarX, 0, $CurrentBarX + $QuietBar, $height - 1 - $FontHeight - 2, $Color);
         $CurrentBarX += $QuietBar;
     }
     ImagePNG($im);
     imagedestroy($im);
     jexit();
 }