"PHP Drawing technology && Verification Code Drawing"

Source: Internet
Author: User

PHP Drawing is a relatively simple thing, basic drawings such as lines, circles, rectangles, arcs, filled rectangles, filled fan, non-Chinese text printing, Chinese text printing in the code below will be a slender explanation.

Need to support Chinese fonts, can be found in the Windows own font library, and copied to the current directory, renamed to Font.ttf

1<?PHP2     //1. Create a canvas3     $im=imagecreatetruecolor (500,400);4     //2. Create a color5     $red=imagecolorallocate ($im, 0,255,0);6     //Draw a circle7Imageellipse ($im, 20,20,20,20,$red);8     //Draw a line9Imageline ($im, 0, 0,400,300,$red);Ten     //Draw a rectangle OneImagerectangle ($im, 0,0,40,50,$red); A     //Draw filled rectangles -Imagefilledrectangle ($im, 50,0,90,50,$red); -     //Draw an arc theImagearc ($im, 100,100,50,50,170,350,$red); -     //draw a solid sector -Imagefilledarc ($im, 300,200,50,50,80,210,$red,img_arc_pie); -     //Write + //write a child without Chinese characters first -     $str= "Hello,world!this is function imagestring!"; +Imagestring ($im, 5, 10,250,$str,$red); A     //another way to use text with Chinese characters at     $str= "hello,wrold! I am a thief of plum!" "; -Imagettftext ($im, 30,0,10,150,$red, "Font.ttf",$str); -     //output to a Web page or save to a file -     Header("Content-type:image/png"); -Imagepng ($im); -     //destroy picture free memory inImagedestroy ($im); -?>
View Code

The effect is as follows:

The drawing is generated by the code, but can be downloaded directly from the browser, according to the drawing of the method used in different, to save as a. jpg,. png,. gif format graphics, We generally use. png because the image in this format is clearer, while the PHP drawing technique is used primarily for drawing reports and drawing verification codes.

2. Attach the existing picture to the panel.

1<?PHP2 3     //1. Create a canvas4     $im=imagecreatetruecolor (500,500);5     //2. Create a color6     $red=imagecolorallocate ($im, 0,255,0);7 8     //Loading Pictures9     $srcimage=imagecreatefromjpeg ("1.jpg");//can also be loaded from PNG or GIFTen //Copy original image to canvas One      A //Get an array of picture information first.  -     $image _info_arr=getimagesize("1.jpg"); -Imagecopy ($im,$srcimage, 0,0,0,0,$image _info_arr[0],$image _info_arr[1]); the  -     //output to a Web page, or you can save it.  -     Header("Content-type:image/png"); -Imagepng ($im); +     //destroying pictures, freeing up memory -Imagedestroy ($im); +?>
View Code

Need Picture: 1.jpg, note that the image size is smaller than the size of the panel, otherwise it will error.

Effect:

3. Draw the verification code.

I have encapsulated it in a file and can be used directly when doing a project.

Aim.php: Provide the user input verification code graphical interface, which uses the JS technology to achieve the click Verification code to replace the image function.

1 <form> Please enter the CAPTCHA: <input type= "text" name= "Checkcode" >2 <!--using JS to implement the verification code automatically changed--3 </form>
View Code

Checkcode.php: Provides the process control code to draw the verification code.

1<?PHP2     //first define an empty string3     $checkCode="";4     //randomly generate four numbers and stitch them together5      for($i= 1;$i<=4;$i++)6     {7         $checkCode.=Rand(0,9);8     }9 Ten     //Start drawing Verification Code One  A //1. Creating a canvas -     $im=imagecreatetruecolor (45,25); -     //2. Randomly generate a color the     $color=imagecolorallocate ($im,Rand(0,255),Rand(0,255),Rand(0,255)); -     //$color =imagecolorallocate ($im, 255,0,0); -  - //3. Plotting interference lines +      for($i= 1;$i<=20;$i++) -     { +Imageline ($im, 0,Rand(0,24), 44,Rand(0,24), Imagecolorallocate ($im,Rand(0,255),Rand(0,255),Rand(0,255))); A     } at     //4. Draw a String -Imagestring ($im, 5,3,3,$checkCode,$color); -     Header("Content-type:image/png"); -Imagepng ($im); -  -     //4. Destroying Images inImagedestroy ($im); -?>
View Code

Effect:

"PHP Drawing technology && Verification Code Drawing"

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.