PHP学习笔记08——GD绘图基础

  1 <?php
  2     //1. 使用GD库绘制图形
  3         /* 使用GD库画图分为四个基础步骤
  4          * 创建图像->绘制图像->输出图像->释放资源
  5          */
  6         /* 创建图像
  7          * imagecreate(int sx, int sy)            基于调色板的图像(256色)
  8          * imagecreatetruecolor(int sx, int sy)    基于真彩色图像,不能用于gif图像
  9          */
 10     $image = imagecreatetruecolor(800, 800);
 11         /* 设置颜色
 12          * imagecolorallocate(resource image, int red, int green, int blue)
 13          */
 14     $col_white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
 15     $col_gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
 16     $col_darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
 17     $col_navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
 18     $col_darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
 19     $col_red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
 20     $col_darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
 21     
 22     
 23         /* 简单绘制图像的函数举例
 24          * imagefill($image,$sx,$sy,$color)                        区域填充,与(sx,sy)相邻且颜色相同的电都会被填充
 25          * imagesetpixel($image,$sx,$sy,$color)                    画点  (sx,sy)
 26          * imageline($image,$sx,$sy,$ex,$ey,$color)             画线 (sx,sy)->(ex,ey)
 27          * imagerectangle($image,$sx,$sy,$ex,$ey,$color)        画空心矩形
 28          * imagefilledrectagle($image,$sx,$sy,$ex,$ey,$color)     画填充矩形
 29          * imagepolygon($image,$array,$arraynum,$colors)        画多边形
 30          * imagefilledpolygon($image,$array,$arraynum,$colors)    画填充多边形
 31          * imageellipse($image,$cx,$cy,$w,$h,$color)            画椭圆,(cx,cy)为圆心,w,h分别是宽高
 32          * imagefilledellipse($image,$cx,$cy,$w,$h,$color)        画填充椭圆
 33          * imagearc($image,$cx,$cy,$w,$h,$s,$e,$color)            画弧,s,e指的是起始和结束度数
 34          * imagefilledarc($image,$cx,$cy,$w,$h,$s,$e,$color,$style)    画填充弧,多了一个style参数
 35          */
 36     imagefill($image, 0, 0, $col_white);
 37     
 38     imagesetpixel($image, 150, 20, $col_red);
 39     imageline($image, 120, 50, 180, 50, $col_red);
 40     
 41     imagerectangle($image, 200, 30, 300, 130, $col_red);
 42     imagefilledrectangle($image, 225, 55, 275, 105, $col_red);
 43     
 44     $array_pts = array(350, 30, 400, 40, 430, 70, 370, 100);
 45     $array_ptsfilled = array(450, 30, 500, 40, 530, 70, 470, 100);
 46     imagepolygon($image, $array_pts, 4, $col_red);
 47     imagefilledpolygon($image, $array_ptsfilled, 4, $col_red);
 48     
 49     imageellipse($image, 650, 80, 150, 100, $col_red);
 50     imagefilledellipse($image, 650, 80, 75, 50, $col_red);
 51     
 52     imagearc($image, 100, 200, 150, 100, 0, 180, $col_red);
 53     imagefilledarc($image, 100, 200, 150, 100, 180, 210, $col_red, IMG_ARC_PIE);    //饼状
 54     imagefilledarc($image, 100, 200, 150, 100, 215, 245, $col_red, IMG_ARC_CHORD);    //扇形对应的三角
 55     imagefilledarc($image, 100, 200, 150, 100, 250, 280, $col_red, IMG_ARC_EDGED);    
 56     imagefilledarc($image, 100, 200, 150, 100, 285, 315, $col_red, IMG_ARC_NOFILL);
 57     imagefilledarc($image, 100, 200, 150, 100, 320, 350, $col_red, IMG_ARC_ROUNDED);
 58     
 59         //以下绘制饼形图,画出3D效果
 60     for ($i = 60; $i > 50; $i--) {
 61         imagefilledarc($image, 50, $i, 100, 50, -160, 40, $col_darknavy, IMG_ARC_PIE);
 62         imagefilledarc($image, 50, $i, 100, 50, 40, 75, $col_darkgray, IMG_ARC_PIE);
 63         imagefilledarc($image, 50, $i, 100, 50, 75, 200, $col_darkred, IMG_ARC_PIE);
 64     }
 65     
 66     imagefilledarc($image, 50, 50, 100, 50, -160, 40, $col_navy, IMG_ARC_PIE);
 67     imagefilledarc($image, 50, 50, 100, 50, 40, 75, $col_gray, IMG_ARC_PIE);
 68     imagefilledarc($image, 50, 50, 100, 50, 75, 200, $col_red, IMG_ARC_PIE);
 69     
 70     imagestring($image, 1, 15, 55, '34.7%', $col_white);
 71     imagestring($image, 1, 45, 35, '55.5%', $col_white);
 72         
 73         /* 绘制文字函数
 74          * imagestring($image,$font,$x,$y,$s,$color)        画水平字符串,font取1~5越大尺寸越大
 75          * imagestringup($image,$font,$x,$y,$s,$color)        画竖直字符串
 76          * imagechar($image,$font,$x,$y,$c,$color)            画水平字符
 77          * imagecharup($image,$font,$x,$y,$c,$color)        画竖直字符
 78          * 以上函数都只能输出内置字体,使用imagettftext输出与设备无关的truetype字体
 79          * imagettftext($image,$size,$angle,$x,$,$color,$fontfile,$text)
 80          * size字体大小(GD1是像素,GD2是点数,$angle表示角度,fontfile是字体路径,text是字符串,注意要使用UTF8编码)
 81          */
 82     $array_str = "string";
 83     imagestring($image, 4, 250, 150, $array_str, $col_red);
 84     imagestringup($image, 4, 250, 250, $array_str, $col_red);
 85     
 86     for ($i = 0; $i < strlen($array_str); $i++) {
 87         imagechar($image, 5, 350+$i*10, 150+$i*10, $array_str{$i}, $col_red);
 88         imagecharup($image, 5, 350+$i*10, 150+$i*10, $array_str{$i}, $col_red);
 89     }
 90     
 91     $text = iconv("GB2312", "UTF-8", "无兄弟,不编程");
 92     imagettftext($image, 20, 0, 450, 200, $col_red, "msyh.ttc", $text);
 93     
 94     
 95     
 96         /* 生成图像
 97          * 支持GIF,JPEG,PNG,WBMP格式,分别对应四个函数imagepng,imagegif,imagejpeg,imagewbmp
 98          * 这四个函数第一个参数都是资源名,参数二是文件名,将图片保存到文件;不提供的话会直接输出到浏览器,但是要设置header类型
 99          * imagejpeg第三个参数指定图像质量(0~100),imagewbmp第三个参数指定背景色
100          * 以下程序自动监测GD库支持的图像类型以生成对应格式的图像
101          */
102     if (function_exists("imagegif")) {
103         header('Content-type: image/gif');
104         imagegif($image);
105     } elseif (function_exists("imagejpeg")) {
106         header('Content-type: image/jpeg');
107         imagejpeg($image);
108     } elseif (function_exists("imagepng")) {
109         header('Content-type: image/png');
110         imagepng($image);
111     } elseif (function_exists("imagewbmp")) {
112         header('Content-type: image/vnd.wap.wbmp');
113         imagewbmp($image);
114     }
115         /* 销毁资源
116          */
117     imagedestroy($image);
118 
119     
120 ?>

执行结果

 

posted @ 2013-08-16 12:35  Burn_E  阅读(626)  评论(0编辑  收藏  举报