GD2使用图像处理技术生成验证码
程序员文章站
2022-06-23 18:37:49
...
一 代码
<?php header('content-type:image/png'); srand((double) microtime() * 1000000); $im = imagecreate(60, 20); imagefill($im, 0, 0, imagecolorallocate($im, 200, 200, 200)); //测试代码 //$validatorCode = $_GET['code']; //网站使用代码 $validatorCode = '1234'; imagestring($im, rand(2, 5), 10, 3, substr($validatorCode, 0, 1), imagecolorallocate($im, 0, rand(0, 255), rand(0, 255))); imagestring($im, rand(2, 5), 20, 3, substr($validatorCode, 1, 1), imagecolorallocate($im, rand(0, 255), 0, rand(0, 255))); imagestring($im, rand(2, 5), 30, 3, substr($validatorCode, 2, 1), imagecolorallocate($im, rand(0, 255), rand(0, 255), 0)); imagestring($im, rand(2, 5), 40, 3, substr($validatorCode, 3, 1), imagecolorallocate($im, 0, rand(0, 255), rand(0, 255))); for ($i = 0; $i < 200; $i ++) { imagesetpixel($im, rand() % 70, rand() % 30, imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255))); } imagepng($im); imagedestroy();
二 运行结果