


Generate intricate (tilted, sinusoidal, pasted, rotated) verification codes
Mar 24, 2018 pm 05:52 PMThis time I will bring you the method of generating intricate (inclined, sinusoidal interference lines, pasting, rotation verification) verification codes, and generating intricate (tilt, sinusoidal interference lines, pasting, rotation verification) verification codes What are the precautions? The following is a practical case, let’s take a look.
Verification code used by many websites now is very easy to be automatically recognized by software. This article introduces a PHP-generated complex verification code that adds effects such as tilting, string interference lines, pasting, and rotation.
There are three common verification code methods that are not easily recognized by software:
1. Font variation (generally distorted through algorithms)
2. Paste fonts (the QQ verification code is represented here, it is still difficult to find online, crack the QQ verification code)
3 , interference lines, noise (this kind of identification is quite easy and can be automatically identified by the program)
As mentioned above, the first and second methods are more difficult to identify.
Specific implementation code:
<?php /** *帶文字旋轉(zhuǎn),傾斜,黏貼,加正弦干擾線驗(yàn)證碼* */ class Utils_Caption { var $Width = 60; //圖片寬 var $Height = 30; //圖片高 var $Length = 4; //驗(yàn)證碼位數(shù) var $BgColor = "#FFFFFF"; //背景色 var $TFonts = array("font.ttf"); var $TFontSize=array(17,20); //字體大小范圍 var $TFontAngle=array(-20,20); //旋轉(zhuǎn)角度 var $Chars = "0123456789"; //驗(yàn)證碼范圍(字母數(shù)字) var $Code = array(); //驗(yàn)證碼 var $Image = ""; //圖形對(duì)象 var $FontColors=array('#f36161','#6bc146','#5368bd'); //字體顏色,紅綠藍(lán) var $TPadden = 0.75;///字符間距,多少個(gè)字符 var $Txbase = 5;///x軸兩邊距離 var $Tybase =5 ;///y軸兩邊距離 var $TLine =true; ///畫干擾線 public function RandRSI() ///生成驗(yàn)證碼 { $this->TFontAngle=range($this->TFontAngle[0],$this->TFontAngle[1]); $this->TFontSize=range($this->TFontSize[0],$this->TFontSize[1]); $arr=array(); $Chars=$this->Chars; $TFontAngle=$this->TFontAngle; $TFontSize=$this->TFontSize; $FontColors=$this->FontColors; $code=""; $font=dirname(FILE)."/font/".$this->TFonts[0]; $charlen=strlen($Chars)-1; $anglelen=count($TFontAngle)-1;?//?角度范圍 $fontsizelen=count($TFontSize)-1;?//?角度范圍 $fontcolorlen=count($FontColors)-1;?//?角度范圍 for($i=0;$i<$this->Length;$i++)?///得到字符與顏色 { $char=$Chars[rand(0,$charlen)];?///得到字符 $angle=$TFontAngle[rand(0,$anglelen)];?///旋轉(zhuǎn)角度 $fontsize=$TFontSize[rand(0,$fontsizelen)];?///字體大小 $fontcolor=$FontColors[rand(0,$fontcolorlen)];?///字體大小 $bound=$this->_calculateTextBox($fontsize,$angle,$font,$char);?///得到范圍 $arr[]=array($fontsize,$angle,$fontcolor,$char,$font,$bound);?///得到矩形框 $code.=$char; } $this->Code=$arr;?//驗(yàn)證碼 return?$code; } public?function?Draw()?///畫圖 { if(empty($this->Code))?$this->RandRSI(); $codes=$this->Code;?///用戶驗(yàn)證碼 $wh=$this->_getImageWH($codes); $width=$wh[0]; $height=$wh[1];?///高度 $this->Width=$width; $this->Height=$height; $this->Image?=?imageCreate(?$width,?$height?); $image=$this->Image; $back?=?$this->_getColor2($this->_getColor(?$this->BgColor));?///背景顏色 imageFilledRectangle($image,?0,?0,?$width,?$height,?$back);?///填充背景 $TPadden=$this->TPadden; $basex=$this->Txbase; $color=null; foreach?($codes?as?$v)?///逐個(gè)畫字符 { $bound=$v[5]; $color=$this->_getColor2($this->_getColor($v[2])); imagettftext($image,?$v[0],?$v[1],?$basex,?$bound['height'],$color?,?$v[4],?$v[3]); $basex=$basex+$bound['width']*$TPadden-$bound['left'];///計(jì)算下一個(gè)左邊距 } $this->TLine?$this->_wirteSinLine($color,$basex):null;?///畫干擾線 header("Content-type:?image/png"); imagepng(?$image); imagedestroy($image); } /** ?*通過(guò)字體角度得到字體矩形寬度* ?* ?*?@param?int?$font_size?字體尺寸 ?*?@param?float?$font_angle?旋轉(zhuǎn)角度 ?*?@param?string?$font_file?字體文件路徑 ?*?@param?string?$text?寫入字符 ?*?@return?array?返回長(zhǎng)寬高 ?*/ private?function?_calculateTextBox($font_size,?$font_angle,?$font_file,?$text)?{ $box?=?imagettfbbox($font_size,?$font_angle,?$font_file,?$text); $min_x?=?min(array($box[0],?$box[2],?$box[4],?$box[6])); $max_x?=?max(array($box[0],?$box[2],?$box[4],?$box[6])); $min_y?=?min(array($box[1],?$box[3],?$box[5],?$box[7])); $max_y?=?max(array($box[1],?$box[3],?$box[5],?$box[7])); return?array( 'left'?=>?($min_x?>=?-1)???-abs($min_x?+?1)?:?abs($min_x?+?2), 'top'?=>?abs($min_y), 'width'?=>?$max_x?-?$min_x, 'height'?=>?$max_y?-?$min_y, 'box'?=>?$box ); } private?function?_getColor(?$color?)?//#ffffff { return?array(hexdec($color[1].$color[2]),hexdec($color[3].$color[4]),hexdec($color[5].$color[6])); } private?function?_getColor2(?$color?)?//#ffffff { return?imagecolorallocate?($this->Image,?$color[0],?$color[1],?$color[2]); } private?function?_getImageWH($data) { $TPadden=$this->TPadden; $w=$this->Txbase; $h=0; foreach?($data?as?$v) { $w=$w+$v[5]['width']*$TPadden-$v[5]['left']; $h=$h>$v[5]['height']?$h:$v[5]['height']; } return?array(max($w,$this->Width),max($h,$this->Height)); } //畫正弦干擾線 private?function?_wirteSinLine($color,$w) { $img=$this->Image; $h=$this->Height; $h1=rand(-5,5); $h2=rand(-1,1); $w2=rand(10,15); $h3=rand(4,6); for($i=-$w/2;$i<$w/2;$i=$i+0.1) { $y=$h/$h3*sin($i/$w2)+$h/2+$h1; imagesetpixel($img,$i+$w/2,$y,$color); $h2!=0?imagesetpixel($img,$i+$w/2,$y+$h2,$color):null; } } }
DEMO code:
$rsi = new Utils_Caption(); $rsi->TFontSize=array(15,17); $rsi->Width=50; $rsi->Height=25; $code?=?$rsi->RandRSI(); session_start(); $_SESSION["CHECKCODE"]?=?$code; $rsi->Draw();
Running effect:
Everyone can Modify the corresponding values ??in the code to achieve the complexity you need
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
php retains key value + merged array detailed explanation
How to remove duplicates in a two-dimensional array value
The above is the detailed content of Generate intricate (tilted, sinusoidal, pasted, rotated) verification codes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez
