怎么使用php的gd库画一条抗锯齿的粗斜线

php中文网
发布: 2016-06-13 13:32:42
原创
2368人浏览过

如何使用php的gd库画一条抗锯齿的粗斜线?
如题,使用多边形画的填充图是有锯齿的。imageantialias函数是无效的。

------解决方案--------------------
点阵图有锯齿是必然的
imageantialias 的抗锯齿是有作用的,你可以对比他打开和关闭时的效果
如果你对 imageantialias 的效果不满意(毕竟GD不是专业的图像处理包),那就要你自己编程解决了

------解决方案--------------------
他是扰码加密的,有空研究一下
------解决方案--------------------
好无聊啊,最终是用 js 实现的
------解决方案--------------------
JS有现成的插件 做出来的很漂亮。。。用PHP干嘛
------解决方案--------------------
给你一个早年写的代码,供参考

PHP code
<?php //error_reporting(E_ALL ^ E_NOTICE);

class Graph {
  /**
   * 伪3D绘图类
   */
  var $im;
  var $type = "gif"; // png,gif,jpeg,wbmp
  var $xo;
  var $yo;
  var $color;
  var $fillcolor;
  var $filldarkcolor;
  var $filltype = true;
  var $orgx;
  var $orgy;
  var $viewx;
  var $viewy;
  var $winx;
  var $winy;
  var $extx = 1;
  var $exty = -1;
  var $ksin;
  var $kcos;

  function Graph() {
    if(func_num_args() == 2)
        $this->create(func_get_arc(0),func_get_arc(1));
    else
        $this-&gt;create(400,300);
  }
  /**
   * 在($x,$y)处画点
   */
  function pixel($x,$y) {
    $p = $this-&gt;get_view($x,$y);
    imagesetpixel($this-&gt;im,$p['x'],$p['y'],$this-&gt;color);
    $this-&gt;xo = $p['x'];
    $this-&gt;yo = $p['y'];
  }
  /**
   * 移动到($x,$y)处
   */
  function moveto($x,$y) {
    $p = $this-&gt;get_view($x,$y);
    $this-&gt;xo = $p['x'];
    $this-&gt;yo = $p['y'];
  }
  /**
   * 从($x1,$y1)到($x2,$y2)处画线
   */
  function line($x1,$y1,$x2,$y2) {
    $p1 = $this-&gt;get_view($x1,$y1);
    $p2 = $this-&gt;get_view($x2,$y2);
    imageline($this-&gt;im,$p1['x'],$p1['y'],$p2['x'],$p2['y'],$this-&gt;color);
  }
  /**
   * 从当前位置到($x,$y)处画线
   */
  function lineto($x,$y) {
    $p = $this-&gt;get_view($x,$y);
    imageline($this-&gt;im, $this-&gt;xo, $this-&gt;yo, $p['x'], $p['y'], $this-&gt;color);
    $this-&gt;xo = $p['x'];
    $this-&gt;yo = $p['y'];
  }
  /**
   * 设置当前颜色
   */
  function color($clr) {
    $r = ($clr&gt;&gt;16) &amp; 0xff; 
    $g = ($clr&gt;&gt;8) &amp; 0xff; 
    $b = ($clr) &amp; 0xff; 
    $this-&gt;color = ImageColorAllocate($this-&gt;im, $r,$g,$b);
    $this-&gt;fillcolor = ImageColorAllocate($this-&gt;im, $r,$g,$b);
    $this-&gt;filldarkcolor = ImageColorAllocate($this-&gt;im, $r/2,$g/2,$b/2);
    return $this-&gt;color;
  }
  /**
   * 设置当前充填颜色
   */
  function fillcolor($clr) {
    $r = ($clr&gt;&gt;16) &amp; 0xff; 
    $g = ($clr&gt;&gt;8) &amp; 0xff; 
    $b = ($clr) &amp; 0xff; 
    $this-&gt;fillcolor = ImageColorAllocate($this-&gt;im, $r,$g,$b);
    return $this-&gt;fillcolor;
  }
  /**
   * 创建GD句柄并设置坐标系
   */
  function create($x,$y) {
    $this-&gt;im = imagecreatetruecolor($x,$y);
imageantialias($this-&gt;im, 1);
imagefill($this-&gt;im, 0, 0, $this-&gt;color(0xffffff));
    $this-&gt;viewx = $x-1;
    $this-&gt;viewy = -($y-1);
    $this-&gt;winx = $x;
    $this-&gt;winy = $y;
    $this-&gt;orgx = 0;
    $this-&gt;orgy = 0;//$y;
    $this-&gt;colors = $this-&gt;color(0xffffff);

    settype($this-&gt;ksin,"double");
    settype($this-&gt;kcos,"double");
    $this-&gt;ksin = sin(deg2rad(0));
    $this-&gt;kcos = cos(deg2rad(0));
  }
  /**
   * 坐标映射
   */
  function get_view($x,$y) {
    $this-&gt;xo = $x*$this-&gt;kcos - $y*$this-&gt;ksin;
    $this-&gt;yo = $x*$this-&gt;ksin + $y*$this-&gt;kcos;
    $p['x'] = ($this-&gt;xo + $this-&gt;orgx)*($this-&gt;viewx/$this-&gt;winx);
    $p['y'] = ($this-&gt;yo + $this-&gt;orgy)*($this-&gt;viewy/$this-&gt;winy)-$this-&gt;viewy;
    return $p;
  }
  /**
   * 设置限度
   */
  function set_ext($x,$y=0) {
    $this-&gt;winx = $x;
    if($y == 0) {
        $this-&gt;winy = - $x * $this-&gt;viewy / $this-&gt;viewx;
    }else {
        $this-&gt;winy = $y;
    }
  }
  /**
   * 设置旋转角
   */
  function set_r($p) {
    $this-&gt;ksin = sin(deg2rad($p));
    $this-&gt;kcos = cos(deg2rad($p));
  }
  /**
   * 构造图形,必需在派生类中重载本方法
   */
  function paint() {
    /**
     * 必需由派生类重载
     * 必需包含语句 $this-&gt;create(宽度,高度);
     */
    die("paint 方法必需由派生类重载!");
  }
  /**
   * 输出图形
   */
  function run() {
    $this-&gt;paint();
    $func = "Image".$this-&gt;type;
    if(! function_exists($func)) {
        $this-&gt;type = "png";
        $func = "Image".$this-&gt;type;
    }
    Header("Content-type: image/{$this-&gt;type}");
    $func($this-&gt;im);
    imageDestroy($this-&gt;im);
  }

