專做系統家具、系統櫃
客製化流暢舒適居家,免費估價規劃
拆除全系列活動百葉窗.防火安全門.氣密隔音窗
堅固耐用.工廠直營.歡迎洽詢

首頁  •  j2h 論壇 • 程式設計討論     • 

PHP驗証碼

房東:驕子
發表時間:2007-04-12
[檢舉]


class Image{
//----------驗証碼部分----------
var $num=\'4\';//隨機數字長度
var $str;//隨機出現四個字
var $width=\'80\';//圖片寬度
var $height=\'20\';//圖片高度
var $im;//驗証碼圖片
var $bg;//圖片背景
var $color;//字體顏色

//----------圖片縮略圖部分----------
var $fname;//檔案名
var $pname;//上傳時post過來的名字
var $ftype;//得到圖片檔案的類型
var $ywidth;//原檔案的寬度
var $yheight;//原檔案的高度
var $nwidth=\'107\';//剪後的圖片寬度
var $nheight=\'100\';//剪後的圖片高度
var $newimage;//剪成功\後的圖片
var $omage;//寫在原圖片上
var $spath;//縮略圖儲存的路徑

//----------圖片水印部分----------
var $text=\'www.xxxx.com\';//水印文字
var $tca=\'255\';//可設定的字體顏色--red
var $tcb=\'255\';//可設定的字體顏色--green
var $tcc=\'255\';//可設定的字體顏色--blue
var $rcolor;//讀取字體顏色寫進水印
var $sx;//文字的x軸位置
var $sy;//文字的y軸位置
var $twidth=\'166\';//文字的寬度
var $theight=\'16\';//文字的高度

//----------驗証碼部分-----------------------------------------
function Head(){
header(\"Content-type: image/png\");
}
function Rand(){
$this->str=substr(md5(rand()),0,$this->num);
return $this->str;
}
function Creat(){
$this->im=imagecreate($this->width,$this->height);
$this->bg = imagecolorallocate($this->im, 255, 255, 255);
$this->color = imagecolorallocate($this->im, 0, 0, 0);
return $this->im;
return $this->bg;
return $this->color;
}
function Write(){
for($i=0;$i<$this->num;$i++){
$x=ceil($this->width/$this->num)*$i+5;
imageString($this->im,5,$x,2, $this->str[$i],$this->color);
}
}
function Out(){
$this->Head();
$this->Rand();
$this->Creat();
$this->Write();
imagepng($this->im);
}
//-----------驗証碼使用方法 $im->Out(); ----------------------------------

//----------圖片水印部分-----------------------------------------------------
function Gettype(){
$this->fname=\"0.jpg\";//$this->fname=$_POST[$this->pname];
list($width, $height, $type) = getimagesize($this->fname);
$this->ywidth=$width;
$this->yheight=$height;
$this->ftype=$type;
return $this->fname;
return $this->ywidth;
return $this->yheight;
return $this->ftype;
}
function Gethead(){
switch ($this->ftype){
case 1;
header(\'Content-type: image/gif\');
break;
case 2;
header(\'Content-type: image/jpeg\');
break;
case 3;
header(\'Content-type: image/png\');
break;
}
}
function Setnewimage($width,$height){
$this->nwidth=$width;
$this->nheight=$height;
return $this->nwidth;
return $this->nheight;
}
function Getnewimage(){
$this->newimage=imagecreatetruecolor($this->nwidth,$this->nheight);
return $this->newimage;
}
function Setspath($spath){
$this->spath=$spath;
return $this->spath;
}
function Getnewtype(){
switch ($this->ftype){
case 1;
$this->omage=imagecreatefromgif($this->fname);
break;
case 2;
$this->omage=imagecreatefromjpeg($this->fname);
break;
case 3;
$this->omage=imagecreatefrompng($this->fname);
break;
}
return $this->omage;
}
function imageresampled (){
imagecopyresampled($this->newimage,$this->omage,0,0,0,0,$this->nwidth,$this->nheight,$this->ywidth,$this->yheight);
}
function toimage(){
switch ($this->ftype){
case 1;
imagegif($this->newimage);//imagegif($this->newimage,$this->spath);
break;
case 2;
imagejpeg($this->newimage);//imagejpeg($this->newimage,$this->spath);
break;
case 3;
imagepng($this->newimage);//imagepng($this->newimage,$this->spath);
break;
}
return $this->omage;
}
function Getout(){
$this->Gettype();
$this->Gethead();
$this->Getnewimage();
$this->Getnewtype();
$this->imageresampled();
$this->toimage();
}
//-----------縮略圖使用方法 $im->Getout(); ----------------------------------

//----------圖片水印部分-------------------------------------------------------
function text($string){
$this->text=iconv(\"GB2312\",\"UTF-8\",$string);
return $this->text;
}
function Settcolor($tca,$tcb,$tcc){
$this->tca=$tca;
$this->tcb=$tcb;
$this->tcc=$tcc;
return $this->tca;
return $this->tcb;
return $this->tcc;
}
function Gettcolor(){
$this->rcolor=imagecolorallocate($this->omage,$this->tca,$this->tcb,$this->tcc);
}
function Getpostion($i){
$this->Gettype();
switch ($i){
case 0;//左上角
$this->sx=0;
$this->sy=0;
break;
case 1;//上的中間
$this->sx=$this->ywidth/2-$this->twidth/2;
$this->sy=0;
break;
case 2;//右上角
$this->sx=$this->ywidth-$this->twidth;
$this->sy=0;
break;
case 3;//左的中間
$this->sx=0;
$this->sy=$this->yheight/2-$this->theight;
break;
case 4;//中間
$this->sx=$this->ywidth/2-$this->twidth/2;
$this->sy=$this->yheight/2-$this->theight;
break;
case 5;//右的中間
$this->sx=$this->ywidth-$this->twidth;
$this->sy=$this->yheight/2-$this->theight;
break;
case 6;//左下角
$this->sx=0;
$this->sy=$this->yheight-$this->theight;
break;
case 7;//下的中間
$this->sx=$this->ywidth/2-$this->twidth/2;
$this->sy=$this->yheight-$this->theight;
break;
case 8;//右下角
$this->sx=$this->ywidth-$this->twidth;
$this->sy=$this->yheight-$this->theight;
break;
}
return $this->sx;
return $this->sy;
}
function Writestring(){
imagestring($this->omage,5,$this->sx,$this->sy,$this->text,$this->rcolor);
//imagettftext($this->omage,16,0,$this->sx+100,$this->sy+100,$this ->rcolor,\"simsun.ttc\",$this->text);//中文水印用這句
}
function imagetosy(){
switch ($this->ftype){
case 1;
imagegif($this->omage);//imagegif($this->omage,$this->spath);
break;
case 2;
imagejpeg($this->omage);//imagejpeg($this->omage,$this->spath);
break;
case 3;
imagepng($this->omage);//imagepng($this->omage,$this->spath);
break;
}
return $this->omage;
}
function Syinout(){
$this->Gettype();
$this->Gethead();
$this->Getnewtype();
$this->Gettcolor();
$this->Writestring();
$this->imagetosy();
}
//-----------縮略圖使用方法 $im->Syinout(); ----------------------------------
}
$im=new Image;

//$im->Out();//輸出驗証碼

//$im->Setnewimage(150,150);//如果這裡不設定,輸出的縮略圖為預設的大小
//$day = date(\"Ymdhis\");
//$fname=$day.\".jpg\";
//$im->Setspath($fname);//縮略圖的儲存路徑
//$im->Getout();//輸出為縮略圖

//$im->text(\"www.xxxxx.com.cn\");//設定文字水印,不用則為預設值
//$im->Settcolor(0,0,0);//設定文字水印的顏色,不用則為預設值
//$im->Getpostion(8);//控制水印文字的位置,預設為左上角
//$im->Syinout();//輸出為水印圖片
?>
圖片驗証

//001.php
session_start();
session_register(\"login_check_number\");
//昨晚看到了chianren上的驗証碼效果,就考慮了一下,用PHP的GD庫完成了類似功\能
//先成生背景,再把生成的驗証碼放上去
$img_height=64; //先定義圖片的長、寬
$img_width=24;
//if($HTTP_GET_VARS[\"act\"]== \"init\"){
//srand(microtime() * 100000);//PHP420後,srand不是必須的
for($Tmpa=0;$Tmpa<4;$Tmpa++){
$nmsg.=dechex(rand(0,15));
//}//by sports98


$HTTP_SESSION_VARS[login_check_number] = $nmsg;

$HTTP_SESSION_VARS[login_check_number] = strval(mt_rand(\"1111\",\"9999\")); //生成4位的隨機數,放入session中
//誰能做下補充,可以同時生成字母和數字啊??----由sports98完成了

$aimg = imageCreate($img_height,$img_width); //生成圖片
ImageColorAllocate($aimg, 255,255,255); //圖片底色,ImageColorAllocate第1次定義顏色PHP就認為是底色了
$black = ImageColorAllocate($aimg, 0,0,0); //定義需要的黑色

//下面該生成雪花背景了,其實就是在圖片上生成一些符號
for ($i=1; $i<=200; $i++)
{ //先用100個做測試
imageString($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),\"*\",imageColorAllocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
//哈,看到了吧,其實也不是雪花,就是生成﹡號而已。為了使它們看起來\"雜亂無章、5顏6色\",就得在1個1個生成它們的時候,讓它們的位置、顏色,甚至大小都用隨機數,rand()或mt_rand都可以完成。
}
ImageRectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//先成一黑色的矩形把圖片包圍

//上面生成了背景,現下就該把已經生成的隨機數放上來了。道理和上面差不多,隨機數1個1個地放,同時讓他們的位置、大小、顏色都用成隨機數~~
//為了區別於背景,這裡的顏色不超過200,上面的不小於200
for ($i=0; $i < strlen($HTTP_SESSION_VARS[login_check_number]); $i++)
{
imageString($aimg, mt_rand(3,5),$i*$img_height/4+mt_rand(1,10),mt_rand(1,$img_width/2), $HTTP_SESSION_VARS[login_check_number][$i],imageColorAllocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
}
Header(\"Content-type: image/png\"); // 告訴瀏覽器,下面的數據是圖片,而不要按文字顯示
ImagePng($aimg); // 生成png格式。。。嘿嘿效果蠻像回事的嘛。。。
ImageDestroy($aimg);
}

?>
\"\"



  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!



  •  共 0 人回應

    姓名:
    佈告內容: