php產生驗證碼檔案的方法

來源:互聯網
上載者:User
本篇文章主要介紹php產生驗證碼檔案的方法,感興趣的朋友參考下,希望對大家有所協助。

本文執行個體講述了php實現的驗證碼檔案類,具體如下:

<?php/*** @file* @version 1.0* @author  網海浪子* @brief   驗證碼檔案類**/class CCheckCodeFile{//驗證碼位元private $mCheckCodeNum = 4;//產生的驗證碼private $mCheckCode  = '';//驗證碼的圖片private $mCheckImage = '';//幹擾像素private $mDisturbColor = '';//驗證碼的圖片寬度private $mCheckImageWidth = '80';//驗證碼的圖片寬度private $mCheckImageHeight = '20';/**** @brief  輸出頭**/private function OutFileHeader(){ header ("Content-type: image/png");}/**** @brief  產生驗證碼**/private function CreateCheckCode(){ $this->mCheckCode = strtoupper(substr(md5(rand()),0,$this->mCheckCodeNum));  return $this->mCheckCode;} /**** @brief  產生驗證碼圖片**/private function CreateImage(){ $this->mCheckImage = @imagecreate ($this->mCheckImageWidth,$this->mCheckImageHeight); imagecolorallocate ($this->mCheckImage, 200, 200, 200);  return $this->mCheckImage;}/**** @brief  設定圖片的幹擾像素**/private function SetDisturbColor(){  for ($i=0;$i<=128;$i++)  {  $this->mDisturbColor = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));  imagesetpixel($this->mCheckImage,rand(2,128),rand(2,38),$this->mDisturbColor);  }}/**** @brief  設定驗證碼圖片的大小** @param  $width  寬** @param  $height 高**/public function SetCheckImageWH($width,$height){ if($width==''||$height=='')return false; $this->mCheckImageWidth = $width; $this->mCheckImageHeight = $height; return true;}/**** @brief  在驗證碼圖片上逐個畫上驗證碼**/private function WriteCheckCodeToImage(){  for ($i=0;$i<=$this->mCheckCodeNum;$i++)  {  $bg_color = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,128), rand(0,255));  $x = floor($this->mCheckImageWidth/$this->mCheckCodeNum)*$i;  $y = rand(0,$this->mCheckImageHeight-15);  imagechar ($this->mCheckImage, 5, $x, $y, $this->mCheckCode[$i], $bg_color);  }}/**** @brief  輸出驗證碼圖片**/public function OutCheckImage(){ $this ->OutFileHeader(); $this ->CreateCheckCode(); $this ->CreateImage(); $this ->SetDisturbColor(); $this ->WriteCheckCodeToImage(); imagepng($this->mCheckImage); imagedestroy($this->mCheckImage);}}$c_check_code_image = new CCheckCodeFile();//$c_check_code_image ->SetCheckImageWH(100,50);//設定顯示驗證碼圖片的尺寸$c_check_code_image ->OutCheckImage();?>

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

相關推薦:

php採用鏈式調用的方式連續調用函數的使用方

php數字及數字運算驗證碼法

php針對檔案的遞迴遍曆及重新命名的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.