欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php生成圖片驗(yàn)證碼

 更新時(shí)間:2015年06月09日 08:55:25   投稿:hebedich  
驗(yàn)證碼在WEB應(yīng)用中非常重要,通常用來(lái)防止用戶惡意提交表單,如惡意注冊(cè)和登錄、論壇惡意灌水等。本文將通過(guò)實(shí)例講解使用PHP生成常見(jiàn)的驗(yàn)證碼

先給看下 大致的效果

那么接下來(lái)的就直接貼代碼吧

<?php
 $image = imagecreatetruecolor(100, 30); //創(chuàng)建畫(huà)布
 $imagecolor = imagecolorallocate($image, 255, 255, 255); //背景色
 imagefill($image, 0, 0, $imagecolor); //填充背景色
 for($i=0;$i<4;$i++ ){     //循環(huán)4位數(shù)
  $fontsize = 6;
  $fontcolor = imagecolorallocate($image, rand(0, 200), rand(0, 200), rand(0, 200));
  $fontcontent = rand(0, 9);
  $x = $i*100/4 + rand(5, 15);
  $y = rand(5, 10);
  imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
 }
 for($i=0;$i<200;$i++ ){    //循環(huán) 添加干擾點(diǎn)
  $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
  $x = rand(1, 99);
  $y = rand(1, 29);
  imagesetpixel($image, $x, $y, $pointcolor);
 }
 for($i=0;$i<3;$i++){    //循環(huán) 添加干擾線 
  $linecolor = imagecolorallocate($image, rand(100, 250), rand(100, 250), rand(100, 250));
  $x1 = rand(1, 25);
  $x2 = rand(50, 75);
  $y1 = rand(1, 15);
  $y2 = rand(15, 25);
  imageline($image, $x1, $y1, $x2, $y2, $linecolor);
 }
 header("content-type:image/png");
 imagepng($image);
 imagedestroy($image);
?>

再給大家分享一個(gè)可以生成中文驗(yàn)證碼

<?php
//1.qi啟用gd庫(kù)GD庫(kù)提供了一系列用來(lái)處理圖片的API,使用GD庫(kù)可以處理圖片,或者生成圖片。 
// 在網(wǎng)站上GD庫(kù)通常用來(lái)生成縮略圖或者用來(lái)對(duì)圖片加水印或者對(duì)網(wǎng)站數(shù)據(jù)生成報(bào)表。
session_start();

// 把GBK編碼的字符串轉(zhuǎn)換成UTF-8字符串,第一個(gè)參數(shù)之所以寫(xiě)GBK,是因?yàn)楸緋hp文件在主機(jī)中存儲(chǔ)的編碼是GBK編碼
// UTF-8編碼瀏覽器普遍支持,通用性強(qiáng),這里就轉(zhuǎn)換成UTF-8
$str = iconv("GBK", "utf-8", "蕓蕓眾生綠水青山名勝古跡敞開(kāi)心胸便會(huì)云蒸霞蔚快樂(lè)將永遠(yuǎn)伴隨著你");
if(!is_string($str) || !mb_check_encoding($str,"utf-8"))
{
	exit("不是字符串或者不是utf-8");
}
$zhongwenku_size;
// 按UTF-8編碼方式獲取字符串的長(zhǎng)度
$zhongwenku_size = mb_strlen($str,"UTF-8");

// 把上述字符導(dǎo)入數(shù)組中
$zhongwenku = array();
for( $i=0; $i<$zhongwenku_size; $i++)
{
	$zhongwenku[$i] = mb_substr($str, $i,1,"UTF-8");
}

$result = "";

// 圖片上要寫(xiě)入的四個(gè)字符
for($i=0; $i<4; $i++)
{
	switch (rand(0, 1))
	{
		case 0:
			$result.=$zhongwenku[rand(0, $zhongwenku_size-1)];
			break;
		case 1:
			$result.=dechex(rand(0,15));
			break;
	}
	
}

$_SESSION["check"] = $result;
	
// 創(chuàng)建一個(gè)真彩圖片 寬100,高30
$img = imagecreatetruecolor(100, 30);

// 分配背景顏色
$bg = imagecolorallocate($img, 0, 0, 0);

// 分配文字顏色
$te = imagecolorallocate($img, 255,255,255);

// 在圖片上寫(xiě)字符串
//imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te);

// 在圖片上根據(jù)載入字體可以寫(xiě)出特殊字體
imagettftext($img, 13, rand(2, 9), 20 ,20, $te, "MSYH.TTF",$result);

$_SESSION["check"] = $result;

for($i=0; $i<3; $i++)
{
//	$t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255));
	// 畫(huà)線
	imageline($img, 0, rand(0, 20), rand(70,100), rand(0, 20), $te);	
}

$t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255));
// 為圖片添加噪點(diǎn)
for($i=0; $i<200; $i++)
{
	imagesetpixel($img, rand(1, 100), rand(1, 30), $t);
}
// 發(fā)送http頭信息 指定本次發(fā)送的是image中的jpeg
header("Content-type: image/jpeg");
// 輸出jpeg圖片至瀏覽器
imagejpeg($img);

?>

再來(lái)一個(gè)實(shí)例吧

<?php
 
session_start();
function random($len) {
  $srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";
  mt_srand();
  $strs = "";
  for ($i = 0; $i < $len; $i++) {
    $strs .= $srcstr[mt_rand(0, 30)];
  }
  return $strs;
}
 
//隨機(jī)生成的字符串
$str = random(4); 
 
//驗(yàn)證碼圖片的寬度
$width = 50;   
 
//驗(yàn)證碼圖片的高度
$height = 25;   
 
//聲明需要?jiǎng)?chuàng)建的圖層的圖片格式
@ header("Content-Type:image/png");
 
//創(chuàng)建一個(gè)圖層
$im = imagecreate($width, $height);
 
//背景色
$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
 
//模糊點(diǎn)顏色
$pix = imagecolorallocate($im, 187, 230, 247);
 
//字體色
$font = imagecolorallocate($im, 41, 163, 238);
 
//繪模糊作用的點(diǎn)
mt_srand();
for ($i = 0; $i < 1000; $i++) {
  imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
}
 
//輸出字符
imagestring($im, 5, 7, 5, $str, $font);
 
//輸出矩形
imagerectangle($im, 0, 0, $width -1, $height -1, $font);
 
//輸出圖片
imagepng($im);
 
imagedestroy($im);
 
$str = md5($str);
 
//選擇 cookie
//SetCookie("verification", $str, time() + 7200, "/");
 
//選擇 Session
$_SESSION["verification"] = $str;
?>

接下來(lái)只要在頁(yè)面中調(diào)用就可以了:

<img id="checkpic" onclick="changing();" src='/images/checkcode.php' />

如果想實(shí)現(xiàn) "看不清?換一張" 效果,添加如下 JS 到頁(yè)面中

function changing(){
  document.getElementById('checkpic').src="/images/checkcode.php?"+Math.random();
} 

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論