分享

php绘图技术

 yliu277 2016-03-02

坐标体系

下图说明了php坐标系,坐标原点位于左上角,以像素为单位。像素是计算机屏幕上最小的显示单位。

php绘图技术

像素是个密度单位,厘米是长度单位 ,二者无法比较

 

php绘图技术

1、php绘图的基本原理和步骤

  • 创建画布
  • 绘制需要的图形
  • 输出图像到网页
  • 销毁该图片(释放内存)

php绘图技术

要用php绘图,必须在php.ini启用gd库。

 

例子:

 

<?php
$im=imagecreatetruecolor(400,300);
imagefill($im,250,250,250);            //背景色

$red=imagecolorallocate($im,255,0,0);
$blue=imagecolorallocate($im,0,0,128);
$gray=imagecolorallocate($im,192,192,192);
$dred = ImageColorAllocate($im, 144,0,0);
$dblue = ImageColorAllocate($im, 0,0,80);
$dgray = ImageColorAllocate($im, 144,144,144);


//圆形
//imageellipse($im,100,100,100,30,$red);
//直线
 //imageline($im,100,100,400,300,$red);
//矩形
//imagerectangle($im,100,100,200,250,$red);
//填充矩形
//imagefilledrectangle($im,100,100,200,250,$red);
//弧线
//imagearc($im,100,100,80,50,60,360,$red);
//扇形
//制作3D饼状图
for($i=150;$i>130;$i--){                                          //制作下面的阴影部分 逐层叠加
imagefilledarc($im,150,$i,200,100,150,20,$dred,IMG_ARC_PIE);
imagefilledarc($im,150,$i,200,100,21,90,$dblue,IMG_ARC_PIE);
imagefilledarc($im,150,$i,200,100,91,151,$dgray,IMG_ARC_PIE);
}
imagefilledarc($im,150,130,200,100,150,20,$red,IMG_ARC_PIE);
imagefilledarc($im,150,130,200,100,21,90,$blue,IMG_ARC_PIE);          //加盖最上层
imagefilledarc($im,150,130,200,100,91,151,$gray,IMG_ARC_PIE);
//拷贝图片到画布
$srcImage=imagecreatefromgif("1.gif");   //将一个gif图加入到一个新图像
$srcImageInfo=getimagesize("1.gif");     //本函数可用来取得 GIF、JPEG 及 PNG 三种 WWW 上图片的高与宽,不需要安装 GD library 就可以使用本函数。返回的数组有四个元素。返回数组的第一个元素 (索引值 0) 是图片的宽度,单位是像素 (pixel)。第二个元素 (索引值 1) 是图片的高度。第三个元素 (索引值 2) 是图片的文件格式,其值 1 为 GIF 格式、 2 为 JPEG/JPG 格式、3 为 PNG 格式。第四个元素 (索引值 3) 为图片的高与宽字符串,height=xxx width=yyy。
//拷贝原图片到目标画布

 //imagecopy($im,$srcImage,50,50,10,40,$srcImageInfo[0],$srcImageInfo[1]);
//写字
//$str="你好!";
//$str=iconv("utf-8","gb2312",$str);
//imagettftext($im,20,0,100,100,$red,"SIMYOU.ttf",$str);
//imagettftext($im, 20, 0, 100, 100, $white, "simsun.ttc", "I哈哈!!");

header("content-type:image/png");
imagepng($im);
imagedestory($im);
?>

 

使用jpgraph库开发

php绘图技术

jpgraph里的案例的代码中不能有空格,空行和其他输出,,因为这些案例输出的是图片,代码有其他输出会损坏图片(如果要让图片不被其他人看就打开图片源码加个空格)。

 

如果希望把jpgraph的图表嵌入到其他的php文件中,可以使用<img>

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约