php拼圖驗證碼,旋轉拼圖驗證碼

本文目錄一覽:

php的圖片驗證碼代碼

這個是phpcms的驗證碼,經過十幾萬個網站經驗的,非常好用

?php

session_start();

$enablegd = 1;

//判斷圖像處理函數是否存在

$funcs = array(‘imagecreatetruecolor’,’imagecolorallocate’,’imagefill’,’imagestring’,’imageline’,’imagerotate’,’imagedestroy’,’imagecolorallocatealpha’,’imageellipse’,’imagepng’);

foreach($funcs as $func)

{

if(!function_exists($func))

{

$enablegd = 0;

break;

}

}

ob_clean(); //清理緩衝

if($enablegd)

{

//create captcha

$consts = ‘cdfgkmnpqrstwxyz23456’;

$vowels = ‘aek23456789’;

for ($x = 0; $x 6; $x++)

{

$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //獲取$consts中的一個隨機數

$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //獲取$vowels中的一個隨機數

}

$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];

$_SESSION[‘checkcode’] = $string = substr($radomstring,0,4); //顯示4個字符

$imageX = strlen($radomstring)*8; //圖像的寬

$imageY = 20; //圖像的高

$im = imagecreatetruecolor($imageX,$imageY); //新建一個真彩色圖像

//creates two variables to store color

$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色

$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),

imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),

imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),

imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))

);

$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配顏色並說明透明度

$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中間背景

$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中間背景2

//與左上角的顏色相同的都會被填充

imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));

//往圖像上寫入文字

imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/ALGER.TTF’, $string[0]);

imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/ARIALNI.TTF’, $string[1]);

imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/ALGER.TTF’, $string[2]);

imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/arial.ttf’, $string[3]);

//畫邊框

$border = imagecolorallocate($im, 133, 153, 193);

imagerectangle($im, 0, 0, $imageX – 1, $imageY – 1, $border);

//畫一些隨機出現的點

$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));

for ($i=0;$i80;$i++)

{

imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);

}

//畫隨機出現的線

for ($x=0; $x9;$x++)

{

if(mt_rand(0,$x)%2==0)

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //畫線

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //畫橢圓

}

else

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);

}

}

//output to browser

header(“content-type:image/png\r\n”);

imagepng($im);

imagedestroy($im);

}

else

{

$files = glob(XINCHENG_ROOT.’images/checkcode/*.jpg’);

if(!is_array($files)) die(‘請檢查文件目錄完整性:/images/checkcode/’);

$checkcodefile = $files[rand(0, count($files)-1)]; //隨機其中一個文件

$_SESSION[‘checkcode’] = substr(basename($checkcodefile), 0, 4); //獲得文件名

header(“content-type:image/jpeg\r\n”);

include $checkcodefile;

}

?

PHP滑動拼圖驗證碼的圖片是怎樣生成的

1 如果放在項目中用,驗證碼圖片希望可以是接口返回。ImageView以及其子類支持花式加載圖片。

2 繼承自ImageView,繪製圖片本身不用我們干預,也不用我們操心scaleType,節省很多工作。

* 在onSizeChanged()方法中生成 和 控件寬高相關的屬性值:

1 初始化時隨機生成驗證碼區域起點

2 生成驗證碼區域Path

3 生成滑塊Bitmap

* onDraw()時,依次繪製:

1 驗證碼陰影

2 滑塊

php圖片驗證碼實現

可以用php的GD庫做

//隨機生成驗證碼

class randomString

{

function createRandomStr($strLen)

{

list($usec, $sec) = explode(‘ ‘, microtime());

(float) $sec + ((float) $usec * 100000);

$number = ”;

$number_len = $strLen;

$stuff = ‘1234567890abcdefghijklmnopqrstuvwxyz’;//附加碼顯示範圍ABCDEFGHIJKLMNOPQRSTUVWXYZ

$stuff_len = strlen($stuff) – 1;

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

$number .= substr($stuff, mt_rand(0, $stuff_len), 1);

}

return $number;

}

}

通過ZD庫將驗證碼變成圖片

$number = $createStr-createRandomStr(‘4’);//驗證碼的位數

$number_len = strlen($number);

$_SESSION[“VERIFY_CODE”] = $number;

// 生成驗證碼圖片

$img_width = 60;

$img_height = 20;

$img = imageCreate($img_width, $img_height);

ImageColorAllocate($img, 0x6C, 0x74, 0x70);

$white = ImageColorAllocate($img, 0xff, 0xff, 0xff);

$ix = 6;

$iy = 2;

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

imageString($img, 5, $ix, $iy, $number[$i], $white);

$ix += 14;

}

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

{

$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($img, rand()%100 , rand()%50 , $randcolor);

}

// 輸出圖片

header(“Content-type: ” . image_type_to_mime_type(IMAGETYPE_PNG));

imagepng($img);

imagedestroy($img);

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-28 12:17
下一篇 2024-12-28 12:17

相關推薦

  • PHP和Python哪個好找工作?

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

    編程 2025-04-29
  • 心形照片拼圖模板

    如何使用心形照片拼圖模板 一、模板介紹 心形照片拼圖模板是一種讓用戶可以將自己的照片拼接成一個心形的巧妙設計,每個照片都是一個拼圖塊,當所有的照片配合完成時,呈現出一個完整的心形。…

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

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

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

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

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

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

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

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

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

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

    編程 2025-04-27
  • PHP版本管理工具phpenv詳解

    在PHP項目開發過程中,我們可能需要用到不同版本的PHP環境來試驗不同的功能或避免不同版本的兼容性問題。或者我們需要在同一台服務器上同時運行多個不同版本的PHP語言。但是每次手動安…

    編程 2025-04-24
  • PHP數組去重詳解

    一、array_unique函數 array_unique是php中常用的數組去重函數,它基於值來判斷元素是否重複,具體使用方法如下: $array = array(‘a’, ‘b…

    編程 2025-04-24
  • PHP導出Excel文件

    一、PHP導出Excel文件列寬調整 當我們使用PHP導出Excel文件時,有時需要調整單元格的列寬。可以使用PHPExcel類庫中的setWidth方法來設置單元格的列寬。下面是…

    編程 2025-04-24

發表回復

登錄後才能評論