function generateImage($token) { $iFont = 5; // Font ID $iSpacing = 2; // Spacing between characters $iDisplacement = 5; // Vertical chracter displacement // Establish font metric and image size $iCharWidth = ImageFontWidth($iFont); $iCharHeight = ImageFontHeight($iFont); $iWidth = strlen($token) * ($iCharWidth + $iSpacing); $iHeight = $iCharHeight + 2 * $iDisplacement; // Create the image $pic = ImageCreate($iWidth, $iHeight); // Allocate a background and foreground colour $col = array('white' => ImageColorAllocate($pic, 255, 255, 255), 'blue' => ImageColorAllocate($pic, 45, 45, 100), 'green' => ImageColorAllocate($pic, 45, 100, 45), 'red' => ImageColorAllocate($pic, 100, 45, 45), 'purple' => ImageColorAllocate($pic, 100, 45, 100), 'grey' => ImageColorAllocate($pic, 225, 225, 225), 'grey2' => ImageColorAllocate($pic, 200, 200, 200)); for ($x = 0; $x < $iWidth; $x += 2) { for ($y = 0; $y < $iHeight; $y += 2) { ImageSetPixel($pic, $x, $y, $col['grey']); } } $iX = 1; for ($i = 0; $i < strlen($token); $i++) { ImageChar($pic, $iFont - 1, $iX, $iDisplacement - rand(-$iDisplacement, $iDisplacement), $token[$i], $col['grey2']); $iX += $iCharWidth + $iSpacing; } $iX = 2; $c = array('blue', 'green', 'red', 'purple'); for ($i = 0; $i < strlen($token); $i++) { $colour = $c[rand(0, count($c) - 1)]; ImageChar($pic, $iFont, $iX, $iDisplacement - rand(-$iDisplacement, $iDisplacement), $token[$i], $col[$colour]); $iX += $iCharWidth + $iSpacing; } for ($x = 1; $x < $iWidth; $x += 4) { for ($y = 1; $y < $iHeight; $y += 4) { ImageSetPixel($pic, $x, $y, $col['white']); } } // Draw some lines for ($i = 0; $i < 4; $i++) { ImageLine($pic, rand(0, $iWidth / 2), rand(0, $iHeight / 2), rand($iWidth / 2, $iWidth), rand($iHeight / 2, $iHeight), $col['white']); } ob_start(); if (function_exists('imagejpeg')) { ImageJPEG($pic); } elseif (function_exists('imagepng')) { ImagePNG($pic); } else { ob_end_clean(); return false; } $data = ob_get_contents(); ob_end_clean(); ImageDestroy($pic); return $data; }
/** * $IMG_WIDTH = 100; //图像宽度 $IMG_HEIGHT = 23; //图像高度 $SESSION_VDCODE = ''; //Session变量名称 * $operator = '+-'; //运算符 */ public static function display($IMG_WIDTH = 70, $IMG_HEIGHT = 25, $SESSION_VDCODE = 'vdcode', $operator = '+') { $session_id = session_id(); if (empty($session_id)) { @session_start(); } $code = array(); $num1 = $code[] = mt_rand(1, 9); $code[] = '+'; //{mt_rand(0,1)}; $num2 = $code[] = mt_rand(1, 9); $codestr = implode('', $code); //eval("\$result = ".implode('',$code).";"); $code[] = '='; $_SESSION[$SESSION_VDCODE] = (int) $num1 + (int) $num2; $img = ImageCreate($IMG_WIDTH, $IMG_HEIGHT); ImageColorAllocate($img, mt_rand(230, 250), mt_rand(230, 250), mt_rand(230, 250)); $color = ImageColorAllocate($img, 0, 0, 0); $offset = 0; foreach ($code as $char) { $offset += 15; $txtcolor = ImageColorAllocate($img, mt_rand(0, 255), mt_rand(0, 150), mt_rand(0, 255)); ImageChar($img, mt_rand(3, 5), $offset, mt_rand(1, 5), $char, $txtcolor); } for ($i = 0; $i < 100; $i++) { $pxcolor = ImageColorAllocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); ImageSetPixel($img, mt_rand(0, $IMG_WIDTH), mt_rand(0, $IMG_HEIGHT), $pxcolor); } header('Content-type: image/png'); ImagePng($img); exit; }
public static function make() { $sessionvar = 'vdcode'; //Session变量名称 $width = 150; //图像宽度 $height = 20; //图像高度 $ans = mt_rand(0, 9); $p1 = mt_rand(1, 9); if ($p1 <= $ans) { $p2 = $ans - $p1; $operator = '+'; } else { $p2 = $p1 - $ans; $operator = '-'; } $code = array(); $code[] = $p1; $code[] = $operator; $code[] = $p2; $code[] = '='; $code[] = $ans; $_SESSION[$sessionvar] = $ans; $img = ImageCreate($width, $height); ImageColorAllocate($img, mt_rand(230, 250), mt_rand(230, 250), mt_rand(230, 250)); $color = ImageColorAllocate($img, 0, 0, 0); $offset = 0; foreach ($code as $char) { $offset += 20; $txtcolor = ImageColorAllocate($img, mt_rand(0, 255), mt_rand(0, 150), mt_rand(0, 255)); ImageChar($img, mt_rand(3, 5), $offset, mt_rand(3, 5), $char, $txtcolor); } for ($i = 0; $i < 100; $i++) { $pxcolor = ImageColorAllocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); ImageSetPixel($img, mt_rand(0, $width), mt_rand(0, $height), $pxcolor); } return ImagePng($img); }
public function getCaptcha() { $this->image = ImageCreate($this->width, $this->height); $bgcolor = ImageColorAllocate($this->image, $this->bgcolor[0], $this->bgcolor[1], $this->bgcolor[2]); // 背景色 ImageFill($this->image, 0, 0, $bgcolor); // 填充背景色 // 檢查是否要邊框 if ($this->border) { $this->makeBorder(); } // 開始生成 for ($i = 0; $i < $this->quantity; $i++) { $alpha_or_number = mt_rand(0, 1); // 隨機取英文或數字 $str = $alpha_or_number ? $this->alpha : $this->number; $which = mt_rand(0, strlen($str) - 1); // 取英文或數字位置 $code = substr($str, $which, 1); // 取英文或數字 $j = !$i ? 4 : $j + 15; // 字串位置 $color3 = ImageColorAllocate($this->image, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]); // 字串顏色 ImageChar($this->image, $this->fontsize, $j, 8, $code, $color3); // 依序寫入字串 $this->captcha .= $code; // 生成字串 } // 檢查是否要進行圖片干擾 if ($this->line) { $this->mixedLine(); } $this->CI->load->library('session'); $this->CI->session->set_userdata('captcha', $this->captcha); return $this->image; // 輸出 }
srand((double) microtime() * 1000000); $im = ImageCreate($w, $h); $bgcolor = ImageColorAllocate($im, 255, 255, 255); ImageFill($im, 0, 0, $bgcolor); if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); } for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); $code = substr($str, $which, 1); $j = !$i ? 4 : $j + 15; $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); ImageChar($im, $fontsize, $j, 3, $code, $color3); $randcode .= $code; } for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); } for ($i = 0; $i < $how * 40; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); } $_SESSION['randcode'] = $randcode; Imagegif($im); ImageDestroy($im); ?>
function show() { Header("Content-type: image/gif"); /* * ��ʼ�� */ $border = 0; //�Ƿ�Ҫ�߿� 1Ҫ:0��Ҫ $how = $this->num; //��֤��λ�� $w = $this->width; //ͼƬ��� $h = $this->height; //ͼƬ�߶� $fontsize = 5; //�����С $alpha = "abcdefghijkmnopqrstuvwxyz"; //��֤������1:��ĸ $number = "023456789"; //��֤������2:���� $randcode = ""; //��֤���ַ�����ʼ�� srand((double) microtime() * 1000000); //��ʼ����������� $im = ImageCreate($w, $h); //������֤ͼƬ /* * ���ƻ������ */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //���ñ�����ɫ ImageFill($im, 0, 0, $bgcolor); //��䱳��ɫ if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //���ñ߿���ɫ ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //���Ʊ߿� } /* * ��λ��������ַ� */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //��ĸ�������� $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //ȡ�ĸ��ַ� $code = substr($str, $which, 1); //ȡ�ַ� $j = !$i ? 4 : $j + 15; //���ַ�λ�� $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //�ַ��漴��ɫ ImageChar($im, $fontsize, $j, 3, $code, $color3); //���ַ� $randcode .= $code; //��λ������֤���ַ��� } /* * ��Ӹ��� */ for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //��������ɫ ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //������ } for ($i = 0; $i < $how * 15; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //���ŵ���ɫ ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //���ŵ� } //����֤���ַ���д��session //$this->session->set_userdata(array($this->name=>$randcode)); $_SESSION[$this->name] = $randcode; /*��ͼ����*/ Imagegif($im); ImageDestroy($im); /*��ͼ����*/ }
<?php header("Content-type: image/png"); $img = ImageCreate(18, 25); ImageColorAllocate($img, 0, 0, 0); //��˹�����ٻ���մ� $orange = ImageColorAllocate($img, 255, 128, 0); /* ����� $num ��͵����Ẻ�ź�ŷ��١�ŧ�Ҩҡ query string ������ counter.php ������� */ ImageChar($img, 5, 5, 5, $num, $orange); ImagePNG($img); ImageDestroy($img);
function create_my_captcha() { Header("Content-type: image/gif"); /* * 初始化 */ $border = 0; //是否要边框 1要:0不要 $how = 4; //验证码位数 $w = $how * 15; //图片宽度 $h = 20; //图片高度 $fontsize = 16; //字体大小 $alpha = "abcdefghijkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ"; //验证码内容1:字母 $number = "23456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 添加干扰 */ for ($i = 0; $i < 2; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $how * 7; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } //把验证码字符串写入session $_SESSION['login_check_number'] = strtoupper($randcode); /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ }
if ($chart_values[$i] == $chart_values[$i + 1]) { $no_of_users[$chart_values[$i]] = $x++; } else { $no_of_users[$chart_values[$i]] = $x; $x = 1; } } //$no_of_users = array ( "0"=>"2","20"=>"3","66" => "2" ,"70" => "3", "100" => "1") ; $users_arr_keys = @array_keys($no_of_users); //PRINTING THE VALUES ON THE CHART for ($i = 0; $i < count($users_arr_keys); $i++) { $key = $users_arr_keys[$i]; $val = $no_of_users[$key]; $x1 = $key * 4 + 15; $y1 = 180 - $distance_inbetween * $val; $key1 = $users_arr_keys[$i + 1]; $val1 = $no_of_users[$key1]; $x2 = $key1 * 4 + 15; $y2 = 180 - $distance_inbetween * $val1; // imagearc ( $image, $x1, $y1, $x2, $y2, $x1, $x2, $border); //echo "$y1<br>"; if ($key1 != NULL) { ImageLine($image, $x1, $y1, $x2, $y2, $border); } ImageChar($image, $labelfont, $x1, $y1 - 5, ".", $border); } header("Content-type: image/png"); // or "Content-type: image/png" ImageJPEG($image); // or imagepng($image) ImageDestroy($image);
public function show($callback = NULL) { Header('Content-type: image/gif'); /* * 初始化 */ $border = 0; //是否要边框 1要:0不要 $how = $this->num; //验证码位数 $w = $this->width; //图片宽度 $h = $this->height; //图片高度 $fontsize = 5; //字体大小 $alpha = 'abcdefghijkmnopqrstuvwxyz'; //验证码内容1:字母 $number = '023456789'; //验证码内容2:数字 $randcode = ''; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 添加干扰 */ for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $how * 15; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } //把验证码字符串写入session //$_SESSION[$this->name]=$randcode; // if (isset($this->output_conf)) { // if (is_array($this->output_conf)) { // $this->output_conf['verify_code'] = $randcode; // } else { // $this->output_conf->verify_code = $randcode; // } // } // $this->CI->session->set_userdata('verify_code', $randcode); if (isset($callback)) { $callback($randcode); } /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ return $randcode; }
function show() { Header("Content-type: image/gif"); /* * 初始化 */ $border = 0; //是否要边框 1要:0不要 $how = $this->num; //验证码位数 $w = $this->width; //图片宽度 $h = $this->height; //图片高度 $fontsize = 5; //字体大小 $alpha = "abcdefghijkmnpqrstuvwxyz"; //验证码内容1:字母 $number = "23456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 添加干扰 * 目的是为了不让机器轻易破解 */ for ($i = 0; $i < 5; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $how * 15; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } //把验证码字符串写入session //$this->session->set_userdata(array($this->name=>$randcode)); //???????????????/ $_SESSION[$this->name] = $randcode; /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ }
public function getCode() { Header("Content-type: image/png"); /* * 初始化 */ $border = 1; //是否要边框 1要:0不要 $how = 4; //验证码位数 $w = $how * 15; //图片宽度 $h = 20; //图片高度 $fontsize = 6; //字体大小 $alpha = "abcdefghijkmnopqrstuvwxyz"; //验证码内容1:字母 $number = "023456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } //把验证码字符串写入session session_start(); $_SESSION['authnum_session'] = $randcode; /* * 添加干扰 */ for ($i = 0; $i < 1; $i++) { $color1 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 ImageArc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } for ($i = 0; $i < $how * 40; $i++) { $color2 = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } //关键代码,防止出现'图像因其本身有错无法显示'的问题 ob_clean(); /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ }