php 3D饼图类绘制类函数

 更新时间:2016年11月25日 16:58  点击:1489
下面关于php 3D饼图类绘制类函数实现原理是根据//椭圆长半轴 等参数绘制一个3D饼图形的代码。
 代码如下 复制代码

class chart{

var $a; //椭圆长半轴
var $b; //椭圆短半轴
var $DataArray;  //每个扇形的数据
var $ColorArray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x
//为边缘及阴影为黑色

function chart($pa=100,$pb=60,$sData="100,200,300,400,500,300", $sColor="ee00ff,dd0000,cccccc,ccff00,00ccff,ccff00")
{
    $this->a=$pa;
    $this->b=$pb;
    $this->DataArray=split(",",$sData);
    $this->ColorArray=split(",",$sColor);
}

function setA($v){
    $this->a=$v;
}

function getA(){
    return $this->a;
}

function setB($v){
    $this->b=$v; 
}

function getB(){
    return $this->b;
}

function setDataArray($v){
    $this->DataArray=split(",",$v);
}

function getDataArray($v){
    return $this->DataArray;
}

function setColorArray($v){
    $this->ColorArray=split(",",$v);
}

function getColorArray(){
    return  $this->ColorArray;
}

 
function  DrawPie(){
    $image=imagecreate($this->a*2+40,$this->b*2+40);
    $PieCenterX=$this->a+10;
    $PieCenterY=$this->b+10;
    $DoubleA=$this->a*2;
    $DoubleB=$this->b*2;
    list($R,$G,$B)=getRGB(0);
    $colorBorder=imagecolorallocate($image,$R,$G,$B);
    $DataNumber=count($this->DataArray);
    
    //$DataTotal
    for($i=0;$i<$DataNumber;$i++)      $DataTotal+=$this->DataArray[$i]; //算出数据和
    
    //填充背境
    imagefill($image, 0, 0, imagecolorallocate($image, 0xFF, 0xFF, 0xFF));

    /*
    ** 画每一个扇形
    */
    $Degrees = 0;
    for($i = 0; $i < $DataNumber; $i++){
        $StartDegrees = round($Degrees);
        $Degrees += (($this->DataArray[$i]/$DataTotal)*360);
        $EndDegrees = round($Degrees);
        $percent = number_format($this->DataArray[$i]/$DataTotal*100, 1); 
        list($R,$G,$B)=getRGB(hexdec($this->ColorArray[$i]));
        $CurrentColor=imagecolorallocate($image,$R,$G,$B);
        if ($R>60 and $R<256)            $R=$R-60;
        if ($G>60 and $G<256)            $G=$G-60;
        if ($B>60 and $B<256)            $B=$B-60;
        $CurrentDarkColor=imagecolorallocate($image,$R,$G,$B);
        //画扇形弧
        imagearc($image,$PieCenterX,$PieCenterY,$DoubleA,$DoubleB,$StartDegrees,$EndDegrees,$CurrentColor);
        //画直线
        list($ArcX, $ArcY) = pie_point($StartDegrees , $this->a , $this->b);
        imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor);
        //画直线
        list($ArcX, $ArcY) = pie_point($EndDegrees,$this->a , $this->b);
        imageline($image,$PieCenterX,$PieCenterY,ceil($PieCenterX + $ArcX),ceil($PieCenterY + $ArcY),$CurrentColor);
        //填充扇形
        $MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);
        list($ArcX, $ArcY) = Pie_point($MidPoint, $this->a*3/4 , $this->b*3/4);
        
