2012-04-02 144 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