圖形圖像處理技術,gd庫的強大支持,PHP的圖像可以是PHP的強項,PHP圖形化類庫,jpgraph是一款非常好用的強大的圖形處理工具。
在PHP中加載GD庫
gd官方網址下載:
http://www.boutell.com/gd
激活gd庫,修改php.in文件
將該文件中的“;extension=php_gd2.dll”選項前的分號“;”刪除
驗證GD庫是否安裝成功
輸入“127.0.0.1/phpinfo.php”並按Enter鍵,檢索到的安裝信息,即說明GD庫安裝成功。
Jpgraph的安裝與配置
官方網站http://www.aditus.nu/jpgraph/下載
解壓到文件夾,編輯php.ini文件,修改include_path參數,如include_path = ".;F:\AppServ\www\jpgraph",重新啟動Apache。
配置Jpgraph類庫的文件jpg-config.inc.php,
支持中文的配置
DEFINE('CHINESE_TTF_FONT','bkai00mp.ttf');
默認圖片格式的配置
DEFINE("DEFAULT_GFORMAT","auto");
創建畫布,可以通過imagecreate()函數實現
<?php $im = imagecreate(200,100); $white = imagecolorallocate($im, 255,65,150); imagegif($im); ?>
gd庫支持中文,但只能是utf-8,使用imageString()會顯示亂碼,只能接收utf-8編碼格式,默認使用英文字體。
header()函數定義輸出圖像類型
imagecreatefromjpeg()函數載入圖片
imagecolorallocate()函數設置輸出字體顏色
iconv()函數對輸出的中文字符串的編碼格式進行轉換
imageTTFText()函數向照片中添加文字
<?php header("content-type:image/jpeg"); $im = imagecreateformjpeg("images/photo.jpg"); $textcolor = imagecolorallocate($im, 35,35,23); //定義字體 $fnt="c:/windows/fonts/simhei.ttf"; //定義輸出字體串 $motto = iconv("gb2312","utf-8","長白山"); // 寫文字 imageTTFText($im, 220,0,200,233, $textcolor, $fnt, $motto); // 寫TTF文字到圖中 // 簡歷里jpeg圖形 imageipeg($im); imagedestory($im); ?>
使用圖像處理技術生成的驗證碼
<?php session_start(); header("content-type:image/png"); //設置創建圖像的格式 $image_width=70; //設置圖像寬度 $image_height=18; //設置圖像高度 srand(microtime()*100000); //設置隨機數的種子 for($i=0;$i<4;$i++){ //循環輸出一個4位的隨機數 $new_number.=dechex(rand(0,15)); } $_SESSION[check_checks]=$new_number; //將獲取的隨機數驗證碼寫入到SESSION變量中 $num_image=imagecreate($image_width,$image_height); //創建一個畫布 imagecolorallocate($num_image,255,255,255); //設置畫布的顏色 for($i=0;$i<strlen($_SESSION[check_checks]);$i++){ //循環讀取SESSION變量中的驗證碼 $font=mt_rand(3,5); //設置隨機的字體 $x=mt_rand(1,8)+$image_width*$i/4; //設置隨機字符所在位置的X坐標 $y=mt_rand(1,$image_height/4); //設置隨機字符所在位置的Y坐標 $color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //設置字符的顏色 imagestring($num_image,$font,$x,$y,$_SESSION[check_checks][$i],$color); //水平輸出字符 } imagepng($num_image); //生成PNG格式的圖像 imagedestroy($num_image); //釋放圖像資源 ?>
<?php session_start(); if($_POST["Submit"]!=""){ $checks=$_POST["checks"]; if($checks==""){ echo "<script> alert('驗證碼不能為空');window.location.href='index.php';</script>"; } if($checks==$_SESSION[check_checks]){ echo "<script> alert('用戶登錄成功!');window.location.href='index.php';</script>"; }else{ echo "<script> alert('您輸入的驗證碼不正確!');window.location.href='index.php';</script>"; } } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>驗證碼應用</title> <style type="text/css"> <!-- .STYLE1 { font-size: 12px; color: #FFFFFF; font-weight: bold; } .style2 {font-weight: bold; font-size: 12px;} --> </style> </head> <body> <form name="form" method="post" action=""> <table width="1003" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="168" height="169" background="images/index_01.gif"> </td> <td width="685" background="images/index_02.gif"> </td> <td width="150" background="images/index_03.gif"> </td> </tr> <tr> <td width="168" height="311" background="images/index_04.gif"> </td> <td background="images/index_05.gif"><table width="675" height="169" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="43" align="center" valign="baseline"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="baseline"> </td> </tr> <tr> <td width="382" height="24" align="center" valign="baseline"> </td> <td width="207" height="24" valign="middle"><span class="style2">用戶名</span><span class="STYLE1"> <input name="txt_user" id="txt_user" style="height:20px " size="10"> </span></td> <td width="86" height="24" align="center" valign="baseline"> </td> </tr> <tr> <td height="24" align="center" valign="baseline"> </td> <td height="24" valign="middle"><span class="style2">密碼</span><span class=