php生成帶有雪花背景的驗證碼,php生成帶有雪花背景的驗證碼是什麼

本文目錄一覽:

如何用PHP生成驗證碼

PHP生成驗證碼的原理:使用PHP的GD庫,生成一張帶驗證碼的圖片,並將驗證碼保存在Session中。PHP生成驗證碼的大致流程有:

1、產生一張png的圖片;

2、為圖片設置背景色;

3、設置字體顏色和樣式;

4、產生4位數的隨機的驗證碼;

5、把產生的每個字符調整旋轉角度和位置畫到png圖片上;

6、加入噪點和干擾線防止註冊機器分析原圖片來惡意破解驗證碼;

7、輸出圖片;

8、釋放圖片所佔內存。

session_start(); 

getCode(4,60,20); 

 

function getCode($num,$w,$h) { 

    $code = “”; 

    for ($i = 0; $i  $num; $i++) { 

        $code .= rand(0, 9); 

    } 

    //4位驗證碼也可以用rand(1000,9999)直接生成 

    //將生成的驗證碼寫入session,備驗證時用 

    $_SESSION[“helloweba_num”] = $code; 

    //創建圖片,定義顏色值 

    header(“Content-type: image/PNG”); 

    $im = imagecreate($w, $h); 

    $black = imagecolorallocate($im, 0, 0, 0); 

    $gray = imagecolorallocate($im, 200, 200, 200); 

    $bgcolor = imagecolorallocate($im, 255, 255, 255); 

    //填充背景 

    imagefill($im, 0, 0, $gray); 

 

    //畫邊框 

    imagerectangle($im, 0, 0, $w-1, $h-1, $black); 

 

    //隨機繪製兩條虛線,起干擾作用 

    $style = array ($black,$black,$black,$black,$black, 

        $gray,$gray,$gray,$gray,$gray 

    ); 

    imagesetstyle($im, $style); 

    $y1 = rand(0, $h); 

    $y2 = rand(0, $h); 

    $y3 = rand(0, $h); 

    $y4 = rand(0, $h); 

    imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED); 

    imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED); 

 

    //在畫布上隨機生成大量黑點,起干擾作用; 

    for ($i = 0; $i  80; $i++) { 

        imagesetpixel($im, rand(0, $w), rand(0, $h), $black); 

    } 

    //將數字隨機顯示在畫布上,字符的水平間距和位置都按一定波動範圍隨機生成 

    $strx = rand(3, 8); 

    for ($i = 0; $i  $num; $i++) { 

        $strpos = rand(1, 6); 

        imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black); 

        $strx += rand(8, 12); 

    } 

    imagepng($im);//輸出圖片 

    imagedestroy($im);//釋放圖片所佔內存 

}

php 怎麼獲取驗證碼的值

$_SESSION[“vcode”] = $vcode;

驗證碼的值就在 $_SESSION[“vcode”] 裡面。

PHP製作驗證碼時候只顯示一個白色框框

?php 

ob_clean();

//header(“Content-type:text/html”);

header(“Content-type:image/jpeg”); //以jpeg格式輸出,注意上面不能輸出任何字符,否則出錯 

$w = 100; //設置圖片寬和高 

$h = 50; 

$fontsize=16;//設置字體大小

$str = Array(); //用來存儲隨機碼 

$string = “ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”;//隨機挑選其中4個字符,也可以選擇更多,注意循環的時候加上,寬度適當調整 

$vcode=”;

