PHP5 simple example of generating a barcode _ PHP Tutorial

Source: Internet
Author: User
A simple example of generating a barcode in PHP5. This software supports two versions: PHP4 and PHP5. the PHP5 version is used in this article. Before using the function, you must enable the GD module of PHP. In Windows, it is php_gd2.dll, and in Linux it is gd. so. The software supports PHP4 and PHP5 versions. the PHP5 version is used in this article. Before using the function, you must enable the GD module of PHP. In Windows, it is php_gd2.dll, and in Linux it is gd. so. Decompress the compressed file to Apache and execute Http: // localhost/barcode/index. php. For more information about generating the PHP5 barcode, see:

1. Type: Select the barcode Type

2. Output: Output Image format

3. Thickness: bar code height

4. Resolution: bar code size

5. Font: the text at the bottom of the bar code cannot be displayed.

6. Text: bar code printed content

498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'alt = bar code effect src = "http://www.bkjia.com/uploadfile/2013/0904/20130904094617982.jpg" width = 615 height = 557>

Of course, this PHP5 generated barcode program only generates text as a barcode, but it cannot be flexibly embedded into other PHP programs during use. I will compress the test in the package. php has made some adjustments so that it can be used flexibly in other programs. You only need to pass the barcode type and text to test. php at runtime, for example:
Http: // localhost/barcode/test. php? Codebar = BCGcode39 & text = 20090729

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

Mytest. php code:

   
  
  1. <img src="test.php?codebar=BCGcode39&text=20090729">
PHP5 generates a barcode:

498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'alt = run instance src = "http://www.bkjia.com/uploadfile/2013/0904/20130904094617379.jpg" width = 665 height = 186>

PHP5 generates the code test. php:

   
  
  1. // Including all required classes
  2. Require('Class/BCGFont. php');
  3. Require('Class/BCGColor. php');
  4. Require('Class/BCGDrawing. php');
  5. /* 'Bcgcodabar ', 'bcgcode11', 'bcgcode39extended', 'bcgcode93 ',
  6. 'Bcgcode128 ', 'bcgean8', 'bcgean13', 'bcgisbn', 'bcgi25', 'bcgs25', 'bcgmsi ',
  7. 'Bcgupca ', 'bcgupce', 'bcgupcext2', 'bcgupcext5', 'bcgpostnet', 'bcgothercode '*/
  8. $ Codebar=$ _ REQUEST['Codebar'];// All codes supported by the software. you only need to adjust the $ codebar parameter.
  9. // Including the barcode technology
  10. Include('Class /'.$ Codebar.'. Barcode. php');
  11. // Loading Font
  12. $ Font=NewBCGFont ('./Class/font/Arial. ttf', 10 );
  13. // The arguments are R, G, B for color.
  14. $ Color_black=NewBCGColor (0, 0, 0 );
  15. $ Color_white=NewBCGColor (1, 255,255,255 );
  16. $ Code=New $ Codebar();
  17. $ Code-> SetScale (2 );// Resolution
  18. $ Code-> SetThickness (30 );// Thickness
  19. $ Code-> SetForegroundColor ($ Color_black);// Color of bars
  20. $ Code-> SetBackgroundColor ($ Color_white);// Color of spaces
  21. $ Code-> SetFont ($ Font);// Font (or 0)
  22. $ Text=$ _ REQUEST['Text'];// PHP5 generates the content of the barcode to be data
  23. $ Code-> Parse ($ Text);
  24. /* Here is the list of the arguments
  25. 1-Filename (empty: display on screen)
  26. 2-Background color */
  27. $ Drawing=NewBCGDrawing ('',$ Color_white);
  28. $ Drawing-> SetBarcode ($ Code);
  29. $ Drawing-> Draw ();
  30. // Header that says it is an image (remove it if you save the barcode to a file)
  31. Header ('Content-Type: image/png');
  32. // Draw (or save) the image into PNG format.
  33. $ Drawing-> Finish (BCGDrawing: IMG_FORMAT_PNG );
  34. ?>
An exception occurs during running ";
Throw $ e; // throwing an exception
}
}
? >
 


Bytes. Before using the function, you must enable the GD module of PHP. In Windows, it is php_gd2.dll, and in Linux it is gd. so. Compress...

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.