php - 用于 PHP GD 图的平滑起伏不定的图像线

标签 php graph gd

我无法为该图获得平滑的线条。我尝试过抗锯齿、增加图像,我在 php.net 上发现了一些平滑功能,但结果并不一致,有时会导致线条本身不连贯。

我更新了下面的代码并显示/评论了用于解决问题的更改

代码:

<?php
header("Content-type: image/png");

$Values=array(rand(40,80),rand(40,100),rand(10,50),rand(80,160),rand(30,100),rand(40,120),rand(280,360),rand(20,80),rand(10,80),rand(40,120),rand(180,260),rand(40,160),rand(550,700),rand(480,600),rand(240,340),rand(480,600),rand(240,340));
$imgWidth=500;
$imgHeight=200;
$grid=25;
$graphspacing=0.05;

while (list($key, $val) = each($Values)) {
    if($val>$max){
        $max=$val;
    }
}

for ($i=0; $i<count($Values); $i++){
    $graphValues[$i] = $Values[$i] * (($imgHeight*(1-$graphspacing))/$max);
}

// use imagecreatetruecolor instead of imagecreate
$image=imagecreatetruecolor($imgWidth, $imgHeight);

// added antialiasing
imageantialias($image, true);

// had to force a white bg
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image , 0,0 , $bgColor);

$colorWhite=imagecolorallocate($image, 255, 255, 255);
$colorGrey=imagecolorallocate($image, 192, 192, 192);
$colorBlue=imagecolorallocate($image, 0, 0, 255);

imageline($image, 0, 0, 0, $imgHeight, $colorGrey);
imageline($image, 0, 0, $imgWidth, 0, $colorGrey);
imageline($image, $imgWidth-1, 0, $imgWidth-1, $imgHeight-1, $colorGrey);
imageline($image, 0, $imgHeight-1, $imgWidth-1, $imgHeight-1, $colorGrey);

// Create grid
for ($i=1; $i<($imgWidth/$grid); $i++) {
    imageline($image, $i*$grid, 0, $i*$grid, $imgHeight, $colorGrey);
}
for ($i=1; $i<($imgHeight/$grid); $i++) {
    imageline($image, 0, $i*$grid, $imgWidth, $i*$grid, $colorGrey);
}

if($imgWidth/$grid>count($graphValues)){ 
    $space=$grid; 
} else { 
    $space = $imgWidth/(count($graphValues)-1);
}

for ($i=0; $i<count($graphValues)-1; $i++) {
    imageline($image, $i*$space, ($imgHeight-$graphValues[$i]), ($i+1)*$space, ($imgHeight-$graphValues[$i+1]), $colorBlue);
}

//添加平滑过滤器 imagefilter($image, IMG_FILTER_SMOOTH, 15);

imagepng($image);//,NULL,9);
imagedestroy($image);

?>

结果是(我得到的最好结果):

之前

enter image description here

之后

enter image description here

Cheers.Bo

最佳答案

您正在使用 imagecreate 我建议使用 imagecreatetruecolor 然后将 imageantialias 添加到该图像....(看不到任何 imageantialias在你的代码中)

总有不同

我建议您查看 http://php.net/manual/en/function.imageantialias.php 中的示例

希望对你有帮助

谢谢

:)

关于php - 用于 PHP GD 图的平滑起伏不定的图像线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9982791/

相关文章:

php - 使用 php (jpeg) 优化上传的图像

php - 将参数从 PHP 传递到远程 shell 脚本

algorithm - 如何检查依赖图中的额外冲突信息?

algorithm - 查找距给定区号最近的配送中心

Common Lisp 绘制所有子节点的图形

perl - 使用 Cygwin 的 CPAN GD 模块安装失败

php - 广东!将 png 图像转换为 jpeg 并使 alpha 默认为白色而不是黑色

php - 如何通过 PHP 在文档中设置 cookie?

php - 正则表达式匹配任何字符串,无论是否为 Unicode?

php - laravel 如何将关系转移到数组