for($i = 0;$i  4;$i++){ 

$str[$i] = $string[rand(0,strlen($string)-1)]; 

$vcode .= $str[$i]; 

//echo $vcode;

session_start(); //啟用超全局變量session 

//session_register(“vcode”);

$_SESSION[“vcode”] = $vcode; 

$im = imagecreatetruecolor($w,$h); //創建圖片畫布

$white = imagecolorallocate($im,255,255,255); //第一次調用設置背景色 

$black = imagecolorallocate($im,0,0,0); //邊框顏色 

imagefilledrectangle($im,0,0,$w,$h,$white); //畫一矩形填充 

imagerectangle($im,0,0,$w-1,$h-1,$black); //畫一矩形框

$fontsrc=”consola.ttf” ;

//生成雪花背景 

for($i = 1;$i  200;$i++){ 

$x = mt_rand(1,$w-9); 

$y = mt_rand(1,$h-9); 

$color = imagecolorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); 

imagechar($im,1,$x,$y,”*”,$color); 

//將驗證碼寫入圖案 

for($i = 0;$i  count($str);$i++){ 

$x = 13 + $i * ($w – 15)/4; 

$y = mt_rand(25,$h-15 ); 

$color = imagecolorallocate($im,mt_rand(0,225),mt_rand(0,150),mt_rand(0,225)); 

//imagechar($im,$fontsize,$x,$y,$str[$i],$color); 

imagettftext($im,$fontsize,0,$x,$y,$color,$fontsrc,$str[$i]);

imagejpeg($im); //輸出圖片文件

imagedestroy($im); //銷毀釋放資源

?

怎麼用php生成圖像,生成驗證碼

?php

//驗證碼類

class ValidateCode {

 private $charset = ‘abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789’;//隨機因子

 private $code;//驗證碼

 private $codelen = 4;//驗證碼長度

 private $width = 90;//寬度

 private $height = 40;//高度

 private $img;//圖形資源句柄

 private $font;//指定的字體

 private $fontsize = 20;//指定字體大小

 private $fontcolor;//指定字體顏色

 //構造方法初始化

 public function __construct() {

  $this-font = dirname(__FILE__).’/font/elephant.ttf’;//注意字體路徑要寫對,否則顯示不了圖片

 }

 //生成隨機碼

 private function createCode() {

  $_len = strlen($this-charset)-1;

  for ($i=0;$i$this-codelen;$i++) {

   $this-code .= $this-charset[mt_rand(0,$_len)];

  }

 }

 //生成背景

 private function createBg() {

  $this-img = imagecreatetruecolor($this-width, $this-height);

  $color = imagecolorallocate($this-img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));

  imagefilledrectangle($this-img,0,$this-height,$this-width,0,$color);

 }

 //生成文字

 private function createFont() {

  $_x = $this-width / $this-codelen;

  for ($i=0;$i$this-codelen;$i++) {

   $this-fontcolor = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

   imagettftext($this-img,$this-fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this-height / 1.4,$this-fontcolor,$this-font,$this-code[$i]);

  }

 }

 //生成線條、雪花

 private function createLine() {

  //線條

  for ($i=0;$i6;$i++) {

   $color = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

   imageline($this-img,mt_rand(0,$this-width),mt_rand(0,$this-height),mt_rand(0,$this-width),mt_rand(0,$this-height),$color);

  }

  //雪花

  for ($i=0;$i100;$i++) {

   $color = imagecolorallocate($this-img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));

   imagestring($this-img,mt_rand(1,5),mt_rand(0,$this-width),mt_rand(0,$this-height),’*’,$color);

  }

 }

 //輸出

 private function outPut() {

  header(‘Content-type:image/png’);

  imagepng($this-img);

  imagedestroy($this-img);

 }

 //對外生成

 public function doimg() {

  $this-createBg();

  $this-createCode();

  $this-createLine();

  $this-createFont();

  $this-outPut();

 }

 //獲取驗證碼

 public function getCode() {

  return strtolower($this-code);

 }

}

求一個php驗證碼及如何使用的例子?

以前項目中的東西

在沒有GB庫的情況下也可以生成驗證碼

關鍵部分做出了注釋

img src=”image.php”

?php

//存為文件 image.php

error_reporting(E_ALL ^ E_NOTICE);

require_once(‘image.class.php’);

$image_model = new Image();

//可以在此處更改屬性達到驗證碼不同大小的圖片

$image_model-outputBrowser();

?

?php

/*

* \驗證碼生成類 存為文件image.class.php

* \如果不支持GD庫,則採用像素畫法

* \詳見

*/

class Image

