In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how php implements image CAPTCHA. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Php to achieve picture CAPTCHA method: 1, load GD extension; 2, create canvas and add content on the canvas; 3, save output through imagepng; 4, release resources; 5, generate random CAPTCHA data.
This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer.
How does php realize the picture verification code?
PHP realizes the function of picture verification code
CAPTCHA: captcha is a technology used to zone other people and computers.
Principle (Completely Automated Public Turing Test to Tell Computers and Humans Apart (Turing test that automatically distinguishes computers from humans)
How to distinguish between computers and humans?
As long as it is textual, the computer can recognize it; the computer cannot recognize the content of the picture, but it is very easy for the human eye to recognize the content of the picture.
The essence of CAPTCHA: print textual content on the picture to make the difference between computer and human.
First, expand the understanding of the picture
PHP itself cannot manipulate the picture.
PHP can use the provided image extension to manipulate images.
There are many image extensions: GD is commonly used.
Load GD extensions: all GD extension functions begin with image
2. PHP operation picture
1. Add canvas (create canvas)
Picture resource imagecreatetruecolor (width, height)
two。 Add content (text) to the canvas
A) assign colors to the content to be added to the picture: associate the color to the picture resource before you can use the
Color handle [integer] imagecolorallocate (picture resources, red, green, blue); / / colors can use the numbers 0-255or hexadecimal # hexadecimal
B) Writing text: can only write in English (ASCII code table)
Boolean imagestring (picture resource, text size, starting X coordinate, starting Y coordinate, writing content, color)
Font size: 1-5
3. Save output
Imagepng (picture resource [, save location])
4. Release resources (recommended release of resources)
Boolean result imagedestroy (picture resource)
/ / 1. Create canvas $img = imagecreatetruecolor (200200); / / var_dump ($img); / / 2. Paint / / 2.1assign colors to the canvas $color = imagecolorallocate ($img,255255255); / / echo $color;//2.2 write words $true = imagestring ($img,5,50,90,'hello world',$color); / / var_dump ($true); / / 3. Save the output / / 3.1 output / / tell the browser that the content is the picture / / header ('Content-type:image/png'); / / imagepng ($img); / / 3.2Save the picture imagepng ($img,'hello.png'); / / Save to the current directory / / 4. Release resources $res = imagedestroy ($img); var_dump ($res); III. CAPTCHA picture
1. Generate random CAPTCHA data
two。 Create a canvas
3. Fill background color: imagefill (picture resource, start position X, start position Y, color handle); / / imagefill: after finding a pixel, if you find that the adjacent pixels are the same color as the current pixel (all black) will be automatically rendered.
4. Add text content: assign colors first
5. Save output: CAPTCHA is all output
6. Release resources
/ / make a CAPTCHA picture / / get the CAPTCHA string $captcha =''; for ($I = 0posiposii
< 4;$i++){ //chr: 将数字转换成对应的字符(ASCII) switch(mt_rand(0,2)){ case 0: //数字 $captcha .= chr(mt_rand(49,57)); break; case 1: //大写字母 $captcha .= chr(mt_rand(65,90)); break; case 2: //小写字母 $captcha .= chr(mt_rand(97,122)); break; }}//echo $captcha;//创建画布$img = imagecreatetruecolor(200,200);//给背景分配颜色$bg = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));//填充背景色imagefill($img,0,0,$bg);//循环写入for($i = 0;$i < 4;$i++){ //分配文字颜色 $txt = imagecolorallocate($img,mt_rand(50,150),mt_rand(50,150),mt_rand(50,150)); //写入文字 imagestring($img,mt_rand(1,5),60 + $i*20,90,$captcha[$i],$txt);}//输出header('Content-type:image/png');imagepng($img);//释放资源imagedestroy($img);四、中文验证码 两个注意点 获取随机中文: 在PHP中都是以字节为单位操作数据,中文在不同字符集中有不同字节数 中文写入函数: imagettftext(图片资源,字体大小, 字体旋转角度, 字体的起始X,字体起始Y, 字体文件,内容, 颜色); 1.创建画布: 填充背景色 2.获取随机中文 3.将中文写入到图片 4.保存输出图片 5.销毁资源 //中文验证码header('Content-type:text/html;charset=utf-8');//创建画布$img = imagecreatetruecolor(200,200);//给背景分配颜色$bg = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));//填充背景色imagefill($img,0,0,$bg);//写入内容for($i = 0;$i < 4;$i++){ //分配颜色 $txt = imagecolorallocate($img,mt_rand(50,150),mt_rand(50,150),mt_rand(50,150)); //获取随机中文 $string = "今天我寒夜里看雪飘过怀着冷却了的心窝飘远方"; $pos = mt_rand(0,strlen($string) - 1); $start = $pos - $pos % 3; //utf-8取模3,GBK取模2 //取三个长度(字符串截取) $target = substr($string,$start,3); //写入中文 imagettftext($img,mt_rand(20,40),mt_rand(-45,45),40 + $i * 30, mt_rand(80,120),$txt,'simple.ttf',$target);}//输出图片header('Content-type:image/png');imagepng($img);//销毁资源imagedestroy($img);五、封装验证码类//验证码工具类class Captcha{ //属性 private $width; private $height; private $strlen; private $lines; //干扰线数量 private $stars; //干扰点数量 private $font; //字体路径 //构造方法:初始化属性 public function __construct($info = array()){ //初始化属性 $this->Width = isset ($info ['width'])? $info [' width']: 146; $this- > height = isset ($info ['height'])? $info [' height']: 23; $this- > strlen = isset ($info ['strlen'])? $info [' strlen']: 4; $this- > lines = isset ($info ['lines'])? $info [' lines']: 10 $this- > stars = isset ($info ['stars'])? $info [' stars']: 50; $this- > font = isset ($info ['font'])? $info [' font']: 'fonts/AxureHandwriting-BoldItalic.otf';} / / generate CAPTCHA image public function generate () {/ / create canvas, given background color $img = imagecreatetruecolor ($this- > width,$this- > height) $c_bg = imagecolorallocate ($img,mt_rand (200255), mt_rand (200255), mt_rand (200255)); imagefill ($img,0,0,$c_bg); / / write the string $captcha = $this- > getStr (); / / add the interference point "*" for ($I = 0
< $this->Stars;$i++) {/ / Random colors $c_star = imagecolorallocate ($img,mt_rand (100150), mt_rand (100150), mt_rand (100150)); / / write * imagestring ($img,mt_rand (1L5), mt_rand (0height-> width-> height),'*', $c_star) } / / increase the interference line for ($I = 0
< $this->Lines;$i++) {/ / Random colors $c_line = imagecolorallocate ($img,mt_rand (150200), mt_rand (150200), mt_rand (150200)); / / underlined imageline ($img,mt_rand (0memoritithis-> width), mt_rand (0maxithis-> height), mt_rand (0memthis-> width), mt_rand (0memthis- > height), $c_line) } / / Random color for ($I = 0
< $this->Strlen;$i++) {$c_str = imagecolorallocate ($img,mt_rand (0100), mt_rand (0100), mt_rand (0100)); imagettftext ($img,mt_rand (10) 20), mt_rand (- 45), 20 + $I * 30 height (14) > font,$captcha ($I) } / / output picture header ('Content-type:image/png'); imagepng ($img); / / release resource imagedestroy ($img);} / / get random string / / @ return string private function getStr () {/ / ASCII code table generates string $str ='' / / generate for in a loop ($I = 0positioni
< $this->Strlen;$i++) {/ / randomly select numbers, lowercase letters and uppercase letters switch (mt_rand (0Magne2)) {case 0: / / numeric $str. = chr (mt_rand (49mem57)); break Case 1: / / lowercase $str. = chr (mt_rand (97122)); break; case 2: / / uppercase $str. = chr (mt_rand (65)); break }} / / Save the verification code string to session $_ SESSION ['captcha'] = $str; / / return the result return $str } / * * verification code * @ param1 string $captcha, the verification code data entered by the user * @ return boolean, returned true successfully, and failed to return false * / public static function checkCaptcha ($captcha) {/ / verify with the verification code in session / / the verification code is not case-sensitive return (strtoupper ($captcha) = = strtoupper ($_ SESSION ['captcha'])) Thank you for your reading! This is the end of this article on "how to achieve picture verification code in php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 288
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.