2012-04-02 142 views
3

我无法获得此图形的平滑线条。我尝试了抗锯齿,增加了图像,并在php.net上找到了一些平滑功能,但结果并不一致,有时会导致锯齿线条本身。用于PHP GD图形的平滑波纹图像

我更新下面的代码,并显示/ COMMENTED用于固定ISSUE

代码中的改变:

<?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($图像,IMG_FILTER_SMOOTH,15 );

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

?> 

结果是(最好的,我还没有得到):后

enter image description here

enter image description here

eers.Bo

+0

只要有一个工作示例,init'$ max = 0'就是mssing。 – MaxD 2013-10-22 20:06:31

回答

3

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

总是有一个总是差

我会建议你看看例子在http://php.net/manual/en/function.imageantialias.php

我希望这有助于

感谢

:)

+0

我认为这是我忽略的一个微妙的区别..虐待它! – roberthuttinger 2012-04-02 20:48:08

+0

酷..我希望我能够帮助 – Baba 2012-04-02 21:00:47

+0

我改变了上面的代码来显示工作修复。事实之后,我添加了一个SMOOTH过滤器,使它更好。 – roberthuttinger 2012-04-03 13:26:59