  /**
   * 圆类图形数据生成,参数$p为坐标旋转角
   */
  function get_point($ox,$oy,$w,$h,$start,$end) {
    $a = $w/2;
    $b = $h/2;
    $rs = array();
    $i = $start;
    for($i=$start;$iget_view($ox+$x,$oy+$y);
        $rs[] = $p['x'];
        $rs[] = $p['y'];
        if($i == $end) break;
        if($i+5 &gt; $end) $i = $end - 5;
    }
    $p = $this-&gt;get_view($ox,$oy);
    $rs[] = $p['x'];
    $rs[] = $p['y'];
    $rs[] = $rs[0];
    $rs[] = $rs[1];
    return $rs;
  }
  /**
   * 弓形
   */
  function chord($left,$top,$w,$h,$start,$end,$z=0) {
    $ar = $this-&gt;get_point($left,$top,$w,$h,$start,$end,$p);
    for($i=0;$i<count>get_view($ar[$i],$ar[$i+1]);
        $ar[$i] = $p['x'];
        $ar[$i+1] = $p['y'];
    }
    imagefilledpolygon($this-&gt;im,$ar,count($ar)/2-1,$this-&gt;fillcolor);
    imagepolygon($this-&gt;im,$ar,count($ar)/2-1,$this-&gt;color);
  }
  /**
   * 扇形
   */
  function pie($left,$top,$w,$h,$start,$end,$z=0) {
    $this-&gt;get_view($left,$top+$z);
    $ar = $this-&gt;get_point($left,$top+$z,$w,$h,$start,$end);
    $this-&gt;get_view($left,$top);
    $arh = $this-&gt;get_point($left,$top,$w,$h,$start,$end);
    for($j=0;$j<count array if>filltype)
            imagefilledpolygon($this-&gt;im,$t,4,$this-&gt;filldarkcolor);
        else
            imagepolygon($this-&gt;im,$t,4,$this-&gt;color);
    }
    if($this-&gt;filltype)
        imagefilledpolygon($this-&gt;im,$ar,count($ar)/2-1,$this-&gt;fillcolor);
  }
  /**
   * 弧形
   */
  function arc($left,$top,$w,$h,$start,$end,$z=0) {
    $ar = $this-&gt;get_point($left,$top,$w,$h,$start,$end,$p);
    $this-&gt;moveto($ar[0],$ar[1]);
    for($i=2;$i<count>lineto($ar[$i],$ar[$i+1]);
  }
}

/**
 * 重载基类的paint方法
 */
class myGraph extends Graph {
  function paint() {
//    $this-&gt;create(400,300);
    $this-&gt;create(200,150);
$this-&gt;set_ext(400);
    $this-&gt;set_r(10); // 旋转
    $this-&gt;filltype = false; //充填
    $k = 40;    //高
    $a =200;    //中心x
    $b = 150;    //中心y
    $c = 200;    //长轴
    $d = 100;    //短轴

    $this-&gt;color(0x000000);
    $this-&gt;color(0xff0000);
    $this-&gt;pie($a,$b,$c,$d,0,100,$k);
    $this-&gt;color(0x00ff00);
    $this-&gt;pie($a,$b,$c,$d,120,240,$k);
    $this-&gt;color(0x0000ff);
    $this-&gt;pie($a+10,$b-10,$c,$d,240,360,$k);
    $this-&gt;color(0x8080ff);
    $this-&gt;pie(50,80,40,20,0,360,50);
    $this-&gt;line($a,0,$a,$b*2);
    $this-&gt;line(0,$b,$a*2,$b);
  }
}

$g = new myGraph;
$g-&gt;run();
 <div class="clear"></div></count></count></count>
登录后复制
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号