{

var $img_height = 20; //圖片的長

var $img_width = 68; //圖片的寬

var $img_cache = ”; //圖像信息緩存

var $check_number = ”; //驗證碼

var $lenght = 4; //驗證碼位數

var $is_gd = false;

//’———-以下為不支持GD需要的變量

var $pixels = ”;

var $digits = ”;

var $lines = ”;

var $data = ”;

var $chunk = ”;

var $crc_table = ”;

function Image()

{

session_start();

session_register(“check_number”);

$this-session = $_SESSION;

$this-is_gd = function_exists(‘gd_info’) ? true : false;

$this-getCheckNumber();

if( $this-is_gd ):

$this-getFromGD();

else:

$this-getFromCode();

endif;

}

//’—-瀏覽器輸出

function outputBrowser()

{

if( $this-is_gd )

{

Header(“Content-type: image/png”); //告訴瀏覽器,下面的數據是圖片,而不要按文字顯示

Imagepng($this-img_cache); //生成png格式。。。嘿嘿效果蠻像回事的嘛。。。

ImageDestroy($this-img_cache);

}

else echo($this-img_cache);

}

//’—-文件輸出

function outputFile($filename=null)

{

if(!$filename)$filename=time().’.png’;

$fp = fopen($filename, ‘w+’);

fwrite($fp, $this-img_cache);

fclose($fp);

}

//’—-得到驗證碼

function getCheckNumber()

{

//srand(microtime() * 100000);//PHP420後,srand不是必須的

for($Tmpa=0;$Tmpa$this-lenght;$Tmpa++)

{

$this-check_number.= $this-is_gd ? dechex(rand(0,15)) : rand(0,3);

}

unset($Tmpa);

$this-session[‘check_number’] = strtoupper($this-check_number);

}

//’—-用GD庫生成

function getFromGD()

{

//$black = ”;

$this-img_cache = imageCreate($this-img_width,$this-img_height); //生成圖片

//圖片底色,ImageColorAllocate第1次定義顏色PHP就認為是底色了

$black = ImageColorAllocate($this-img_cache, 255,255,255);

//下面該生成雪花背景了,其實就是在圖片上生成一些符號

//先用100個做測試

/*

for ($i=1; $i=15; $i++)

{

imageString($this-img_cache,1,mt_rand(1,$this-img_height-10),mt_rand(1,$this-img_width-10),”.”,

imageColorAllocate($this-img_cache,mt_rand(100,255),mt_rand(0,250),mt_rand(0,200)));

//哈,看到了吧,其實也不是雪花,就是生成*號而已。為了使它們看起來”雜亂無章、5顏6色”,就得在1個1個生成它們的時候,讓它們的位置、顏色,甚至大小都用隨機數,rand()或mt_rand都可以完成。

}

*/

//上面生成了背景,現在就該把已經生成的隨機數放上來了。道理和上面差不多,隨機數1個1個地放,同時讓他們的位置、大小、顏色都用成隨機數~~

//為了區別於背景,這裡的顏色不超過200,上面的不小於200

imagettftext($this-img_cache, 20, 0, 10, 20, $black, “./html/VINERITC.TTF”, “AC67”);

for ($i=0;$istrlen($this-session[‘check_number’]);$i++)

{

imagettftext($this-img_cache, 14, rand(0,30), $i*$this-img_width/4 + 3, 17, imageColorAllocate($this-img_cache,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)), “./html/VINERITC.TTF”, $this-session[‘check_number’][$i]);

}

for($i=0;$i100;$i++)//加入干擾象素

{

imagesetpixel($this-img_cache, rand()%70 , rand()%30 , imageColorAllocate($this-img_cache,mt_rand(100,255),mt_rand(0,250),mt_rand(0,200)));

}

$black = ImageColorAllocate($this-img_cache, 0,0,0); //定義需要的黑色

//先成一黑色的矩形把圖片包圍

ImageRectangle($this-img_cache,0,0, $this-img_width-1,$this-img_height-1, $black);

}

//End Function getGD

#———————————————————————————————————————–

function set_4pixel($r, $g, $b, $x, $y)

{

$ofs = 3 * ($this-img_width * $y + $x);

$this-pixels[$ofs] = chr($r);

$this-pixels[$ofs + 1] = chr($g);

$this-pixels[$ofs + 2] = chr($b);

$this-pixels[$ofs + 3] = chr($r);

$this-pixels[$ofs + 4] = chr($g);

$this-pixels[$ofs + 5] = chr($b);

$ofs += 3 * $this-img_width;

$this-pixels[$ofs] = chr($r);

$this-pixels[$ofs + 1] = chr($g);

$this-pixels[$ofs + 2] = chr($b);

$this-pixels[$ofs + 3] = chr($r);

$this-pixels[$ofs + 4] = chr($g);

$this-pixels[$ofs + 5] = chr($b);

}

//生成數字圖象的函數

function draw2digits($x, $y, $number)

{

//$this-draw_digit($x, $y, intval($number / 10) );

$this-draw_digit($x + 11, $y, $number % 10);

}

function draw_digit($x, $y, $digit)

