PHP Chinese character validation code class function

Source: Internet
Author: User
Tags foreach border color chr getcolor gettext lowercase rand strlen

<?php

002 Class Captcha

003 {

004 Private $_about = Array (

005 ' imageline ' =>8,//Line interference, normal for 0

006 ' Imagepixel ' =>300,//pixel interference, normal for 0

007 ' Location ' =>true,//font random position, false normal

008 ' bgcolor ' => ' #ffffff ',//background

009 ' TextColor ' => ',//text

010 ' borcolor ' => ' #e1e1e1 ',//border

011 ' linecolor ' => ' #dedede ',//Line

012 ' pixelcolor ' => ' #646464 '/pixel

013);

014

015/**

016 * Construction <span Class=t_tag onclick=tagshow (event) href= "TAG.PHP?NAME=%BA%AF%CA%FD" > Functions </SPAN>

017 */

018 Public Function __construct () {}

019

020/**

021 * Setcookie

022 *

023 * @param string $data write <span Class=t_tag onclick=tagshow (event) href= "TAG.PHP?NAME=%CA%FD%BE%DD" > Data </ Span>

024 */

025 Public Function Setcookie ($data)

026 {

027 if (Is_array ($data)) {

028 $string = ';

029 foreach ($data as $v) $string. = $v;

030 $data = $string;

031}

032 Setcookie (' Captcha ', $data, Time () +60, '/');

033}

034/**

035 * Setcaptcha

036 *

037 * Set Output <span Class=t_tag onclick=tagshow (event) href= "TAG.PHP?NAME=%D1%E9%D6%A4" > Validation </SPAN> Code

038 *

039 * @param integer $width width

040 * @param integer $height high

041 * @param integer $fontSize font size

042 * @param string $font font <span class=t_tag onclick=tagshow (event) href= "Tag.php?name=%ce%c4%bc%fe" > Files </ Span> position

043 * @param integer $length validation <span class=t_tag onclick=tagshow (event) href= "TAG.PHP?NAME=%D7%D6%B7%FB" > Characters </ Span> length

044 * @param integer $type verify character type

045 * 1: Only digits, 2: Only lowercase letters, 3: only capitals,

046 * 4: Lowercase alphanumeric mixed, 5: Uppercase and Digital mixed, 6: uppercase and lowercase numerals mixed, 7: Kanji

047 * @param array $about Verification code configuration

048 */

049 Public Function Setcaptcha ($width =80, $height =30, $fontSize =13, $font = ' Code.ttf ', $length =4, $type =3, $about =array ( ))

050 {

051 Is_array ($about) | | Exit (' parameter Error! ');

052 foreach ($about as $key => $value) {

053 if (array_key_exists ($key, $this->_about)) {

054 $this->_about[$key] = $value;

055}

056}

15V

058 Extract ($this->_about,extr_overwrite);

059 $im = Imagecreatetruecolor ($width, $height);

060//Background color

061 $bgC _arr = $this->getcolor ($bgColor);

062 $imgBgColor = imagecolorallocate ($im, $bgC _arr[0], $bgC _arr[1], $bgC _arr[2]);

063//Border color

064 $borC _arr = $this->getcolor ($borColor);

065 $imgBorderColor = imagecolorallocate ($im, $borC _arr[0], $borC _arr[1], $borC _arr[2]);

066//Fill

067 Imagefill ($im, 0,0, $imgBgColor);

068 Imagerectangle ($im, 0,0, $width-1, $height-1, $imgBorderColor);

16X

070//Draw Line

071 if ($imageLine) {

072//Line Color

073 $lineC _arr = $this->getcolor ($lineColor);

074 $imgLineColor = imagecolorallocate ($im, $lineC _arr[0], $lineC _arr[1], $lineC _arr[2]);

075 for ($i =0; $i < $imageLine; $i + +) {

076 Imageline ($im, 0,mt_rand (0, $height), $width, Mt_rand (0, $height), $imgLineColor);

077}

078}

079//Pixel interference

080 if ($imagePixel) {

081//Pixel color

082 $pixelC _arr = $this->getcolor ($pixelColor);

083 $imgPixelColor = Imagecolorallocate ($im, $pixelC _arr[0], $pixelC _arr[1], $pixelC _arr[2]);

084 for ($i =0; $i < $imagePixel; $i + +) {

085 Imagesetpixel ($im, Mt_rand (2, $width-2), Mt_rand (2, $height-2), $imgPixelColor);

086}

087}

088

089//Get string

090 if (! $text = $this->gettext ($type, $length)) exit (' The CAPTCHA does not find the specified character type! ');

091//Check font file

092 is_file ($font) | | Exit (' Authenticode did not find the specified font file! ');

093 $y = ceil ($height/2) +ceil ($fontSize/2) *0.9;

094 $pitch = ceil ($width/$length);

095 $text = Ctype_alnum ($text) $text: Str_split ($text, 3);

096 $pad = 0;

097 for ($i =0; $i < $length; $i + +) {

098 if (!empty ($textColor)) {

099//Text color

$txtC _arr = $this->getcolor ($textColor);

$imgTextColor = Imagecolorallocate ($im, $txtC _arr[0], $txtC _arr[1], $txtC _arr[2]);

102} else {

$imgTextColor = Imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));

104}

if (false = = $location) {

$fontSizePad = Ctype_alnum ($text [$i]) $fontSize *1.4: $fontSize *1.7;

$x = $pad + $fontSize + (Ceil ($pitch/2)-$fontSizePad);

108 Imagettftext ($im, $fontSize, 0, $x, $y, $imgTextColor, $font, $text [$i]);

109 $pad + + $pitch;

The. else {

$d = $pad + + $pitch;

112 $d + + $fontSize *0.5;

113 Imagettftext ($im, $fontSize, Mt_rand ( -20,20), Mt_rand ($d-= $pitch, $pad-$fontSize *1.4), $y, $imgTextColor, $font, $ text[$i]);

114}

115}

116//write to <span Class=t_tag onclick=tagshow (event) href= "Tag.php?name=session" >session</SPAN>

117 $this->setcookie ($text);

118 $this->getcaptcha ($im);

119}

