php 的圖像處理在驗證碼是最常見的,下面說下使用php創建圖像的具體步驟。
簡要說明:PHP 并不僅限于創建 HTML 輸出, 它也可以創建和處理包括 GIF,PNG(推薦),JPEG,WBMP以及XPM 在內的多種格式的圖像。 更加方便的是,PHP 可以直接將圖像數據流輸出到瀏覽器。 要想在 PHP 中使用圖像處理功能,你需要連帶 GD 庫一起來編譯 PHP。 GD 庫和 PHP 可能需要其他的庫, 這取決于你要處理的圖像格式。
你可以使用PHP中的圖像函數來獲取下列格式圖像:JPEG,GIF,PNG(推薦:創建出來的圖像不失針),SWF,TIFF 和 JPEG2000。
步驟說明:具體函數說明請查看php手冊
- <?php
- //第一:設定標頭,告訴瀏覽器你要生成的MIME 類型
- header("Content-type: image/png");
- //第二:創建一個畫布,以后的操作都將基于此畫布區域
- $codew= 100;
- $codeh= 60;
- $codeimg= imagecreatetruecolor($codew,$codeh);
- //獲取畫布顏色
- $red= imagecolorallocate($codeimg, 255, 0, 0);
- $white= imagecolorallocate($codeimg, 255, 255, 255);
- $green= imagecolorallocate($codeimg, 75, 222, 26);
- //第三:填充畫布背景顏色
- imagefill($codeimg, 0, 0,$red);
- //第四:繪制線條 + 填充文字...
- imageline($codeimg, 0, 00, 30, 60,$white);
- imageline($codeimg, 0, 00, 50, 60,$white);
- imageline($codeimg, 0, 00, 80, 60,$white);
- //填充文字
- imagestring($codeimg, 10, 30, 30,"qwe4",$green);
- //Vevb.com
- //第五:輸出創建的畫布
- imagepng($codeimg);
- //第六:銷毀畫布
- imagedestroy($codeimg);
- ?>
新聞熱點
疑難解答