{

$digit = $this-digits[$digit];

$m = 8;

for($b = 1, $i = 0; $i 7; $i++, $b *= 2)

{

if(($b $digit) == $b)

{

$j = $i * 4;

$x0 = $this-lines[$j] * $m + $x;

$y0 = $this-lines[$j + 1] * $m + $y;

$x1 = $this-lines[$j + 2] * $m + $x;

$y1 = $this-lines[$j + 3] * $m + $y;

if($x0 == $x1)

{

$ofs = 3 * ($this-img_width * $y0 + $x0);

for($h = $y0; $h = $y1; $h++, $ofs += 3 * $this-img_width)

{

$this-pixels[$ofs] = chr(0);

$this-pixels[$ofs + 1] = chr(0);

$this-pixels[$ofs + 2] = chr(0);

}

}

else

{

$ofs = 3 * ($this-img_width * $y0 + $x0);

for($w = $x0; $w = $x1; $w++)

{

$this-pixels[$ofs++] = chr(0);

$this-pixels[$ofs++] = chr(0);

$this-pixels[$ofs++] = chr(0);

}

}

}//End if

}//End for

}

//將文字加入到圖象中

function add_chunk($type)

{

// chunk :為層

// length: 4 字節: 用來計算 chunk

// chunk type: 4 字節

// chunk data: length bytes

// CRC: 4 字節: 循環冗餘碼校驗

// copy data and create CRC checksum

$len = strlen($this-data);

$this-chunk = pack(“c*”, ($len 24) 255,

($len 16) 255,

($len 8) 255,

$len 255);

$this-chunk .= $type;

$this-chunk .= $this-data;

//calculate a CRC checksum with the bytes chunk[4..len-1]

$z = 16777215;

$z |= 255 24;

$c = $z;

for ($n = 4; $n strlen($this-chunk); $n++) {

$c8 = ($c 8) 0xffffff;

$c = $this-crc_table[($c ^ ord($this-chunk[$n])) 0xff] ^ $c8;

}

$crc = $c ^ $z;

$this-chunk .= chr(($crc 24) 255);

$this-chunk .= chr(($crc 16) 255);

$this-chunk .= chr(($crc 8) 255);

$this-chunk .= chr($crc 255);

//將結果加到$img_cache中

$this-img_cache .= $this-chunk;

}

//主程序

function getFromCode()

