linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file BarcodeObject

Documentation is available at BarcodeObject

  1. <?php
  2. //============================================================+
  3. // File name   : barcode.php
  4. // Begin       : 2002-07-31
  5. // Last Update : 2005-01-02
  6. // Author      : Karim Mribti [barcode@mribti.com]
  7. // Version     : 1.1 [0.0.8a (original code)]
  8. // License     : GNU LGPL (Lesser General Public License) 2.1
  9. //               http://www.gnu.org/copyleft/lesser.txt
  10. // Source Code : http://www.mribti.com/barcode/
  11. //
  12. // Description : Generic Barcode Render Class for PHP using
  13. //               the GD graphics library.
  14. //
  15. // NOTE:
  16. // This version contains changes by Nicola Asuni:
  17. //  - porting to PHP5
  18. //  - code style and formatting
  19. //  - automatic php documentation in PhpDocumentor Style
  20. //    (www.phpdoc.org)
  21. //  - minor bug fixing
  22. //  - $mCharSet and $mChars variables were added here
  23. //============================================================+
  24.  
  25. /**
  26.  * Barcode Render Class for PHP using the GD graphics library.
  27.  * @author Karim Mribti, Nicola Asuni
  28.  * @name BarcodeObject
  29.  * @package linea21.externals
  30.  * @subpackage com.tecnick.tcpdf
  31.  * @version 0.0.8a 2001-04-01 (original code)
  32.  * @since 2001-03-25
  33.  * @license http://www.gnu.org/copyleft/lesser.html LGPL
  34.  */
  35.  
  36. // Styles
  37. // Global
  38.  
  39. /**
  40.  * option: generate barcode border
  41.  */
  42. define("BCS_BORDER"1);
  43.  
  44. /**
  45.  * option: use transparent background
  46.  */
  47. define("BCS_TRANSPARENT"2);
  48.  
  49. /**
  50.  * option: center barcode
  51.  */
  52. define("BCS_ALIGN_CENTER"4);
  53.  
  54. /**
  55.  * option: align left
  56.  */
  57. define("BCS_ALIGN_LEFT"8);
  58.  
  59. /**
  60.  * option: align right
  61.  */
  62. define("BCS_ALIGN_RIGHT"16);
  63.  
  64. /**
  65.  * option: generate JPEG image
  66.  */
  67. define("BCS_IMAGE_JPEG"32);
  68.  
  69. /**
  70.  * option: generate PNG image
  71.  */
  72. define("BCS_IMAGE_PNG"64);
  73.  
  74. /**
  75.  * option: draw text
  76.  */
  77. define("BCS_DRAW_TEXT"128);
  78.  
  79. /**
  80.  * option: stretch text
  81.  */
  82. define("BCS_STRETCH_TEXT"256);
  83.  
  84. /**
  85.  * option: reverse color
  86.  */
  87. define("BCS_REVERSE_COLOR"512);
  88.  
  89. /**
  90.  * option: draw check
  91.  * (only for I25 code)
  92.  */
  93. define("BCS_I25_DRAW_CHECK"2048);
  94.  
  95. /**
  96.  * set default background color
  97.  */
  98. define("BCD_DEFAULT_BACKGROUND_COLOR"0xFFFFFF);
  99.  
  100. /**
  101.  * set default foreground color
  102.  */
  103. define("BCD_DEFAULT_FOREGROUND_COLOR"0x000000);
  104.  
  105. /**
  106.  * set default style options
  107.  */
  108. define("BCD_DEFAULT_STYLE"BCS_BORDER BCS_ALIGN_CENTER BCS_IMAGE_PNG);
  109.  
  110. /**
  111.  * set default width
  112.  */
  113. define("BCD_DEFAULT_WIDTH"460);
  114.  
  115. /**
  116.  * set default height
  117.  */
  118. define("BCD_DEFAULT_HEIGHT"120);
  119.  
  120. /**
  121.  * set default font
  122.  */
  123. define("BCD_DEFAULT_FONT"5);
  124.  
  125. /**
  126.  * st default horizontal resolution
  127.  */
  128. define("BCD_DEFAULT_XRES"2);
  129.  
  130. // Margins
  131.  
  132. /**
  133.  * set default margin
  134.  */
  135. define("BCD_DEFAULT_MAR_Y1"0);
  136.  
  137. /**
  138.  * set default margin
  139.  */
  140. define("BCD_DEFAULT_MAR_Y2"0);
  141.  
  142. /**
  143.  * set default text offset
  144.  */
  145. define("BCD_DEFAULT_TEXT_OFFSET"2);
  146.  
  147. // For the I25 Only
  148.  
  149. /**
  150.  * narrow bar option
  151.  * (only for I25 code)
  152.  */
  153. define("BCD_I25_NARROW_BAR"1);
  154.  
  155. /**
  156.  * wide bar option
  157.  * (only for I25 code)
  158.  */
  159. define("BCD_I25_WIDE_BAR"2);
  160.  
  161. // For the C39 Only
  162.  
  163. /**
  164.  * narrow bar option
  165.  * (only for c39 code)
  166.  */
  167. define("BCD_C39_NARROW_BAR"1);
  168.  
  169. /**
  170.  * wide bar option
  171.  * (only for c39 code)
  172.  */
  173. define("BCD_C39_WIDE_BAR"2);
  174.  
  175. // For Code 128
  176.  
  177. /**
  178.  * set type 1 bar
  179.  * (only for c128 code)
  180.  */
  181. define("BCD_C128_BAR_1"1);
  182.  
  183. /**
  184.  * set type 2 bar
  185.  * (only for c128 code)
  186.  */
  187. define("BCD_C128_BAR_2"2);
  188.  
  189. /**
  190.  * set type 3 bar
  191.  * (only for c128 code)
  192.  */
  193. define("BCD_C128_BAR_3"3);
  194.  
  195. /**
  196.  * set type 4 bar
  197.  * (only for c128 code)
  198.  */
  199. define("BCD_C128_BAR_4"4);
  200.  
  201. /**
  202.  * Barcode Render Class for PHP using the GD graphics library.
  203.  * @author Karim Mribti, Nicola Asuni
  204.  * @name BarcodeObject
  205.  * @package linea21.externals
  206.  * @version 0.0.8a 2001-04-01 (original code)
  207.  * @since 2001-03-25
  208.  * @license http://www.gnu.org/copyleft/lesser.html LGPL
  209.  */
  210. class BarcodeObject {
  211.     /**
  212.      * @var Image width in pixels.
  213.      * @access protected
  214.      */
  215.     protected $mWidth;
  216.     
  217.     /**
  218.      * @var Image height in pixels.
  219.      * @access protected
  220.      */
  221.     protected $mHeight;
  222.     
  223.     /**
  224.      * @var Numeric code for Barcode style.
  225.      * @access protected
  226.      */
  227.     protected $mStyle;
  228.     
  229.     /**
  230.      * @var Background color.
  231.      * @access protected
  232.      */
  233.     protected $mBgcolor;
  234.     
  235.     /**
  236.      * @var Brush color.
  237.      * @access protected
  238.      */
  239.     protected $mBrush;
  240.     
  241.     /**
  242.      * @var Image object.
  243.      * @access protected
  244.      */
  245.     protected $mImg;
  246.     
  247.     /**
  248.      * @var Numeric code for character font.
  249.      * @access protected
  250.      */
  251.     protected $mFont;
  252.     
  253.     /**
  254.      * @var Error message.
  255.      * @access protected
  256.      */
  257.     protected $mError;
  258.     
  259.     /**
  260.      * @var Character Set.
  261.      * @access protected
  262.      */
  263.     protected $mCharSet;
  264.     
  265.     /**
  266.      * @var Allowed symbols.
  267.      * @access protected
  268.      */
  269.     protected $mChars;
  270.  
  271.     /**
  272.      * Class Constructor.
  273.      * @param int $Width Image width in pixels.
  274.      * @param int $Height Image height in pixels.
  275.      * @param int $Style Barcode style.
  276.      */
  277.     public function __construct($Width=BCD_DEFAULT_WIDTH$Height=BCD_DEFAULT_HEIGHT$Style=BCD_DEFAULT_STYLE{
  278.         $this->mWidth = $Width;
  279.         $this->mHeight = $Height;
  280.         $this->mStyle = $Style;
  281.         $this->mFont = BCD_DEFAULT_FONT;
  282.         $this->mImg = ImageCreate($this->mWidth$this->mHeight);
  283.         $this->mBgcolor = ImageColorAllocate($this->mImg($dbColor 0xFF0000>> 16,
  284.         ($dbColor 0x00FF00>> 8$dbColor 0x0000FF);
  285.         $this->mBrush = ImageColorAllocate($this->mImg($dfColor 0xFF0000>> 16,
  286.         ($dfColor 0x00FF00>> 8$dfColor 0x0000FF);
  287.         if (!($this->mStyle BCS_TRANSPARENT)) {
  288.             ImageFill($this->mImg$this->mWidth$this->mHeight$this->mBgcolor);
  289.         }
  290.     }
  291.     
  292.     /**
  293.      * Class Destructor.
  294.      * Destroy image object.
  295.      */
  296.     public function __destructor({
  297.         $this->DestroyObject();
  298.     }
  299.  
  300.     /**
  301.      * Returns the image object.
  302.      * @return object image. 
  303.      * @author Nicola Asuni
  304.      * @since 1.5.2
  305.      */
  306.     public function getImage({
  307.         return $this->mImg;
  308.     }
  309.     
  310.     /**
  311.      * Abstract method used to draw the barcode image.
  312.      * @param int $xres Horizontal resolution.
  313.      */
  314.     public function DrawObject($xres)    {
  315.         /* there is not implementation neded, is simply the asbsract function. */
  316.         return false;
  317.     }
  318.     
  319.     /**
  320.      * Draws the barcode border.
  321.      * @access protected
  322.      */
  323.     protected function DrawBorder({
  324.         ImageRectangle($this->mImg00$this->mWidth-1$this->mHeight-1$this->mBrush);
  325.     }
  326.     
  327.     /**
  328.      * Draws the alphanumeric code.
  329.      * @param int $Font Font type.
  330.      * @param int $xPos Horiziontal position.
  331.      * @param int $yPos Vertical position.
  332.      * @param int $Char Alphanumeric code to write.
  333.      * @access protected
  334.      */
  335.     protected function DrawChar($Font$xPos$yPos$Char{
  336.         ImageString($this->mImg,$Font,$xPos,$yPos,$Char,$this->mBrush);
  337.     }
  338.     
  339.     /**
  340.      * Draws a character string.
  341.      * @param int $Font Font type.
  342.      * @param int $xPos Horiziontal position.
  343.      * @param int $yPos Vertical position.
  344.      * @param int $Char string to write.
  345.      * @access protected
  346.      */
  347.     protected function DrawText($Font$xPos$yPos$Char{
  348.         ImageString($this->mImg,$Font,$xPos,$yPos,$Char,$this->mBrush);
  349.     }
  350.  
  351.     /**
  352.      * Draws a single barcode bar.
  353.      * @param int $xPos Horiziontal position.
  354.      * @param int $yPos Vertical position.
  355.      * @param int $xSize Horizontal size.
  356.      * @param int $xSize Vertical size.
  357.      * @return bool trur in case of success, false otherwise.
  358.      * @access protected
  359.      */
  360.     protected function DrawSingleBar($xPos$yPos$xSize$ySize{
  361.         if ($xPos>=&& $xPos<=$this->mWidth && ($xPos+$xSize)<=$this->mWidth &&
  362.         $yPos>=&& $yPos<=$this->mHeight && ($yPos+$ySize)<=$this->mHeight{
  363.             for ($i=0;$i<$xSize;$i++{
  364.                 ImageLine($this->mImg$xPos+$i$yPos$xPos+$i$yPos+$ySize$this->mBrush);
  365.             }
  366.             return true;
  367.         }
  368.         return false;
  369.     }
  370.     
  371.     /**
  372.      * Returns the current error message.
  373.      * @return string error message.
  374.      */
  375.     public function GetError({
  376.         return $this->mError;
  377.     }
  378.     
  379.     /**
  380.      * Returns the font height.
  381.      * @param int $font font type.
  382.      * @return int font height.
  383.      */
  384.     public function GetFontHeight($font{
  385.         return ImageFontHeight($font);
  386.     }
  387.     
  388.     /**
  389.      * Returns the font width.
  390.      * @param int $font font type.
  391.      * @return int font width.
  392.      */
  393.     public function GetFontWidth($font{
  394.         return ImageFontWidth($font);
  395.     }
  396.     
  397.     /**
  398.      * Set font type.
  399.      * @param int $font font type.
  400.      */
  401.     public function SetFont($font{
  402.         $this->mFont = $font;
  403.     }
  404.     
  405.     /**
  406.      * Returns barcode style.
  407.      * @return int barcode style.
  408.      */
  409.     public function GetStyle({
  410.         return $this->mStyle;
  411.     }
  412.  
  413.     /**
  414.      * Set barcode style.
  415.      * @param int $Style barcode style.
  416.      */
  417.     public function SetStyle ($Style{
  418.         $this->mStyle = $Style;
  419.     }
  420.  
  421.     /**
  422.      * Flush the barcode image.
  423.      */
  424.     public function FlushObject({
  425.         if (($this->mStyle BCS_BORDER)) {
  426.             $this->DrawBorder();
  427.         }
  428.         if ($this->mStyle BCS_IMAGE_PNG{
  429.             Header("Content-Type: image/png");
  430.             ImagePng($this->mImg);
  431.         else if ($this->mStyle BCS_IMAGE_JPEG{
  432.             Header("Content-Type: image/jpeg");
  433.             ImageJpeg($this->mImg);
  434.         }
  435.     }
  436.     
  437.     /**
  438.      * Destroy the barcode image.
  439.      */
  440.     public function DestroyObject({
  441.         ImageDestroy($this->mImg);
  442.     }
  443. }
  444.  
  445. //============================================================+
  446. // END OF FILE
  447. //============================================================+
  448. ?>

Documentation generated on Fri, 16 Oct 2009 09:28:51 +0200 by phpDocumentor 1.4.1