自行產生驗證碼,中間數字從不相同的方法。


方法:

1. Header("Content-type: image/PNG") 在網頁上顯示成圖片

2. imagecreate() 創建圖片

3. ImageColorAllocate 產生顧色

4. imagestring( 圖片, int font, int x, int y, 字串, int col )
用 col 彩色將字串字串畫到 image 所代表的圖像的 x,y 坐標處(左上角為 0,0)。font字型是 1,2,3,4 或 5,是使用內建字型。

5.imagesetpixel(圖片, int x , int y , 顏色) 為繪圖一個點

6.ImagePNG(圖片) 以 PNG 格式將圖像輸出到瀏覽器或文件

7.ImageDestroy(圖片) 圖片破壞結束

程式範例:

<?php
Session_Start();
//生成新的四位元整數驗證碼
while((authnum=rand()%10000)<1000); 
HTTP_SESSION_VARS['code']=authnum;
   //生成驗證碼圖片
        Header("Content-type: image/PNG"); 
        im = imagecreate(60,50);
        black = ImageColorAllocate(im, 0,0,0);
        gray = ImageColorAllocate(im, 200,200,200);
       imagefill(im,0,0,gray);   
   //將四位元整數驗證碼繪入圖片
        imagestring(im, 5, 12, 8, authnum, black);

        for(i=0;i<50;i++)   //加入干擾象素
        {
                imagesetpixel(im, rand()%60 , rand()%80 , black);

        }

        ImagePNG(im);
        ImageDestroy(im);

?>

文章標籤
全站熱搜
創作者介紹
創作者 小甲學習地 的頭像
小甲學習地

學習資訊部落

小甲學習地 發表在 痞客邦 留言(0) 人氣(1,953)