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