function writeImage($width, $height, $strLen, $fontSize, $angMax, $font, $penColorRGB, $backColorRGB) { $im = Imagecreate($width, $height); $backColorArray = explode(',', $backColorRGB); // Get RGB componenets // from string $penColorArray = explode(',', $penColorRGB); $backColor = imagecolorallocate($im, $backColorArray[0], $backColorArray[1], $backColorArray[2]); // allocate // color // for // background $penColor = imagecolorallocate($im, $penColorArray[0], $penColorArray[1], $penColorArray[2]); // allocate // color // for // writing $x = 20; // Start writing from $x with initial 20 pixels left blank $y = $fontSize + 20; $str = ""; // Declaration just to avoid "undefined variable" warning for ($i = 0; $i <= $strLen - 1; $i++) { $chr = getRandChr(); // Generate a random character $str .= "{$chr}"; // Append to a string, for storing to SESSION $angle = rand(0, $angMax); // Set angle between 0 to $angMax if (rand(0, 1) == 0) { // Set Positive or Negative angle randomly $angle = -$angle; } ImageTTFText($im, $fontSize, $angle, $x, $y, $penColor, $font, $chr); $x = $x + 50; // Increment in x coordinate } $_SESSION['CaptchaCode'] = md5($str); // Assuming session is already // started, final CAPTCHA string // is stored in session header('Content-type: image/jpeg'); // Set header imagejpeg($im); // produce jpeg image imagedestroy($im); // free-up temporary memory used to store image }
/** * @throws Exception * 生成验证码 , 存放到token相对应的session里面 */ public function createCaptcha() { $token = $_COOKIE["token"]; if (empty($token)) { throw new Exception("无法生产验证码,请稍后在试"); } $string = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < 4; $i++) { $pos = rand(0, 35); $str .= $string[$pos]; } $this->load->library('session'); $flag = $this->session->set_userdata($token . "_captcha", $str); $img_handle = Imagecreate(80, 20); //图片大小80X20 $back_color = ImageColorAllocate($img_handle, 255, 255, 255); //背景颜色(白色) $txt_color = ImageColorAllocate($img_handle, 0, 0, 0); //文本颜色(黑色) //加入干扰线 for ($i = 0; $i < 3; $i++) { $line = ImageColorAllocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imageline($img_handle, rand(0, 15), rand(0, 15), rand(100, 150), rand(10, 50), $line); } //加入干扰象素 for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imagesetpixel($img_handle, rand() % 100, rand() % 50, $randcolor); } Imagefill($img_handle, 0, 0, $back_color); //填充图片背景色 ImageString($img_handle, 28, 20, 0, $str, $txt_color); //水平填充一行字符串 ob_clean(); // ob_clean()清空输出缓存区 header("Content-type: image/png"); //生成验证码图片 Imagepng($img_handle); //显示图片 }
private function create_do() { $this->im = Imagecreate($this->config['width'], $this->config['height']); //设置背景为白色 Imagecolorallocate($this->im, $this->config['color'][0], $this->config['color'][1], $this->config['color'][2]); //为此背景加个边框 if ($this->config['boxline'] == True) { $bordercolor = Imagecolorallocate($this->im, 37, 37, 37); Imagerectangle($this->im, 0, 0, $this->config['width'] - 1, $this->config['height'] - 1, $bordercolor); } //生成验证码 $this->create_str(); $coder = $_SESSION[$this->config['codname']]; //输入文字 $fontcolor = Imagecolorallocate($this->im, 46, 46, 46); for ($i = 0; $i < $this->config['length']; $i++) { Imagestring($this->im, 5, $i * 10 + 6, rand(2, 5), $coder[$i], $fontcolor); } //加入干扰线 $interfere = $this->config['interfere']; $interfere = $interfere > 30 ? "30" : $interfere; if (!empty($interfere) && $interfere > 1) { for ($i = 1; $i < $interfere; $i++) { $linecolor = Imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255)); $x = rand(1, $this->config['width']); $y = rand(1, $this->config['height']); $x2 = rand($x - 10, $x + 10); $y2 = rand($y - 10, $y + 10); Imageline($this->im, $x, $y, $x2, $y2, $linecolor); } } ob_clean(); Header("Pragma:no-cache\r\n"); Header("Cache-Control:no-cache\r\n"); Header("Expires:0\r\n"); Header("Content-type:Image/jpeg\r\n"); Imagejpeg($this->im); Imagedestroy($this->im); exit; }
/** * Created by PhpStorm. * User: zhuangshaoxiong * Date: 2015/12/3 * Time: 20:19 */ $string = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < 4; $i++) { $pos = rand(0, 35); $str .= $string[$pos]; } session_start(); $_SESSION['checknode'] = $str; $img_handle = Imagecreate(100, 20); $back_color = ImageColorAllocate($img_handle, 255, 255, 255); $txt_color = ImageColorAllocate($img_handle, 0, 0, 0); echo ddddd; for ($i = 0; $i < 3; $i++) { $line = ImageColorAllocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imageline($img_handle, rand(0, 15), rand(0, 15), rand(100, 150), rand(10, 50), $line); } for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imagesetpixel($img_handle, rand() % 100, rand() % 50, $randcolor); } Imagefill($img_handle, 0, 0, $back_color); ImageString($img_handle, 28, 10, 0, $str, $txt_color); ob_clean(); header("Content-Type: image/png;");