Yan 18 Public Service PHP training class address: YY Channel 88354001 Learning Community: www.zixue.it
PHP Drawing: such as verification code, thumbnails, plus watermark to use the GD library, so to open the GD2 library, in order to use
First find php.ini in Extension=php_gd2.dll remove the front semicolon, and then restart Apache, create a picture can be used
/* General steps for drawing: 1. Create a canvas 2. Create a pigment 3. Paint or Write 4. Save 5. Destroy resources *//Create canvas $img =imagecreatetruecolor ( 300,200); Create pigments $bg =imagecolorallocate ($img, 30,255,255); Canvas Fill Color Imagefill ($img, 0,0, $BG); Save Picture if (Imagepng ($img, './01.png ')) {
Simple Verification Code
Create Picture $im = Imagecreatetruecolor (+); Set the background to blue $blue = Imagecolorallocate ($im, 100, 255, 255); Create pigments $imgcolor =imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); Fill background color Imagefill ($im, 0, 0, $blue); Draw the interference line for ($i =0; $i <4; $i + +) { imageline ($im, Rand (0,20), 0,100,rand (0,60), $imgcolor);} Draw noise for ($i =0; $i <100; $i + +) { imagesetpixel ($im, Rand (0,50), Rand (0,30), $imgcolor);} Write String $str =substr (str_shuffle (' ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789 '), 0,4); Imagestring ($im, 4,10,10, $str, $imgcolor);
Verification code Note Save format, UTF8 no BOM format, only save as UTF8 will appear garbled
The output random verification code is:
PHP notes GD library image creation/simple verification code