        imagefilltoborder($image,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY), $CurrentColor,$CurrentColor);
        imagestring($image,2,floor($PieCenterX + $ArcX-5),floor($PieCenterY + $ArcY-5),$percent."%",$colorBorder);

        //画阴影
        if ($StartDegrees>=0 and $StartDegrees<=180){
           if($EndDegrees<=180){    
               for($k = 1; $k < 15; $k++)
                imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentDarkColor);
           }else{
               for($k = 1; $k < 15; $k++)
                imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, 180, $CurrentDarkColor);
           }

        }
   }
        
    /*到此脚本已经生了一幅图像了
    **现在需要的是把它发到浏览器上,重要的一点是要将标头发给浏览器,让它知道是一个GIF文件。不然的话你只能看到一堆奇怪的乱码
    */ 
    //输出生成的图片    
    header("Content-type: image/gif");
    imagegif($image);
    imagedestroy($image);
}//End drawPie()
}//End class


//实现

$objp = new chart();
$objp->DrawPie();

在web应用中经常会用到生成图片这一功能,在php教程 中创建图片需要gd库的支持才能创建图形,有了这个图形功能,我们就可以方便的生成缩图,验证码,给图片加水印等。

在php中要安装gd库才能正常运行创建图片功能,方法如下,在win系统,找到php.ini把

;extension=php_gd2.dll前面的";"去了,重起apache就OK了。

下面我们一看实例

php的gd库可以生成多种图像文件,如gif,png,jpg,wbmp,xpm等,下面来看一个生成正方形的文件。

<?php
$height = 300;
$width = 300;
//创建背景图
$im = ImageCreateTrueColor($width, $height);
//分配颜色
$white = ImageColorAllocate ($im, 255, 255, 255);
$blue = ImageColorAllocate ($im, 0, 0, 64);
//绘制颜色至图像中
ImageFill($im, 0, 0, $blue);
//绘制字符串:Hello,PHP
ImageString($im, 10, 100, 120, 'Hello,PHP', $white);
//输出图像,定义头
Header ('Content-type: image/png');
//将图像发送至浏览器
ImagePng($im);
//清除资源
ImageDestroy($im);
?>

查看结果只要浏览php文件就可以了,如果要图像调用<img src=a.php />

实例二,使用基本函数创建图片imagecreate()

resource imagescreate(int x,inty)

imagedestroy 是放图片所占内存空间

int ingaedestroy( image)

imagecopy()

int imagecopy( dst_im,sr_im,int x,int y,int x,int y,)

这款php生成图片与验证码图片生成原理代码,是由php gd库来支持,如果你的系统不能创建图片就把gd.dll前面的;去再,重起apache,如果是iis重起iis就OK了。

<?

 代码如下 复制代码
$w?$RESIZEWIDTH=$w:$RESIZEWIDTH=400;// 生成图片的宽度
$h?$RESIZEHEIGHT=$h:$RESIZEHEIGHT=400;// 生成图片的高度
function ResizeImage($im,$maxwidth,$maxheight,$name){
    $width = imagesx($im);
    $height = imagesy($im);
    if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
        if($maxwidth && $width > $maxwidth){
            $widthratio = $maxwidth/$width;
            $RESIZEWIDTH=true;//www.111cn.net
        }
        if($maxheight && $height > $maxheight){
            $heightratio = $maxheight/$height;
            $RESIZEHEIGHT=true;
        }
        if($RESIZEWIDTH && $RESIZEHEIGHT){
            if($widthratio < $heightratio){
                $ratio = $widthratio;
            }else{
                $ratio = $heightratio;
            }
        }elseif($RESIZEWIDTH){
            $ratio = $widthratio;
        }elseif($RESIZEHEIGHT){
            $ratio = $heightratio;
        }
        $newwidth = $width * $ratio;
        $newheight = $height * $ratio;
        if(function_exists("imagecopyresampled")){
              $newim = imagecreatetruecolor($newwidth, $newheight);
              imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }else{
            $newim = imagecreate($newwidth, $newheight);
              imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }
        ImageJpeg ($newim,$name);
        ImageDestroy ($newim);
    }else{
        ImageJpeg ($im,$name);
    }
}
 代码如下 复制代码