120

121/**

122 * GetText

123 *

124 * Get string

125 *

126 * @param integer $type String type

127 * @param integer $length length

128 * @return string $result strings Result

129 */

130 Public Function GetText ($type, $length)

131 {

132 $result = ';

$range = Array ();

134 switch ($type) {

135 Case 1:

136 for ($i =0; $i < $length; $i + +) $result. = Mt_rand (0,9);

137 break;

138 Case 2:

139 $range = Range (' A ', ' Z ');

140 break;

Case 3:

$range = Range (' A ', ' Z ');

143 break;

144 Case 4:

145 $range = Array_merge (range (0,9), Range (' A ', ' Z '));

146 break;

147 Case 5:

148 $range = Array_merge (range (0,9), Range (' A ', ' Z '));

149 break;

Case 6:

151 $range = Array_merge (range (0,9), Range (' A ', ' Z '), Range (' A ', ' Z '));

152 break;

153 Case 7:

154/for ($i =0; $i < $length; $i + +) $result. = Iconv (' gb2312 ', ' Utf-8 ', Chr (rand (0XB0,0XF7)). Chr (rand (0XA1,0XFE));

$range = Array (

156 ' Tong ', ' Love ', ' Philip ', ' Can ', ' Day ', ' one ', ' new ', ' he ', ' policy ', ' rhyme ', ' Year ', ' Ming ', ' ritual ', ' Jay ', ' Ya ', ' poem ', ' endure ', ' son ',

157 ' good ', ' fast ', ' Le ', ' half ', ' Beauty ', ' worship ', ' light ', ' minister ', ' incense ', ' middle ', ' country ', ' open ', ' heart ', ' ah ', ' laugh ', ' figure ', ' Cheng ', ' Tiger '

158);

159 break;

160 Default:return false;

161}

162 if (!empty ($range)) {

163 Shuffle ($range);

164 $result = Mb_substr (Implode (', $range), 0, $length, ' utf-8 ');

165}

166 return $result;

167}

168

169/**

170 * GetColor

171 *

172 * hexadecimal color converted to decimal

173 */

174 Public Function GetColor ($string)

175 {

176 $string = Str_replace (' # ', ', ', $string);

177 if (!ctype_alnum ($string)) {

178 return false;

179}

180 $rgb _arr = Array ();

181 if (strlen ($string) = = 3) {

=0 for ($i; $i <3; $i + +) {

183 $rgb _arr[$i] = Hexdec (Str_repeat ($string {$i}, ' 2 '));

184}

185} else if (strlen ($string) = = 6) {

186 $colorSp = Str_split ($string, 2);

187 foreach ($colorSp as $v) {

188 $rgb _arr[] = Hexdec ($v);

189}

190} else {

191 return false;

192}

193 return $rgb _arr;

194}

195

196 Public Function Getcaptcha ($IM)

197 {

198 header (' content-type:image/gif ');

199 imagegif ($im);

Imagedestroy ($im);

201}

202}

203

204 $about = Array (

205 ' imageline ' =>0,//Line interference, normal for 0

206 ' Imagepixel ' =>300,//pixel interference, normal for 0

207 ' location ' =>false,//font random position, false normal

' bgcolor ' => ' #ffffff ',//Background color

209 ' TextColor ' => ',//text color, default random color

210 ' borcolor ' => ' #e1e1e1 ',//Border color

211 ' LineColor ' => ' #ff0000 ',//Line Color

212 ' pixelcolor ' => ' #a13e3e '//Pixel color

213);

214 error_reporting (0);

215 $a = new Captcha ();

216 $a->setcaptcha (100,30,13, ' Font.ttf ', 4,1, $about);

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.