Title: bar chart of PHP drawing application verification code
Author: morewindows
Blog: http://blog.csdn.net/MoreWindows
Keyword: PhP verification code bar chart imagefilledarc
Before reading this article, we recommend that you refer to the companion article "php drawing basics".
This article describes how to use common PHP plot functions to generate Verification Code images and bar charts.
I. Verification Code
The verification code is very useful in the website. It is a verification code image containing four digits.
The simple verification code image mainly usesAdd some interference points and lines to the correct content. This method is easy to implement. As a demonstration, this article implements a random font (with 10 types of font files) that supports random text color, interference points, and line-of-line verification codes, this type can generate Verification Code images in batches on the disk and specify the number and number of letters of the verification code. For specific functions, refer to the Code:
<? PHP // PHP generate Verification Code // By morewindows (http://blog.csdn.net/MoreWindows) Class csecurity_verify {private $ m_image; private $ m_dir_name; private $ m_image_width; private $ m_image_height; private $ m_digit_num; private $ m_letter_num; private $ m_font_color; const noise_dot_num = 100; // Number of interference points const noise_line_num = 40; // Number of interference lines/** $ dir_name folder for saving the verification code image (absolute path) * $ digit_num Number * $ letter_num number of letters * $ width Verification Code Image Width * $ heig HT Verification Code Image Height */public function _ construct ($ dir_name, $ digit_num, $ letter_num, $ width = 140, $ Height = 40) {$ this-> m_dir_name = $ dir_name; $ this-> m_digit_num = $ digit_num; $ this-> m_letter_num = $ letter_num; $ this-> m_image_width = $ width; $ this-> m_image_height = $ height ;} /** generate a verification code image for the specified condition in the specified directory * $ verify_pic_num how many verification code images will be generated */Public Function batchverifypicture ($ verify_pic_num) {While ($ verify_pic_num> = 0) {$ Verify_pic_num --; self: createverifyimage (); self: drawnoisedot (); self: drawnoiseline (); $ verify_text = self: getverifytext (); $ filesize = self :: drawverifyimage ($ verify_text); if ($ filesize! =-1) echo $ verify_text. ". PNG generated successfully, size ". $ filesize. "byte <br/>"; elseecho $ verify_text. ". PNG generation failed <br/> ";}}/** Create Image */protected function createverifyimage () {$ this-> m_image = imagecreatetruecolor ($ this-> m_image_width, $ this-> m_image_height) or die ("createverifyimage failde"); $ black_color = imagecolorallocate ($ this-> m_image, 243,251,254); imagefill ($ this-> m_image, 0, 0, $ black_color); // set the background color // font color $ m _ Font_color = imagecolorallocate ($ this-> m_image, mt_rand (0,120), mt_rand (0,120), mt_rand (0,120 ));} /** generate Verification Code content * characters used in the verification code. 01ioq is easy to confuse, so it is not needed. */Protected function getverifytext () {$ verify_text = ""; $ letter_array = "inline"; $ digit_num = $ this-> m_digit_num; $ letter_num = $ this-> m_letter_num; while ($ digit_num --) // number $ verify_text. = mt_rand (2, 9); While ($ letter_num --) // The letter $ verify_text. = $ letter_array [mt_rand (0, 22)]; return $ verify_text;}/** */protected function drawverifyimage ($ verify_text) {// font file $ font_file = "TTFs \ t ". Mt_rand (1, 10 ). ". TTF "; // $ verify_text_show =" "; for ($ I = 0; $ I <strlen ($ verify_text); $ I ++) $ verify_text_show. = ($ verify_text [$ I]. ""); // text size, angle, location $ font_size = 20; $ font_angle = mt_rand (0, 5); $ font_pos_x = 0; $ font_pos_y = $ this-> m_image_height-5; imagettftext ($ this-> m_image, $ font_size, $ font_angle, $ font_pos_x, $ font_pos_y, $ this-> m_font_color, $ font_file, $ verify_text_show); $ verify_image _ Filename = $ this-> m_dir_name. "\\$ verify_text.png"; if (! Imagepng ($ this-> m_image, $ verify_image_filename) Return-1; imagedestroy ($ this-> m_image); Return filesize ($ verify_image_filename );} /** paint interference points */protected function drawnoisedot () {$ noise_dot_color = $ this-> m_font_color; For ($ I = 0; $ I <self: noise_dot_num; $ I ++) {imagesetpixel ($ this-> m_image, mt_rand (0, $ this-> m_image_width), mt_rand (0, $ this-> m_image_height ), $ noise_dot_color) ;}}/** painted interference line */protected fun Ction drawnoiseline () {for ($ I = 0; $ I <self: noise_line_num; $ I ++) {$ noise_line_color = imagecolorallocate ($ this-> m_image, mt_rand (50,120), mt_rand (50,120), mt_rand (50,120); imageline ($ this-> m_image, mt_rand (0, $ this-> m_image_width), mt_rand (0, $ this-> m_image_height), mt_rand (0, $ this-> m_image_width), mt_rand (0, $ this-> m_image_height), $ noise_line_color) ;}}}?>
The following is an example. After running the command, six Verification Code images can be generated on drive D. The Code is as follows:
<?phprequire_once 'CSecurity_verify.php';$test = new CSecurity_verify("D:\\", 4, 0);$test->BatchVerifyPicture(6);?>
The verification code is as follows:
Of course, there are also many special effects that can be added, such as text flooding and background color and small letter interference, which can effectively beautify the verification code image. If you have a need, you can study it in depth.
Note that the font file used by the program can be selected from c: \ windows \ fonts and copied to the TTFs folder in the directory where the PHP file is located.
Ii. Bar Chart
You can use bool to draw a bar chart in PHP.Imagefilledarc(Resource$ Image, Int$ CX, Int$ Cy, Int$ W, Int$ H, Int$ S, Int$ E, Int$ Color, Int$ Style) Function. For more information about this function, see the article PHP basics,The principle of a bar chart is to draw multiple layers with dark colors and then draw the top layer with bright colors, so that the comparison between light and shade can produce a three-dimensional effect.For detailed procedures, refer:
The following is an example code of generating a bar chart based on different data values in PHP:
<? PHP // bar chart // By morewindows (http://blog.csdn.net/MoreWindows) $ image_width = 400; $ image_height = 300; $ image = imagecreatetruecolor ($ image_width, $ image_height ); $ black_color = imagecolorallocate ($ image, 243,251,254); imagefill ($ image, 0, 0, $ black_color); // you can specify the background color. // you can specify the background color. $ gray_color = imagecolorallocate ($ image, 0xc0, 0xc0, 0xc0); $ navy_color = imagecolorallocate ($ image, 0x00, 0x00, 0x80); $ red_color = imagec Olorallocate ($ image, 0xff, 0x00, 0x00); // dark color $ darkgray_color = imagecolorallocate ($ image, 0x90, 0x90, 0x90 ); $ darknavy_color = imagecolorallocate ($ image, 0x00, 0x00, 0x50); $ darkred_color = imagecolorallocate ($ image, 0x90, 0x00, 0x00); // the size of each part. $ value_array = array (12.5, 8.4, 79.1); $ all_value = array_sum ($ value_array); $ color_array = array ($ gray_color, $ navy_color, $ red_color); $ drak_color_array = array ($ Darkgray_color, $ darknavy_color, $ darkred_color); // first draw 30 layers for ($ I = 80; $ I> 50; $ I --) {$ angle_begin = 0; $ angle_end = 0; foreach ($ value_array as $ J => $ Val) {$ angle_begin = $ angle_end; $ angle_end + = $ Val * 360/$ all_value; imagefilledarc ($ image, 100, $ I, 200,100, $ angle_begin, $ angle_end, $ drak_color_array [$ J], img_arc_pie) ;}// use a bright drawing at the top layer, in this way, there is a three-dimensional effect. $ Angle_begin = 0; $ angle_end = 0; foreach ($ value_array as $ J => $ Val) {$ angle_begin = $ angle_end; $ angle_end + = $ Val * 360/$ all_value; imagefilledarc ($ image, 100, $ I, 200,100, $ angle_begin, $ angle_end, $ color_array [$ J], img_arc_pie);} // flush imageheader ('content-type: image/PNG '); imagepng ($ image); imagedestroy ($ image);?>
The running effect is as follows:
In general, PHP's drawing function is more convenient and powerful. You can also try phplot to plot the program as needed. Its Class Library is more powerful and easy to use.
Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/7289686