if($_FILES['uploadfile']['size']){
    if($_FILES['uploadfile']['type'] == "image/pjpeg"){
        $im = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']);
    }elseif($_FILES['uploadfile']['type'] == "image/x-png"){
        $im = imagecreatefrompng($_FILES['uploadfile']['tmp_name']);
    }elseif($_FILES['uploadfile']['type'] == "image/gif"){
        $im = imagecreatefromgif($_FILES['uploadfile']['tmp_name']);
    }
    if($im){
        if(file_exists('bbs.jpg')){
            unlink('www.111cn.net.jpg');
        }
        ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,'bbs.jpg');
        ImageDestroy ($im);
  
    }
}
//$uploadfile="bbs.jpg";

?>

创建图片后记得用ImageDestroy 清空内存哦。

 代码如下 复制代码

<?php教程
header("Content-type: image/jpeg");
// 载入图像
$imagen1 = imagecreatefromjpeg("imagen1.jpg");
$imagen2 = imagecreatefromjpeg("imagen2.jpg");

// 复制图像www.111cn.net
imagecopy($imagen1,$imagen2,0,0,0,0,200,150);

// 输出jpeg图像
imagejpeg($imagen1);

//释放内存
imagedestroy($imagen2);
imagedestroy($imagen1);

?>

获取图片属性代码

 代码如下 复制代码

<?php
$info = getimagesize("imagen2.jpg");
print_r($info);

?>

生成png图片的php代码

 代码如下 复制代码

<?php
//PNG格式图像处理函数
function Loadpng ($imgname) {
    $im = @ImageCreateFromPNG ($imgname);
    if (!$im) {    //载入图像失败                     
        $im = ImageCreate (400, 30);     
        $bgc = ImageColorAllocate ($im, 255, 255, 255);
        $tc  = ImageColorAllocate ($im, 0, 0, 0);
       ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc);
       ImageString($im, 4, 5, 5, "Error loading: $imgname", $tc);
    }
    return $im;
 }
 $imgPng=Loadpng("./karte.png");
   /* 输出图像到浏览器 */
 header("Content-type: image/png");
 imagePng($imgPng);
 ?>

关于php生成图形验证码我们讲过很多,这是一个非常简单的适合于php入门者的教程,一个本的用php生成验证码的实例代码。

<?php
$height = 300;
$width = 300;
//创建背景图
$im = ImageCreateTrueColor($width, $height);
//分配颜色
$white = ImageColorAllocate ($im, 255, 255, 255);
$blue = ImageColorAllocate ($im, 0, 0, 64);
//绘制颜色至图像中
ImageFill($im, 0, 0, $blue);
//绘制字符串:Hello,PHP
ImageString($im, 10, 100, 120, 'Hello,PHP', $white);
//输出图像,定义头
Header ('Content-type: image/png');
//将图像发送至浏览器
ImagePng($im);
//清除资源
ImageDestroy($im);
?>

[!--infotagslink--]

相关文章

  • Painter绘制帅气卡通魔法王子漫画教程

    今天小编在这里就来给Painter的这一款软件的使用者们来说一下绘制帅气卡通魔法王子漫画的具体教程,各位想知道绘制步骤的使用者,那么下面就快来跟着小编一起看一看教程...2016-09-14
  • php svn操作类

    以前我们开发大型项目时都会用到svn来同步,因为开发产品的人过多,所以我们会利用软件来管理,今天发有一居然可以利用php来管理svn哦,好了看看吧。 代码如下 ...2016-11-25
  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • PHP 数据库缓存Memcache操作类

    操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • Painter绘制雷神传插画教程

    今天小编在这里就来给Painter的这一款软件的使用者们来说一下绘制雷神传插画的教程,各位想知道具体绘制步骤的使用者,那么下面就快来跟着小编一起看看绘制方法吧。 ...2016-09-14
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Php文件上传类class.upload.php用法示例

    本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。 简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25