{

//填充

for($h = 0; $h $this-img_height; $h++)

{

for($w = 0; $w $this-img_width; $w++)

{

$r = 100 / $this-img_width * $w + 155;

$g = 100 / $this-img_height * $h + 155;

$b = 255 – (100 / ($this-img_width + $this-img_height) * ($w + $h));

$this-pixels .= chr($r);

$this-pixels .= chr($g);

$this-pixels .= chr($b);

}

}

$this-digits = array(95, 5, 118, 117, 45, 121, 123, 69, 127, 125);

$this-lines = array(1, 1, 1, 2, 0, 1, 0, 2, 1, 0, 1, 1, 0, 0, 0, 1, 0, 2, 1, 2, 0, 1, 1, 1, 0, 0, 1, 0);

for($i=0; $i$this-lenght; $i++)

{

$this-draw2digits($i*13, 2, $this-check_number[$i]);

}

//$this-set_4pixel(0, 0, 0, 26, 7);

//$this-set_4pixel(0, 0, 0, 26, 13);

//$this-set_4pixel(0, 0, 0, 52, 7);

//$this-set_4pixel(0, 0, 0, 52, 13);

//創建循環冗餘碼校驗表

$z = -306674912;// = 0xedb88320

for ($n = 0; $n 256; $n++)

{

$c = $n;

for ($k = 0; $k 8; $k++)

{

$c2 = ($c 1) 0x7fffffff;

if($c 1) $c = $z ^ ($c2);

else $c = $c2;

}

$this-crc_table[$n] = $c;

}

// PNG file signature

$this-img_cache = pack(“c*”, 137,80,78,71,13,10,26,10);

// IHDR chunk data:

// width: 4 bytes

// height: 4 bytes

// bit depth: 1 byte (8 bits per RGB value)

// color type: 1 byte (2 = RGB)

// compression method: 1 byte (0 = deflate/inflate)

// filter method: 1 byte (0 = adaptive filtering)

// interlace method: 1 byte (0 = no interlace)

$this-data = pack(“c*”, ($this-img_width24) 255,

($this-img_width 16) 255,

($this-img_width 8) 255,

$this-img_width 255,

($this-img_height 24) 255,

($this-img_height 16) 255,

($this-img_height 8) 255,

$this-img_height 255, 8, 2, 0, 0, 0);

$this-add_chunk(“IHDR”);

//以下不敢亂翻譯,請自行參考

// scanline:

// filter byte: 0 = none

// RGB bytes for the line

// the scanline is compressed with “zlib”, method 8 (RFC-1950):

// compression method/flags code: 1 byte ($78 = method 8, 32k window)

// additional flags/check bits: 1 byte ($01: FCHECK = 1, FDICT = 0, FLEVEL = 0)

// compressed data blocks: n bytes

// one block (RFC-1951):

// bit 0: BFINAL: 1 for the last block

// bit 1 and 2: BTYPE: 0 for no compression

// next 2 bytes: LEN (LSB first)

// next 2 bytes: one’s complement of LEN

// LEN bytes uncompressed data

// check value: 4 bytes (Adler-32 checksum of the uncompressed data)

$len = ($this-img_width * 3 + 1) * $this-img_height;

$this-data = pack(“c*”, 0x78, 0x01, 1, $len 255, ($len8) 255, 255 – ($len 255), 255 – (($len 8) 255) );

$start = strlen($this-data);

$i2 = 0;

for($h = 0; $h $this-img_height; $h++)

{

$this-data .= chr(0);

for($w = 0; $w $this-img_width * 3; $w++)

{

$this-data .= $this-pixels[$i2++];

}

}

//calculate a Adler32 checksum with the bytes data[start..len-1]

$s1 = 1;

$s2 = 0;

for ($n = $start; $n strlen($this-data); $n++)

{

$s1 = ($s1 + ord($this-data[$n])) % 65521;

$s2 = ($s2 + $s1) % 65521;

}

$adler = ($s2 16) | $s1;

$this-data .= chr(($adler 24) 255);

$this-data .= chr(($adler 16) 255);

$this-data .= chr(($adler 8) 255);

$this-data .= chr($adler 255);

$this-add_chunk(“IDAT”);

//IEND: marks the end of the PNG-file

$this-data = “”;

$this-add_chunk(“IEND”);

//列印圖象

//echo($this-img_cache);

}

}//End Class

?

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/184861.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-26 05:02
下一篇 2024-11-26 05:02

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • Python換背景後,邊緣降噪怎麼辦?

    對於這個問題,我們可以從多個方面來解決。 一、背景替換的方法 在背景替換之前,我們需要先將圖像的邊緣進行處理,避免在替換過程中出現鋸齒狀的邊緣。 首先,我們可以通過腐蝕和膨脹的操作…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • Python 如何填充背景顏色

    本文將從多個方面詳細闡述如何使用 Python 填充背景顏色。 一、使用 tkinter 庫 Python 的 tkinter 庫提供了豐富的圖形界面操作功能,包括填充背景顏色的功…

    編程 2025-04-28
  • PHP獲取301跳轉後的地址

    本文將為大家介紹如何使用PHP獲取301跳轉後的地址。301重定向是什麼呢?當我們訪問一個網頁A,但是它已經被遷移到了另一個地址B,此時若服務器端做了301重定向,那麼你的瀏覽器在…

    編程 2025-04-27
  • PHP登錄頁面代碼實現

    本文將從多個方面詳細闡述如何使用PHP編寫一個簡單的登錄頁面。 1. PHP登錄頁面基本架構 在PHP登錄頁面中,需要包含HTML表單,用戶在表單中輸入賬號密碼等信息,提交表單後服…

    編程 2025-04-27
  • Python實現雪花飄落

    本文將介紹如何使用Python實現雪花飄落的效果。我們將從以下四個方面進行闡述:雪花的繪製、雪花的動畫、屏幕的刷新和主函數的編寫。 一、雪花的繪製 我們可以使用Python的tur…

    編程 2025-04-27
  • Python改背景顏色

    通過Python可以實現改變背景顏色這一功能,可以用於美化界面或者作為一種提示方式。 一、安裝必要的庫 在使用Python改變背景之前,需要先安裝必要的庫。 pip install…

    編程 2025-04-27
  • PHP與Python的比較

    本文將會對PHP與Python進行比較和對比分析,包括語法特性、優缺點等方面。幫助讀者更好地理解和使用這兩種語言。 一、語法特性 PHP語法特性: <?php // 簡單的P…

    編程 2025-04-27

發表回復

登錄後才能評論