<?php// 使用php操作gd庫做圖// 1. 創建一個畫布資源$im = imagecreatetruecolor(200, 50);// 2. 創建背景色// 2.1 得到背景顏色$bg_color = imagecolorallocate($im, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));// 2.2 填充畫布imagefill($im, 0, 0, $bg_color);// 3. 獲取驗證碼數據$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';$captcha = '';for( $i = 0; $i < 5; $i++ ){//隨機從字符串中取一個, 加入到captcha;$captcha .= $str[mt_rand(0, strlen($str) - 1)] . ' ';}// 增加干擾線for( $i = 0; $i < 10; $i++ ){// 1.得到干擾線顏色$line_color = imagecolorallocate($im, mt_rand(100, 200), mt_rand(100, 200), mt_rand(100, 200));// 2.畫線imageline($im, mt_rand(0, 200), mt_rand(0, 50), mt_rand(0, 200), mt_rand(0, 50), $line_color);}// 增加干擾點for( $i = 0; $i < 200; $i++ ){// 給點分配顏色$pixel_color = imagecolorallocate($im, mt_rand(100, 200), mt_rand(100, 200), mt_rand(100, 200));// 畫點imagesetpixel($im, mt_rand(0, 200), mt_rand(0, 50), $pixel_color);}// 4.將驗證碼寫入圖片// 4.1得到文字的顏色$str_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));// 4.2 將字符串寫入圖片imagestring($im, 5, 60, 20, $captcha, $str_color); // 5. 指定類型header('Content-type:image/png');// 6. 查看圖片// imagepng($im); imagepng($im, 'test2.png');// 7. 釋放資源imagedestroy($im);
?