- <?php
- header("Content-type:image/png");
- header("Content-Disposition:filename=image_code.png");
- //定義 header 的文件格式為 png,第二個定義其實沒什麼用
- // 開啟 session
- if (!isset($_SESSION)) { session_start(); }
- // 設定亂數種子
- mt_srand((double)microtime()*1000000);
- // 驗證碼變數
- $verification__session = '';
- // 定義顯示在圖片上的文字,可以再加上大寫字母
- $str = 'abcdefghijkmnpqrstuvwxyz1234567890';
- $l = strlen($str); //取得字串長度
- //隨機取出 6 個字
- for($i=0; $i<6; $i++){
- $num=rand(0,$l-1);
- $verification__session.= $str[$num];
- }
- // 將驗證碼記錄在 session 中
- $_SESSION["verification__session"] = $verification__session;
- // 圖片的寬度與高度
- $imageWidth = 160; $imageHeight = 50;
- // 建立圖片物件
- $im = @imagecreatetruecolor($imageWidth, $imageHeight)
- or die("無法建立圖片!");
- //主要色彩設定
- // 圖片底色
- $bgColor = imagecolorallocate($im, 255,239,239);
- // 文字顏色
- $Color = imagecolorallocate($im, 255,0,0);
- // 干擾線條顏色
- $gray1 = imagecolorallocate($im, 200,200,200);
- // 干擾像素顏色
- $gray2 = imagecolorallocate($im, 200,200,200);
- //設定圖片底色
- imagefill($im,0,0,$bgColor);
- //底色干擾線條
- for($i=0; $i<10; $i++){
- imageline($im,rand(0,$imageWidth),rand(0,$imageHeight),
- rand($imageHeight,$imageWidth),rand(0,$imageHeight),$gray1);
- }
- //利用true type字型來產生圖片
- imagettftext($im, 20, 0, 25, 35, $Color,
- "/var/lib/defoma/fontconfig.d/D/DejaVu-Serif-Bold.ttf",
- $verification__session);
- /*
- imagettftext (int im, int size, int angle,
- int x, int y, int col,
- string fontfile, string text)
- im 圖片物件
- size 文字大小
- angle 0度將會由左到右讀取文字,而更高的值表示逆時鐘旋轉
- x y 文字起始座標
- col 顏色物件
- fontfile 字形路徑,為主機實體目錄的絕對路徑,
- 可自行設定想要的字型
- text 寫入的文字字串
- */
- // 干擾像素
- for($i=0;$i<90;$i++){
- imagesetpixel($im, rand()%$imageWidth ,
- rand()%$imageHeight , $gray2);
- }
- imagepng($im);
- imagedestroy($im);
輸出結果:
參考文章:php確認碼圖片
7 回應:
Good, Thank you! 推。
Good, THank You
你的部落格很棒,加油!
不好意思 請問一下
我把程式碼放到本機執行後 出現
Parse error: syntax error, unexpected T_STRING in C:\wamp\www\aaa\header.php on line 42
我程式碼40~42行是這樣
40// 建立圖片物件
41 $im= @imagecreatetruecolor ($imageWidth, $imageHeight)
42 ordie("無法建立圖片!");
怎麼會這樣@@?
最近剛接觸PHP不曉得這是甚麼問題0.0
是 or die 不是 ordie
你需要好用的工具來幫助你
phpEclipse Vim
不好意思 我又來了0.0
剛剛那問題我解決了
現在換成出現
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\aaa\header.php:9) in C:\wamp\www\aaa\header.php on line 10
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\aaa\header.php:9) in C:\wamp\www\aaa\header.php on line 11
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\aaa\header.php:9) in C:\wamp\www\aaa\header.php on line 15
我把10 11 15行用//把他關掉
問題沒出現了但是一片空白
後來我把
放到第一行
剩下10 11行問題還沒解決
你在 <?php 前面出現不正常的字元
這個錯誤訊息在網路上可以找到很多
簡單的說在 header() 及 session_start() 之前不能有輸出
張貼留言