绘图属于php的高级部分,而且在实际的应用开发的过程中也是比较实用,在很多地方都可以看到技术的应用,如统计资料,折线,柱状等实时更新的图等,不可能通过纯美工的方法来实现,因为数据呈现着不可确定性,在一般的开发条件下,数据的获取是来自于服务器的,然而我们要根据这些数据进行友好的表示出来,即图形化的方式,在没有学习绘图技术之前是很难实现的,难度之处在于“变化”,所以,可以利用变量的方式来改变视图成为解决问题的首选方式,下面就是列举的一些php绘图的简单创建以及柱状图的制作过程:
一般的过程:
<?php
$img_1=imagecreatetruecolor(400,300);//创键画布
$mycolor_1=imagecolorallocate($img_1, 255, 0, 0);//定义一个颜色(红色)
$mycolor_2=imagecolorallocate($img_1, 232,222,13);//定义一个颜色(黄色)
/*imageellipse($img_1, 150, 150, 50, 50, $mycolor_1);//定义绘的图的位置大小颜色(椭圆)
imageline($img_1, 150, 0, 150, 175, $mycolor_1);//画一个直线,起始的坐标,颜色
imagerectangle($img_1, 125, 175, 175, 225, $mycolor_1);//画一个矩形
imagefilledrectangle($img_1, 125, 175, 175, 225, $mycolor_2);//画一个填充矩形
imagearc($img_1, 100, 100, 50, 50, 0, 30, $mycolor_2);//单位角度的弧
imagefilledarc($img_1, 50, 200, 100, 100, 30, 120, $mycolor_1,IMG_ARC_PIE);//一个扇形*/
$add_image=imagecreatefromgif("./file/01.gif");//加载原图片
$image_information=getimagesize("./file/01.gif");//获取图片的宽和高
imagecopy($img_1, $add_image, 0, 0, 0, 0, $image_information[0], $image_information[1]);//复制目标图片到画板
//imagestring($img_1, 5, 200, 200, "Hello徐宁", $mycolor_1);//写入字符,不建议,很弱
$str1="你好!!";
imagettftext($img_1, 10, 30, 200, 200, $mycolor_1, "./file/02.TTC", $str1);//可以去window/fonts去找。
header("Content-type: image/png");//定义返回数据类型
imagepng($img_1);//输出并保存图像
imagedestroy($img_1);//销毁服务器资源
?>
下面是我测试制作柱状图的一份实例代码:
<?php
//创建画布
$img_1=imagecreatetruecolor(500, 500);
$white=imagecolorallocate($img_1, 255, 255, 255);
//填充画布的背景色
imagefill($img_1, 0, 0, $white);
//分别对应红蓝灰还有他们的暗色
$red=imagecolorallocate($img_1, 255,24,0);
$darkred=imagecolorallocate($img_1, 146,12,0);
$blue=imagecolorallocate($img_1, 11,6,139);
$darkblue=imagecolorallocate($img_1, 5,5,52);
$gary=imagecolorallocate($img_1, 193,193,193);
//从低至上进行叠加全部为暗色
$darkgary=imagecolorallocate($img_1, 142,142,144);
for($i=60; $i>=50; $i--){
imagefilledarc($img_1, 100, $i, 100, 50, 0, 35, $darkblue, IMG_ARC_PIE);
imagefilledarc($img_1, 100, $i, 100, 50, 35, 75, $darkgary, IMG_ARC_PIE);
imagefilledarc($img_1, 100, $i, 100, 50, 75, 360, $darkred, IMG_ARC_PIE);
}
//在最高层添加纯色层
imagefilledarc($img_1, 100, 50, 100, 50, 0, 35, $blue, IMG_ARC_PIE);
imagefilledarc($img_1, 100, 50, 100, 50, 35, 75, $gary, IMG_ARC_PIE);
imagefilledarc($img_1, 100, 50, 100, 50, 75, 360, $red, IMG_ARC_PIE);
//定义,保存,显示,销毁
header("Content-type: image/png");
imagepng($img_1);
imagedestroy($img_1);
?>
上面还涉及到一些简单的算法,不是很难,但相对比较麻烦,但其对于数据的动态展现优势是显而易见的。