File Header ...
- Header ("Content-type:image/png");
- Create True Color white paper
- $im = @imagecreatetruecolor (a) or Die ("Build image Failed");
- Get background color
- $background _color = imagecolorallocate ($im, 255, 255, 255);
- Fill the background color (this thing is like oil barrels)
- Imagefill ($im, 0,0, $background _color);
- Get border color
- $border _color = imagecolorallocate ($im, 200,200,200);
- Draw a rectangle, border color 200,200,200
- Imagerectangle ($im, 0,0,49,19, $border _color);
Line up background, full screen with 1 or 0
- for ($i =2; $i <18; $i + +) {
- Get random tint
- $line _color = Imagecolorallocate ($im, Rand (200,255), Rand (200,255), Rand (200,255));
- Draw Line
- Imageline ($im, 2, $i, $i, $line _color);
- }
Set Font size
- $font _size=12;
Set up the printed text
- $Str [0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- $STR [1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- $STR [2] = "01234567891234567890123456";
Get a 1th random text
- $imstr [0]["s"] = $Str [rand (0,2)][rand (0,25)];
- $IMSTR [0]["x"] = rand (2,5);
- $imstr [0]["y"] = rand (1,4);
Get a 2nd random text
- $imstr [1]["s"] = $Str [rand (0,2)][rand (0,25)];
- $IMSTR [1]["x"] = $imstr [0]["x"]+ $font _size-1+rand (0,1);
- $imstr [1]["y"] = rand (1,3);
Get a 3rd random text
- $imstr [2]["s"] = $Str [rand (0,2)][rand (0,25)];
- $IMSTR [2]["x"] = $imstr [1]["x"]+ $font _size-1+rand (0,1);
- $imstr [2]["y"] = rand (1,4);
Get a 4th random text
- $imstr [3]["s"] = $Str [rand (0,2)][rand (0,25)];
- $IMSTR [3]["x"] = $imstr [2]["x"]+ $font _size-1+rand (0,1);
- $imstr [3]["y"] = rand (1,3);
Write Random string
- for ($i =0; $i <4; $i + +) {
- Get a random darker color
- $text _color = Imagecolorallocate ($im, Rand (50,180), Rand (50,180), Rand (50,180));
- Drawing text
- Imagechar ($im, $font _size, $imstr [$i] ["X"], $imstr [$i] ["Y"], $imstr [$i] ["s"], $text _color);
- }
Show pictures
- Imagepng ($im);
- Destroying pictures
- Imagedestroy ($im);
- ?>
Copy CodeExample 2, a beautiful PHP image verification code example complete code:
- /*
- * @Author FY
- */
- $imgwidth = 100; Picture width
- $imgheight = 40; Picture 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, 0,0, $while); Fill image
- Get string
- $authstr = ";
- $_len = strlen ($charset)-1;
- for ($i =0; $i < $codelen; $i + +) {
- $authstr. = $charset [Mt_rand (0,$_len)];
- }
- Session_Start ();
- $_session[' Scode ']=strtolower ($AUTHSTR);//All lowercase, mainly for case insensitive
- Random draw points, has been changed to draw the stars
- 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);
- }
- Randomly draw lines, number of lines = number of characters (whatever)
- 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); Calculating character distances
- $_y=intval ($imgheight *0.7); The character is displayed in the position of picture 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], $color 3);
- 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 images
- Header ("Content-type:image/png");
- Imagepng ($im);
- Imagedestroy ($im);
Copy CodeExample 3,PHP5 Image captcha example code PHP5 generates an example of a picture verification code. Need to use PHP GD library function: 1,imagecreatetruecolor-----Create a true color image imagecreatetruecolor (int x_size,int y_size)//x is wide, y means high 2,imagecolorallocate assigns a color (palette) imagecolorallocate (Resource Image,int red,int green,int blue) to an image//red,green, Blue----RGB 3,imagestring drawing function iamgestring (resource image,font,int x,int y, content, color); 4, output function The header of PHP is the action that defines the head, PHP5 supports Type 3 in: 1,content-type:xxxx/yyyy 2,location:xxxx:yyyy/zzzz 3,status:nnn xxxxxx xxxx/yyyy indicates the type of content file such as: Image/gif Image/jpeg Image/png Example: Header ("Content-type:image/jpeg") in the GD library has the corresponding image type Imagejpeg (), Imagegif (), Imagepang () 5, Imageline Draw Line Function Iamgeline (resource image,int x1,int y1,int x2,int y2,int color); Image---Picture x1---start coordinates y1 x2---End coordinates y2 6,imagesetpixel draw point function imagesetpixel (resource image,int x,int y,int color) 7,imagettf Text with font write function Imagettftext (resource image,float size,float angle,int x,int y,int color,string fontfile,string text) 8,php verification code inserted in Chinese method iconv ("gb2312", "Utf-8", "string"); First, to convert the text to UTF-8 format 9, the random function 1,rand ([int min,int max])//rand (1,4) generates 1-4 of the number 2, Dechex (Ten inNumber)//Convert to Hex To generate a verification code: generate a random number--Create a picture--a random number as a picture--saved in session Enter the CAPTCHA example gdchek.php
- /*
- * Generate Image Verification code
- * and open the template in the editor.
- */
- Session_Start ();
- for ($i =0; $i <4; $i + +) {
- $rand. =dechex (rand (1,15)); Generates a 4-digit random number with 16 decimal digits
- }
- $_session[check_gd]= $rand;
- $img =imagecreatetruecolor (100,30); Create a picture
- $BG =imagecolorallocate ($img, 0,0,0); Background color is generated for the first time
- $FC =imagecolorallocate ($img, 255,255,255); The resulting font color
- Draw a line to a picture
- 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 points for pictures
- for ($i =0; $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", "ha hehe");
- Validation of adding Chinese
- Smkai.ttf is a font file, in order to be in other people's computer can play the role of the font, put the file to the root of the project, can be downloaded, there is a native C:\WINDOWS\Fonts
- Imagettftext ($img, 11,10,20,20, $FC, "Simkai.ttf", "Hello Hello");
- Write the string in the picture
- Imagestring ($img, Rand (1,6), Rand (3,70), Rand (3,16), $rand, $FC);
- Output picture
- Header ("Content-type:image/jpeg");
- Imagejpeg ($IMG);
- ?>
Copy Codelogin.php
- /*
- *
- *
- */
- Session_Start ();
- if ($_post[sub]) {
- Determine if the verification code is the same
- if ($_POST[GD_PIC]==$_SESSION[CHECK_GD]) {
- echo "Verified success! ";
- }else{
- echo "Verification code error";
- }
- }
- ?>
Copy Code |