PHP5 A simple example of barcode generation

Source: Internet
Author: User
The software supports the PHP4 and PHP5 two versions, and the PHP5 version is used in this article. Note that the GD module of PHP should be turned on before use. gd.so for Php_gd2.dll,linux in Windows. Extract the compressed file to Apache and execute http://localhost/barcode/index.php. PHP5 generate barcodes See the following figure:

1. Type: Select barcode type

2. Output: Picture format

3. Thickness: Bar code height

4. Resolution: Barcode Size

5. Font: Bar code below the text size, you can not display text

6. Text: Barcode Printing Content

Of course, this PHP5 generated barcode program just to make the text into a barcode, but not flexible to use it embedded in other PHP programs, I will compress the inside of the test.php made some adjustments, so that it can be flexible for other programs. The runtime simply passes the barcode type and text to test.php, for example:
http://localhost/barcode/test.php?codebar=BCGcode39&text=20090729

or run http://localhost/barcode/mytest.php

mytest.php Code:

  
   
   
  1. src="test.php?codebar=bcgcode39&text=20090729">
PHP5 Generate Barcode Effect chart:

PHP5 Generate barcode test.php code:

  
  
  1. <?php
  2. //Including all required classes  
  3. require  (' class/bcgfont.php ');
  4. require  (' class/bcgcolor.php ');
  5. require  (' class/bcgdrawing.php ');
  6. / * ' Bcgcodabar ', ' BCGcode11 ', ' BCGcode39 ', ' bcgcode39extended ', ' BCGcode93 ',
  7. ' BCGcode128 ', ' BCGean8 ', ' BCGean13 ', ' bcgisbn ', ' BCGi25 ', ' BCGs25 ', ' bcgmsi ',  
  8. ' BCGUPCA ', ' bcgupce ', ' BCGupcext2 ', ' BCGupcext5 ', ' bcgpostnet ', ' bcgothercode ' *  
  9. $codebar  =  $_request [' Codebar ']; //All coding supported by the software, simply adjust the $codebar parameters.  
  10. //Including the barcode technology  
  11. include (' class/'. $codebar . '. barcode.php '  );
  12. //Loading Font  
  13. $font  =  New  Bcgfont ('./class/font/arial.ttf ', 10);
  14. //The arguments are R, G, B for color.  
  15. $color _black  =  New  bcgcolor (0, 0, 0);
  16. $color _white  =  New  Bcgcolor (255, 255, 255);
  17. $code  =  New   $codebar();
  18. $code ->setscale (2); //Resolution  
  19. $code ->setthickness (a); //Thickness  
  20. $code ->setforegroundcolor ($color _black); //Color of bars  
  21. $code ->setbackgroundcolor ($color _white); //Color of spaces  
  22. $code ->setfont ($font); //Font (or 0)  
  23. $text  =  $_request [' text ']; //PHP5 generates the content of the barcode to be data  
  24. $code  ->parse ($text);
  25. /* This is the list of the arguments
  26. 1-filename (empty:display on screen)  
  27. 2-background Color * *  
  28. $drawing  =  New  bcgdrawing (", $color _white);
  29. $drawing  ->setbarcode ($code);
  30. $drawing  ->draw ();
  31. //Header that says it's an image (remove it if save the barcode to a file)  
  32. header (' content-type:image/png ');
  33. //Draw (or save) the image into PNG format.  
  34. $drawing  ->finish (bcgdrawing::img_format_png);
  35. ?>

An exception occurred during the run;
Throw $e; Throw the exception again
}
}
? ﹥



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.