PHP ?? ????(???? ??)
?? ??? ?? ?? ??? ??? ? ??????.
??? ?? ??? ??. ? ?? ??? ??? ???? ????? ?? ????? ???? ?? ?? ? ??? ?? ??? ??? ? ????.
?? ??? ??? ???? ??? ???? ??? ???? ??? ?? ?? ???? ?? ?? ????? ???? ?? ? ?????.
????? ??? ???????.
?? ??? ???? ?? ??? ??? ?????.
?? ??:
1. ??? ??? ??? ???? ?????
2. ???? ? ???? ?????
3. ?? time ???? ??? ??? ??(?? ??)?? ???? ???
4. ???? ??? ?? ??? ????(??? ?, ??? ?, ??? ? ?? ????? ???? ??? ??? ???). ????? ??? ?? Visual)
5. 4 ?? ??
6. ??? ???? ????? ?? ??? ?? ????? ??
7. ??? ??
8. ??? ??? ??
? ????? ?? ?? ??? ?? ??? ???? ?????. ????? ? ????? ???? ??? ?? ?????.
1??, ??? ???
$img = imagecreate($width, $height);
??? ??? ??? ??? ? ????. ??? ??? ??? ?? ??? ??? ?????. ??? ???? ???? ???? ??? ??? ? ????.
function check_code($width = 100, $height = 50) { $img = imagecreate($width, $height); }
? ?? ??? ?? ??? ???? ???? ???? ????.
?? ?? ????? ????? ??? ??? ?????. ?? ?? ??? ???? ??. 0-9A-Za-Z? ??? ? ????. ??? ??? 0? o, l? I? ??? ???? ?? ?? ????. ??? ??? ??? ?? ??? ?? ? ????:
??? 1:
4?? ASCII ??, ASCII ?? ? chr ?? sprintf('%c'? ?????. , ? ?? ?? ??? ASCII ??? ???? ?? ??? ??? ??? ?????.
for ($i = 0; $i < $num; $i++) { $rand = mt_rand(0, 2); switch ($rand) { case 0: $ascii = mt_rand(48, 57); //0-9 break; case 1: $ascii = mt_rand(65, 90); //A-Z break; case 2: $ascii = mt_rand(97, 122); //a-z break; } //chr() $string .= sprintf('%c', $ascii); }
?? 2:
?? 1? ?? ????? ?? ???? ascci ??? ???? ??? ???? ????. ? ??? ???? ????? ?? ????. ??? ??? ?? str_shuffle? ???? ??? ?? ?? substr? ???? ??? ? ????.
//沒有0,i,l,o $str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789'; $str = str_shuffle($str); $string = substr($str,0 ,3);
3. ??? ??? ??? ??? ??(?? ??)?? ???? ???.
???? ?????. ??? ???? ?? ?? ???? ? ? ????.
??? RGB ?? ??? ????? ?????. RGB ??? 3?? ?????, ? 3?? ???? 0~255???.
???:
0-120 ?? ?? ??? ?????.
130 - 255? ????? ?? ?????.
???? ???? ?? ????. ??? ?? ?? ??? ??? ? ????.
//淺色的背景函數(shù) function randBg($img) { return imagecolorallocate($img, mt_rand(130, 255), mt_rand(130, 255), mt_rand(130, 255)); } //深色函數(shù),深色的字或者點這些干 擾元素 function randPix($img) { return imagecolorallocate($img, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); }
4. ???? ??? ?? ??? ????
???? 50??? ???? ?? ? ????. ?? ?? ??? 0,0???. ?? ? ??? ?? ? ?? ?? ?? ? ?????.
?? ?? mt_rand(0, ?? ??), mt_rand(0, ?? ??)? ?????. ?? ?? randPix? ???? ??? ?? ???? ??? ?????.
//畫干擾元素 for ($i = 0; $i < 50; $i++) { imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), randPix($img)); }
5. 4?? ??
$string? ?????, $string[0]? ???? ? ?? ?????. ?? ??? ?? ?.
???? imagechar ??? ???? ???? ???? ? ? ????.
??? ???? ??? ???? x ? y ??? ??? ? ????.
X ?? = ??? ??/?? ?(4) * ?? ?. ?? ??? ????? ? ??? ???? ??? ?????. ???? ??? 100??? ???? ? ?? ??? 0, ? ?? ??? 25, ? ?? ??? 50, ? ?? ??? 75? ?????.
Y ?? = mt_rand(0,??? ?? - 15).
?? ??? ???? ?? ???? ??? ? ????.
for ($i = 0; $i < $num; $i++) { $x = floor($width / $num) * $i; $y = mt_rand(0, $height - 15); imagechar($img, 5, $x, $y, $string[$i], randPix($img)); }
6. ??? ???? ????? ?? ??? ?? ????? ?????.
??? ??? ?? ???? imagejpeg, imagepng, imagegif ? ?? ??? ??? ?? ?? ????.
???? MIME ??? image/jpeg, image/png, image/gif ????.
??? ??? ??? ? ????:
$imagetype = 'jpeg';
$imagetype = 'png';
$imagetype = 'gif';
?? ?? ?? ? ??:
$header = 'Content-type:image/' . $imagetype;
?? ??? ??? ?? ??:
$func = 'image' . $type;
???? ??? ???? ?? function_exists? ???? ??? ????? ?????. ???? ?? ???? ? ??? ???? ???? ?? ?? ? ??? ???? ????.
??? ??? ??? ?? ??? ? ????:
$func = 'image' . $type; $header = 'Content-type:image/' . $type; if (function_exists($func)) { header($header); //變?yōu)榱薸magejpeg等 $func($img); } else { echo '圖片類型不支持'; }
8. ???? ???? ?? ??? ?????.
??? ????? ??? ?? ??? ???? ???. ??, ???? ??? ????? ???? ????? ???? ???.
??? ??? ????? ?? ??? ???? ???.
imagedestroy($img); return $string;
??? ?? PHP ?? ??? ???????. ? ??? ?? ??? ??? ??? ? ??? ?? ??? ????????.
<?php check_code(); function check_code($width = 100, $height = 50, $num = 4, $type = 'jpeg') { $img = imagecreate($width, $height); $string = ''; for ($i = 0; $i < $num; $i++) { $rand = mt_rand(0, 2); switch ($rand) { case 0: $ascii = mt_rand(48, 57); //0-9 break; case 1: $ascii = mt_rand(65, 90); //A-Z break; case 2: $ascii = mt_rand(97, 122); //a-z break; } //chr() $string .= sprintf('%c', $ascii); } //背景顏色 imagefilledrectangle($img, 0, 0, $width, $height, randBg($img)); //畫干擾元素 for ($i = 0; $i < 50; $i++) { imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), randPix($img)); } //寫字 for ($i = 0; $i < $num; $i++) { $x = floor($width / $num) * $i + 2; $y = mt_rand(0, $height - 15); imagechar($img, 5, $x, $y, $string[$i], randPix($img)); } //imagejpeg $func = 'image' . $type; $header = 'Content-type:image/' . $type; if (function_exists($func)) { header($header); $func($img); } else { echo '圖片類型不支持'; } imagedestroy($img); return $string; } //淺色的背景 function randBg($img) { return imagecolorallocate($img, mt_rand(130, 255), mt_rand(130, 255), mt_rand(130, 255)); } //深色的字或者點這些干 擾元素 function randPix($img) { return imagecolorallocate($img, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); } ?>