《PHP編程:php生成圖片驗證碼》要點:
本文介紹了PHP編程:php生成圖片驗證碼,希望對您有用。如果有疑問,可以聯系我們。
PHP應用先給看下 大致的效果
PHP應用
PHP應用那么接下來的就直接貼代碼吧
PHP應用
<?php
$image = imagecreatetruecolor(100, 30); //創建畫布
$imagecolor = imagecolorallocate($image, 255, 255, 255); //配景色
imagefill($image, 0, 0, $imagecolor); //填充配景色
for($i=0;$i<4;$i++ ){ //循環4位數
$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++ ){ //循環 添加干擾點
$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++){ //循環 添加干擾線
$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);
?>
PHP應用再給大家分享一個可以生成中文驗證碼
PHP應用
<?php
//1.qi啟用gd庫GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者生成圖片.
// 在網站上GD庫通常用來生成縮略圖或者用來對圖片加水印或者對網站數據生成報表.
session_start();
// 把GBK編碼的字符串轉換成UTF-8字符串,第一個參數之所以寫GBK,是因為本php文件在主機中存儲的編碼是GBK編碼
// UTF-8編碼瀏覽器普遍支持,通用性強,這里就轉換成UTF-8
$str = iconv("GBK", "utf-8", "蕓蕓眾生綠水青山名勝古跡敞開心胸便會云蒸霞蔚快樂將永遠隨同著你");
if(!is_string($str) || !mb_check_encoding($str,"utf-8"))
{
exit("不是字符串或者不是utf-8");
}
$zhongwenku_size;
// 按UTF-8編碼方式獲取字符串的長度
$zhongwenku_size = mb_strlen($str,"UTF-8");
// 把上述字符導入數組中
$zhongwenku = array();
for( $i=0; $i<$zhongwenku_size; $i++)
{
$zhongwenku[$i] = mb_substr($str, $i,1,"UTF-8");
}
$result = "";
// 圖片上要寫入的四個字符
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;
// 創建一個真彩圖片 寬100,高30
$img = imagecreatetruecolor(100, 30);
// 分配背景顏色
$bg = imagecolorallocate($img, 0, 0, 0);
// 分配文字顏色
$te = imagecolorallocate($img, 255,255,255);
// 在圖片上寫字符串
//imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te);
// 在圖片上根據載入字體可以寫出特殊字體
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));
// 畫線
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));
// 為圖片添加噪點
for($i=0; $i<200; $i++)
{
imagesetpixel($img, rand(1, 100), rand(1, 30), $t);
}
// 發送http頭信息 指定本次發送的是image中的jpeg
header("Content-type: image/jpeg");
// 輸出jpeg圖片至瀏覽器
imagejpeg($img);
?>
PHP應用再來一個實例吧
PHP應用
<?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;
}
//隨機生成的字符串
$str = random(4);
//驗證碼圖片的寬度
$width = 50;
//驗證碼圖片的高度
$height = 25;
//聲明需要創建的圖層的圖片格式
@ header("Content-Type:image/png");
//創建一個圖層
$im = imagecreate($width, $height);
//配景色
$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
//模糊點顏色
$pix = imagecolorallocate($im, 187, 230, 247);
//字體色
$font = imagecolorallocate($im, 41, 163, 238);
//繪模糊作用的點
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;
?>
PHP應用接下來只要在頁面中調用就可以了:
PHP應用
<img id="checkpic" onclick="changing();" src='/images/checkcode.php' />
PHP應用如果想實現 "看不清?換一張" 效果,添加如下 JS 到頁面中
PHP應用
function changing(){
document.getElementById('checkpic').src="/images/checkcode.php?"+Math.random();
}
PHP應用以上所述便是本文的全部內容了,希望大家能夠喜歡.
歡迎參與《PHP編程:php生成圖片驗證碼》討論,分享您的想法,維易PHP學院為您提供專業教程。