// File header...
- Header ("Content-type: image/png ");
- // Create white paper with true color
- $ Im = @ imagecreatetruecolor (50, 20) or die ("image creation failed ");
- // Obtain the background color
- $ Background_color = imagecolorallocate ($ instant, 255,255,255 );
- // Fill in the background color (this is similar to a barrel)
- Imagefill ($ im, 0, 0, $ background_color );
- // Obtain the border color
- $ Border_color = imagecolorallocate ($ im, 200,200,200 );
- // Draw a rectangle with a border color of 200,200,200
- Imagerectangle ($ im, $ border_color );
// Show off the background line by line. use 1 or 0 in full screen.
- For ($ I = 2; $ I <18; $ I ++ ){
- // Obtain random light colors
- $ Line_color = imagecolorallocate ($ im, rand (200,255), rand (200,255), rand (200,255 ));
- // Draw a line
- Imageline ($ im, 2, $ I, 47, $ I, $ line_color );
- }
// Set the font size
- $ Font_size = 12;
// Set the printed text
- $ Str [0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
- $ Str [1] = "abcdefghijklmnopqrstuvwxyz ";
- $ Str [2] = "01234567891234567890123456 ";
// Obtain 1st random texts
- $ Imstr [0] ["s"] = $ Str [rand (0, 2)] [rand (0, 25)];
- $ Imstr [0] ["x"] = rand (2, 5 );
- $ Imstr [0] ["y"] = rand (1, 4 );
// Obtain 2nd random texts
- $ Imstr [1] ["s"] = $ Str [rand (0, 2)] [rand (0, 25)];
- $ Imstr [1] ["x"] = $ imstr [0] ["x"] + $ font_size-1 + rand );
- $ Imstr [1] ["y"] = rand (1, 3 );
// Obtain 3rd random texts
- $ Imstr [2] ["s"] = $ Str [rand (0, 2)] [rand (0, 25)];
- $ Imstr [2] ["x"] = $ imstr [1] ["x"] + $ font_size-1 + rand );
- $ Imstr [2] ["y"] = rand (1, 4 );
// Obtain 4th random texts
- $ Imstr [3] ["s"] = $ Str [rand (0, 2)] [rand (0, 25)];
- $ Imstr [3] ["x"] = $ imstr [2] ["x"] + $ font_size-1 + rand );
- $ Imstr [3] ["y"] = rand (1, 3 );
// Write random strings
- For ($ I = 0; $ I <4; $ I ++ ){
- // Obtain random darker colors
- $ Text_color = imagecolorallocate ($ im, rand (50,180), rand (50,180), rand (50,180 ));
- // Draw text
- Imagechar ($ im, $ font_size, $ imstr [$ I] ["x"], $ imstr [$ I] ["y"], $ imstr [$ I] ["s"], $ text_color );
- }
// Display the image
- Imagepng ($ im );
- // Destroy the image
- Imagedestroy ($ im );
- ?>
Example 2: complete code of the beautiful PHP image verification code instance:
- /*
- * @ Author fy
- */
- $ Imgwidth = 100; // image width
- $ Imgheight = 40; // Image height
- $ Codelen = 4; // verification code length
- $ Fontsize = 20; // font size
- $ Charset = 'abcdefghkmnprstuvwxyzabcdefghkmnprstuvwxyz23456789 ';
- $ Font = 'fonts/segoesc. ttf ';
- $ Im = imagecreatetruecolor ($ imgwidth, $ imgheight );
- $ While = imageColorAllocate ($ im, 255,255,255 );
- Imagefill ($ im, $ while); // fill the image
- // Obtain the string
- $ Authstr = '';
- $ _ Len = strlen ($ charset)-1;
- For ($ I = 0; $ I <$ codelen; $ I ++ ){
- $ Authstr. = $ charset [mt_rand (0, $ _ len)];
- }
- Session_start ();
- $ _ SESSION ['scode'] = strtolower ($ authstr); // Convert all to lowercase, mainly to be case insensitive
- // Randomly draw a point, which has been changed to star
- For ($ I = 0; $ I <$ imgwidth; $ I ++ ){
- $ Randcolor = imageColorallocate ($ im, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
- Imagestring ($ im, mt_rand (1, 5), mt_rand (0, $ imgwidth), mt_rand (0, $ imgheight), '*', $ randcolor );
- // Imagesetpixel ($ im, mt_rand (0, $ imgwidth), mt_rand (0, $ imgheight), $ randcolor );
- }
- // Draw lines randomly. the number of lines = the number of characters (casual)
- For ($ I = 0; $ I <$ codelen; $ I ++)
- {
- $ Randcolor = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
- Imageline ($ im, 0, mt_rand (0, $ imgheight), $ imgwidth, mt_rand (0, $ imgheight), $ randcolor );
- }
- $ _ X = intval ($ imgwidth/$ codelen); // calculates the character distance.
- $ _ Y = intval ($ imgheight * 0.7); // the position where the character is displayed on the image 70%.
- For ($ I = 0; $ I $ Randcolor = imagecolorallocate ($ im, mt_rand (0,150), mt_rand (0,150), mt_rand (0,150 ));
- // Imagestring ($ im, 5, $ j, 5, $ imgstr [$ I], $ color3 );
- // Imagettftext (resource $ image, float $ size, float $ angle, int $ x, int $ y, int $ color, string $ fontfile, string $ text)
- Imagettftext ($ im, $ fontsize, mt_rand (-30,30), $ I * $ _ x + 3, $ _ y, $ randcolor, $ font, $ authstr [$ I]);
- }
- // Generate an image
- Header ("content-type: image/PNG ");
- ImagePNG ($ im );
- ImageDestroy ($ im );
Example 3: php5 image verification code Php5 generates an image verification code. You need to use the php GD library function: 1, imagecreatetruecolor ----- create a true color image imagecreatetruecolor (int x_size, int y_size) // x indicates width, and y indicates height 2, imagecolorallocate allocates color (color palette) to an image imagecolorallocate (resource image, int red, int green, int blue) // red, green, blue ---- three primary colors 3, imagestring plotting function iamgestring (resource image, font, int x, int y, content, color); 4. the output function php header defines the header action, php5 supports three types: 1, Content-type: xxxx/yyyy 2, Location: xxxx: yyyy/zzzz 3, Status: nnn xxxxxx/yyyy indicates the type of the Content file, for example, image/gif image/jpeg image/png. example: header ("Content-type: image/jpeg ") the GD Library contains the imagejpeg (), imagegif (), imagepang () 5, and imageline linear function iamgeline (resource image, int x1, int y1, int x2, int y2, int color); image --- image x1 --- start coordinate y1 x2 --- end coordinate y2 6, imagesetpixel image point function imagesetpixel (resource image, int x, int y, int color) 7, imagettftext with font writing function imagettftext (resource image, float size, float angle, int x, int y, int color, string fontfile, string text) 8, php verification code to insert a Chinese method iconv ("gb2312", "UTF-8", "string"); // first, convert the text to UTF-8 format 9, random function 1, rand ([int min, int max]) // rand () generates numbers 1-4 2, dechex (decimal number) // converts to hexadecimal To generate a verification code: generate a random number -- create an image -- write a random number as an image -- save it to the session Enter the verification code example gdchek. php
- /*
- * Generate an image verification code
- * And open the template in the editor.
- */
- Session_start ();
- For ($ I = 0; $ I <4; $ I ++ ){
- $ Rand. = dechex (rand (); // generates a 4-digit random number containing a hexadecimal number.
- }
- $ _ SESSION [check_gd] = $ rand;
- $ Img = imagecreatetruecolor (); // create an image
- $ Bg = imagecolorallocate ($ img, 0); // The first generated background color
- $ Fc = imagecolorallocate ($ img, 255,255,255); // generated font color
- // Draw a line for the image
- For ($ I = 0; $ I <3; $ I ++ ){
- $ Te = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
- Imageline ($ img, rand (0,15), 0,100, 30, $ te );
- }
- // Draw images
- For ($ I = 0; I I <200; $ I ++ ){
- $ Te = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
- Imagesetpixel ($ img, rand () % 100, rand () % 30, $ te );
- }
- // First, convert the text to UTF-8 format
- // $ Str = iconv ("gb2312", "UTF-8", "heheh ");
- // Add Chinese verification
- // Smkai. ttf is a font file. in order to play a font role in other people's computers, put the file in the project root directory, you can download it, and the local C: \ WINDOWS \ Fonts has
- Imagettftext ($ img, 11,10, 20,20, $ fc, "simkai. ttf", "Hello ");
- // Write the string in the image
- // Imagestring ($ img, rand (), $ rand, $ fc );
- // Output image
- Header ("Content-type: image/jpeg ");
- Imagejpeg ($ img );
- ?>
Login. php
- /*
- *
- *
- */
- Session_start ();
- If ($ _ POST [sub]) {
- // Determine whether the verification code is the same
- If ($ _ POST [gd_pic] ==$ _ SESSION [check_gd]) {
- Echo "verification successful! ";
- } Else {
- Echo "incorrect verification code ";
- }
- }
- ?>
|