php圖像驗證碼生成代碼
更新時間:2017年06月08日 11:59:55 作者:cakin24
這篇文章主要為大家詳細
介紹了php圖像驗證碼的生成代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
介紹了php圖像驗證碼的生成代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了php封裝的一個生成圖像驗證碼,供大家參考,具體內(nèi)容如下
一、代碼
index..php
TestCode.php
<?php
class TestCode{//創(chuàng)建類名為TestCode
private $width;
private $height;
private $str;
private $im;
private $strColor;
function __construct($width,$height){
$this->width=$width;
$this->height=$height;
$this->str=$_GET['code'];
$this->createImage();
}
function createImage(){
$this->im=imagecreate($this->width,$this->height);//創(chuàng)建畫布
imagecolorallocate($this->im,200,200,200);//為畫布添加顏色
for($i=0;$i<4;$i++){//循環(huán)輸出四個數(shù)字
$this->strColor=imagecolorallocate($this->im,rand(0,100),rand(0,100),rand(0,100));
imagestring($this->im,rand(3,5),$this->width/4*$i+rand(5,10),rand(2,5),$this->str[$i],$this->strColor);
}
for($i=0;$i<200;$i++){//循環(huán)輸出200個像素點
$this->strColor=imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($this->im,rand(0,$this->width),rand(0,$this->height),$this->strColor);
}
}
function show(){//
header('content-type:image/png');//定義輸出為圖像類型
imagepng($this->im);//生成圖像
imagedestroy($this->im);//銷毀圖像釋放內(nèi)存
}
}
$image=new TestCode(80,20);//將類實例化為對象
$image->show();//調(diào)用函數(shù)
?> 二、運行結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
探討Smarty中如何獲取數(shù)組的長度以及smarty調(diào)用php函數(shù)的詳解
本篇文章是對Smarty中如何獲取數(shù)組的長度以及smarty調(diào)用php函數(shù)的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06
PHP中soap用法示例【SoapServer服務端與SoapClient客戶端編寫】
這篇文章主要介紹了PHP中soap用法,結(jié)合實例形式分析了SoapServer服務端與SoapClient客戶端相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2018-12-12

