<?php
// Define variable to prevent hacking
define('IN_CB',true);

// Including all required classes
require(GTFWConfiguration::GetValue('application', 'gtfw_base') . '/main/lib/barcode/class/index.php');
require(GTFWConfiguration::GetValue('application', 'gtfw_base') . '/main/lib/barcode/class/FColor.php');
require(GTFWConfiguration::GetValue('application', 'gtfw_base') . '/main/lib/barcode/class/BarCode.php');
require(GTFWConfiguration::GetValue('application', 'gtfw_base') . '/main/lib/barcode/class/FDrawing.php');

class ViewBarcode extends ImgResponse
{
   function GetFileName()
   {
      return 'barcode.png';
   }
   
   function ProcessRequest ()
   {
      // Set value
      $type = (isset($_GET['type'])) ? $_GET['type'] : 'code39';
      $height = (isset($_GET['height'])) ? $_GET['height'] : 30;
      $bars = (isset($_GET['bars'])) ? $_GET['bars'] : '000000';
      $spaces = (isset($_GET['spaces'])) ? $_GET['spaces'] : 'FFFFFF';
      $thickness = (isset($_GET['thickness'])) ? $_GET['thickness'] : 1;
      $text = (isset($_GET['text'])) ? $_GET['text'] : 'GTFW Barcode Generator';
      $fontsize = (isset($_GET['fontsize'])) ? $_GET['fontsize'] : 2;

      // including the barcode technology
      include(GTFWConfiguration::GetValue('application', 'gtfw_base') . '/main/lib/barcode/class/'.$type.'.barcode.php');

      // Creating some Color (arguments are R, G, B)
      $bars = new FColor(hexdec(substr($bars,0,2)),hexdec(substr($bars,2,2)),hexdec(substr($bars,4,2)));
      $spaces = new FColor(hexdec(substr($spaces,0,2)),hexdec(substr($spaces,2,2)),hexdec(substr($spaces,4,2)));

      /* Here is the list of the arguments:
      1 - Height of barcode
      2 - Color of bars
      3 - Color of spaces
      4 - Thickness of each bar
      5 - Text to barcode
      6 - Font-size (0-5) */
      $code_generated = new $type($height, $bars, $spaces, $thickness, $text, $fontsize);

      /* Here is the list of the arguments
      1 - Width
      2 - Height
      3 - Filename (empty : display on screen)
      4 - Background color */
      $drawing = new FDrawing(strlen($text)*30,$height+20,'',$spaces);
      $drawing->init(); // You must call this method to initialize the image
      $drawing->add_barcode($code_generated);
      $drawing->draw_all();
      $im = $drawing->get_im();

      // Next line create the little picture, the barcode is being copied inside
      $im2 = imagecreate($code_generated->lastX,$code_generated->lastY);
      imagecopyresized($im2, $im, 0, 0, 0, 0, $code_generated->lastX, $code_generated->lastY, $code_generated->lastX, $code_generated->lastY);
      $drawing->set_im($im2);

      // Header that says it is an image (remove it if you save the barcode to a file)
      header('Content-Type: image/png');

      // Draw (or save) the image into PNG format.
      $drawing->finish(IMG_FORMAT_PNG);
      return null;
   }
}
?>