在引见php画图功用之前,我们起首须要相识一下php中的画图坐标系:
php坐标系中,坐标原点位于左上角,以像素为单元。(引荐进修:PHP编程从入门到通晓)
坐标(x,y)——第一个是x坐标,示意当前位置为程度方向,间隔坐标原点x个像素;第二个是y坐标,示意当前位置为垂直方向,间隔坐标点y个像素。
迥殊申明:像素不是一个长度单元,而是一个密度单元。理论上,分辨率越小,一个像素所占的长度会大大一些。
php画图的基本原理和步骤
(1)建立画布
(2)绘制须要的种种图形(圆,直线,矩形,弧线,扇形...)
(3)输出图象到网页,也能够另存(能够另存为网站开辟罕见的几种图片花样,基于存储花样题目)
(4)烧毁该图片(开释内存)
下面是画验证码的代码:
<?php //echo rand(2,9); //echo "<br/>".dechex(rand(1,15))."<br/>"; session_start(); $checkCode=""; for($i=0;$i<4;$i++) { $checkCode.=dechex(rand(1,15)); } //讲随机验证码保留到session中 $_SESSION['myCheckCode']=$checkCode; //建立图片,并把随机数画上去 $img=imagecreatetruecolor(110, 30); //背景默许就是黑色 //你能够指定背景色彩 $bgcolor=imagecolorallocate($img, 0, 0, 0); imagefill($img, 0, 0, $bgcolor); //建立新的色彩 $white=imagecolorallocate($img, 255, 255, 255); $blue=imagecolorallocate($img, 0, 0, 255); $red=imagecolorallocate($img, 255, 0, 0); $green=imagecolorallocate($img, 0, 255, 0); //画出滋扰线段 for($i=0;$i<20;$i++) { //更好的要领是色彩随机 imageline($img, rand(0,110), rand(0,30), rand(0,110), rand(0,30), imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255))); } //画出噪点,本身画 //for($i=0;$i<10;$i++) //把四个随机值画上去 imagestring($img, rand(1,5), rand(2,80), rand(2,10), $checkCode, $white); //假如要运用中文 //array imagefttext(string $font_file, string $text [,array $extrainfo) //imagettftext($img,15,10,20,25,$white,"STXINWET.TTF","北京你好"); //输出 header("content-type: image/png"); imagepng($img); ?>
用于登录界面(这里只提供部份代码,然则不影响明白,可删除无关内容,直接运用)
<form action="loginProcess.php" method="post"> <table> <tr><td>用户id</td><td><input type="text" name="id" value="<?php echo getCookieVal("id"); ?>"/></td></tr> <tr><td>密 码</td><td><input type="password" name="password"/></td></tr> <tr><td>验证码</td><td><input type="text" name="checkCode"/> <img src="checkCode.php" onClick="this.src='checkCode.php?aa='+Math.random()"/></td></tr> <tr><td colspan="2">是不是保留用户id<input type="checkbox" value="yes" name="keep"></td></tr> <tr><td><input type="submit" value="用户登录"/></td> <td><input type="reset" value="从新填写"/></td></tr> </table> </form>
以上就是php画图功用有什么用处的细致内容,更多请关注ki4网别的相干文章!