PHP自学--制图

来源:互联网 发布:js array slice 编辑:程序博客网 时间:2024/05/17 09:14

PHP绘图技术
1、  PHP绘图的基本原理和步骤
创建画布
绘制需要的各种图像(Gd库提供绘图函数)
输出图像到网页,也可以另存为
销毁图片(释放内存)
  目前网站开发的创建图片格式GIF Jpg/JPEG Ping Bmp。。
总结:
GIF:图片压缩率高,但是只能显示256色,可能造成色彩丢失。可以显示动画图片。
Jpg/JPEG:图片压缩率高(有损压缩),可以用较小的文件来显示,王爷上用的比较多。
Ping:该格式综合了上面2种优势,缺点是不能显示动画。高保真(色彩丢失少。
2、   基本绘图函数介绍

    <?php          //PHP绘图技术,       //phpinfo();           //创建画布,默认的背景是黑色的          $im=imagecreatetruecolor(400, 300);                    //绘制各种需要的图形(圆,直线,          $red=imagecolorallocate($im, 255, 0, 0);                    //圆         // imageellipse($im, 20, 20, 20, 20, $red);                    //直线          imageline($im, 0, 0, 400, 300, $red);                    //矩形          imagerectangle($im, 1, 1, 200, 100, $red);                    //填充矩形                   // imagefilledrectangle($im, 2, 2, 40, 50, $red);                    //弧线         // imagearc($im, 100, 100, 50, 100, 0, 180, $red);                    //扇形          //imagefilledarc($im, 100, 100, 50, 100, 0, 180, $red, IMG_ARC_PIE);                    //拷贝图片到画布          //1,加载原图片         // $srcImage = imagecreatefromgif("1.GIF");          //获得图片信息          //$srcImageInfo=getimagesize("1.GIF");          //2,拷贝元图片到画布         // imagecopy($im, $srcImage, 0, 0, 0, 0, $srcImageInfo[0], $srcImageInfo[1]);                    //写字(不支持中文          imagestring($im, 5, 0, 0, "I LOVE YOU 1 2 3", $red);          //写中文需要用到下面的函数         // imagettftext($im, 20, 0, 100, 100, $red, "'arial.ttf","我爱你" );                              //输出图像到网页,也可以另存                    header("Content-type: image/gif");          imagegif($im);                    //销毁图片,释放内存         imagedestroy($im);                      ?>   

下面画个饼图玩玩

<?php      //分析思路(先画扇形)            //1画布      $im=imagecreatetruecolor(400, 300);      //默认是黑色背景      $white = imagecolorallocate($im, 255,255, 255);      imagefill($im, 0, 0, $white);            //2画出扇形      //创建三个颜色      $red=imagecolorallocate($im, 254, 0, 0);      $darkred=imagecolorallocate($im, 144, 0, 0);      $green=imagecolorallocate($im, 0, 0, 128);      $darkgreen=imagecolorallocate($im, 0, 0, 80);      $gray=imagecolorallocate($im, 192, 192, 192);      $darkgray=imagecolorallocate($im, 144, 144, 144);            for($i=60;$i>=50;$i--){/*暗色*/          imagefilledarc($im, 100, $i, 150, 100, 0, 35, $darkgray, IMG_ARC_PIE);          imagefilledarc($im, 100, $i, 150, 100, 35, 75, $darkgreen, IMG_ARC_PIE);          imagefilledarc($im, 100, $i, 150, 100, 75, 360, $darkred, IMG_ARC_PIE);      }        //顶部  亮色      imagefilledarc($im, 100, 50, 150, 100, 0, 35, $gray, IMG_ARC_PIE);      imagefilledarc($im, 100, 50, 150, 100, 35, 75, $green, IMG_ARC_PIE);      imagefilledarc($im, 100, 50, 150, 100, 75, 360, $red, IMG_ARC_PIE);        //输出图像到网页,也可以另存            header("Content-type: image/gif");      imagegif($im);            // 销毁图片,释放内存      imagedestroy($im); 



0 0
原创粉丝点击