首页 > 语言 > PHP > 正文

php 图片处理函数实例教程

2024-09-04 11:44:26
字体:大 中 小
来源:转载
供稿:网友
  1. <?php  
  2. //公用函数 
  3.  
  4. //把角度转换为弧度  
  5. function deg2Arc($degrees) {  
  6. return($degrees * (pi()/180.0));  
  7. } 
  8. //RGB  
  9. function getRGB($color){  
  10.   $R=($color>>16) & 0xff;  
  11.   $G=($color>>8) & 0xff;  
  12.   $B=($color) & 0xff;  
  13.   return (array($R,$G,$B));  
  14. } 
  15.  
  16. // 取得在椭圆心为(0,0)的椭圆上 x,y点的值  
  17. function pie_point($deg,$va,$vb){  
  18. $x= cos(deg2Arc($deg)) * $va;  
  19. $y= sin(deg2Arc($deg)) * $vb;  
  20. return (array($x, $y));  
  21. } 
  22.  
  23.  
  24. //3D饼图类 
  25.  
  26. class chart{ 
  27.  
  28. var $a; //椭圆长半轴  
  29. var $b; //椭圆短半轴  
  30. var $DataArray;  //每个扇形的数据  
  31. var $ColorArray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x  
  32. //为边缘及阴影为黑色 
  33.  
  34. function chart($pa=100,$pb=60,$sData="100,200,300,400,500,300", $sColor="ee00ff,dd0000,cccccc,ccff00,00ccff,ccff00")  
  35. {  
  36.     $this->a=$pa;  
  37.     $this->b=$pb;  
  38.     $this->DataArray=split(",",$sData);  
  39.     $this->ColorArray=split(",",$sColor);  
  40. } 
  41.  
  42. function setA($v){  
  43.     $this->a=$v;  
  44. } 
  45.  
  46. function getA(){  
  47.     return $this->a;  
  48. } 
  49.  
  50. function setB($v){  
  51.     $this->b=$v;   
  52. } 
  53.  
  54. function getB(){  
  55.     return $this->b;  
  56. } 
  57.  
  58. function setDataArray($v){  
  59.     $this->DataArray=split(",",$v);  
  60. } 
  61.  
  62. function getDataArray($v){  
  63.     return $this->DataArray;  
  64. } 
  65.  
  66. function setColorArray($v){  
  67.     $this->ColorArray=split(",",$v);  
  68. } 
  69.  
  70. function getColorArray(){  
  71.     return  $this->ColorArray;  
  72. } 
  73.  
  74.    
  75. function  DrawPie(){  
  76.     $image=imagecreate($this->a*2+40,$this->b*2+40);  
  77.     $PieCenterX=$this->a+10;  
  78.     $PieCenterY=$this->b+10;  
  79.     $DoubleA=$this->a*2;  
  80.     $DoubleB=$this->b*2;  
  81.     list($R,$G,$B)=getRGB(0);  
  82.     $colorBorder=imagecolorallocate($image,$R,$G,$B);  
  83.     $DataNumber=count($this->DataArray);  
  84.       
  85.     //$DataTotal  
  86.     for($i=0;$i<$DataNumber;$i++)      $DataTotal+=$this->DataArray[$i]; //算出数据和  
  87.       
  88.     //填充背境  
  89.     imagefill($image, 0, 0, imagecolorallocate($image, 0xFF, 0xFF, 0xFF)); 
  90.  
  91.     /*  
  92.     ** 画每一个扇形  
  93.     */  
  94.     $Degrees = 0;  
  95.     for($i = 0; $i < $DataNumber; $i++){  
  96.         $StartDegrees = round($Degrees);  
  97.         $Degrees += (($this->DataArray[$i]/$DataTotal)*360);  
  98.         $EndDegrees = round($Degrees);  
  99.         $percent = number_format($this->DataArray[$i]/$DataTotal*100, 1);   
  100.         list($R,$G,$B)=getRGB(hexdec($this->ColorArray[$i]));  
  101.         $CurrentColor=imagecolorallocate($image,$R,$G,$B);  
  102.         if ($R>60 and $R<256)            $R=$R-60;  
  103.         if ($G>60 and $G<256)            $G=$G-60;  
  104.         if ($B>60 and $B<256)            $B=$B-60;  
  105.         $CurrentDarkColor=imagecolorallocate($image,$R,$G,$B);  
  106.         //画扇形弧  
  107.         imagearc($image,$PieCenterX,$PieCenterY,$DoubleA,$DoubleB,$StartDegrees,$EndDegrees,$CurrentColor);  
  108.         //画直线  
  109.         list($ArcX, $ArcY) = pie_point($StartDegrees , $this->a , $this->b);  
  110.         imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor);  
  111.         //画直线  
  112.         list($ArcX, $ArcY) = pie_point($EndDegrees,$this->a , $this->b);  
  113.         imageline($image,$PieCenterX,$PieCenterY,ceil($PieCenterX + $ArcX),ceil($PieCenterY + $ArcY),$CurrentColor);  
  114.         //填充扇形  
  115.         $MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);  
  116.         list($ArcX, $ArcY) = Pie_point($MidPoint, $this->a*3/4 , $this->b*3/4);  
  117.           
  118.         imagefilltoborder($image,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY), $CurrentColor,$CurrentColor);  
  119.         imagestring($image,2,floor($PieCenterX + $ArcX-5),floor($PieCenterY + $ArcY-5),$percent."%",$colorBorder); 
  120.  
  121.         //画阴影  
  122.         if ($StartDegrees>=0 and $StartDegrees<=180){  
  123.            if($EndDegrees<=180){      
  124.                for($k = 1; $k < 15; $k++)  
  125.                 imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentDarkColor);  
  126.            }else{  
  127.                for($k = 1; $k < 15; $k++)  
  128.                 imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, 180, $CurrentDarkColor);  
  129.            } 
  130.  
  131.         }  
  132.    }  
  133.           
  134.     /*到此脚本已经生了一幅图像了  
  135.     **现在需要的是把它发到浏览器上,重要的一点是要将标头发给浏览器,让它知道是一个GIF文件。不然的话你只能看到一堆奇怪的乱码  
  136.     */   
  137.     //输出生成的图片      
  138.     header("Content-type: image/gif");  
  139.     imagegif($image);  
  140.     imagedestroy($image);  
  141. }//End drawPie()  
  142. }//End class
  143. //开源代码Vevb.com 
  144.  
  145. //实现 
  146. $objp = new chart();  
  147. $objp->DrawPie();  
  148. ?>  

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表