2013-02-15 64 views
7

最終的輸出應該是這樣的圖像(HELLO WORLD)繪製文本: Ignore the line thickness Please..PHP GD如何通過線

下面是我在做什麼: -

$im = imagecreate(400,400); 

$txtcol = imagecolorallocate($im, 0xFF, 0x00, 0x00); 

$size = 20; 

$txt = 'DUMMY TEXT'; 

$font = './font/Capriola-Regular.ttf'; 

/*two points for base line*/ 

$x1 = 50; $x2 = 300; 

$y1 = 150; $y2 = 170; 

/*bof finding line angle*/ 

$delta_x = $x2-$x1; 

$delta_y = $y2-$y1; 

$texangle = (rad2deg(atan2($delta_y,$delta_x)) * 180/M_PI)-360; 

/*eof finding the line angle*/ 


imageline($im,$x1,$y1,$x2,$y2,$txtcol); //Drawing line 

imagettftext($im, $size, $texangle, $x1, $y1, $txtcol, $font, $txt); // Drawing text over line at line's angle 

和電流輸出像:

Current output

任何人能告訴什麼錯我的代碼?

感謝

+0

'imagettftext'中定義了$ ty嗎? – webnoob 2013-02-15 12:52:19

+0

對不起,不是$ ty其$ y1 @webnoob感謝通知我:) – ROBIN 2013-02-15 13:15:32

+0

我認爲問題是找到線角度。你認爲@webnoob – ROBIN 2013-02-15 13:23:53

回答

1

好吧,一直在玩。嘗試更換:

$texangle = (rad2deg(atan2($delta_y,$delta_x)) * 180/M_PI)-360; 

有了:

$texangle = (atan2($delta_y,$delta_x) * -180/M_PI)-360; 

輸出與你的價值觀:

enter image description here

輸出與其它值:

enter image description here

1

這不是你可能會尋找確切的事情,但我相信它會幫助你

$im = imagecreate(400,400); 

$txtcol = imagecolorallocate($im, 0xFF, 0xFF, 0x00); 

$size = 20; 

$txt = 'DUMMY TEXT'; 

$font = 'Capriola-Regular.ttf'; 

/*two points for base line*/ 

$x1 = 70; $x2 = 170; 

$y1 = 140; $y2 = 240; 

/*bof finding line angle*/ 

$delta_x = $x2-$x1; 

$delta_y = $y2-$y1; 

$texangle = (rad2deg(atan2($delta_y,$delta_x)) * 180/M_PI)-360; 

/*eof finding the line angle*/ 


imageline($im,$x1,$y1,$x2,$y2,'0xDD'); //Drawing line 
imagettftext($im, $size, -45, $x1, $y1-10, '0xDD', $font, $txt); // Drawing text over line at line's angle 

header('Content-Type: image/png'); 

imagepng($im); 
imagedestroy($im); 

值是硬編碼。 這裏的角度是-45,如果你希望你的文本出現在行的上方,那麼你必須減少你的行的Y位置的初始值。 你將不得不編寫代碼來計算線的角度x1,x2y1,y2

+0

多數民衆贊成有效。起初,我正在測試,所以我採取硬編碼值。現在的問題是,如果我們動態地提供Line的座標,那麼我們將如何找到imagettftext的角度和Y的降低值? – ROBIN 2013-02-16 05:18:00

+0

@Robin:我認爲webnoob的解決方案應該可以工作。我還沒有測試過它 – zamil 2013-02-16 08:26:59

1

2想法,現在不能測試。

  1. 其中是0(x = 1,y = 0)或(x = 0,y = 1)。看起來你的文字已經偏離90度 度,這通常意味着你的假設0直接是正確的, 可能是直接上升,反之亦然。

  2. (rad2deg(atan2($delta_y,$delta_x)) * 180/M_PI)-360;你在這裏做雙工嗎? rad2deg將弧度轉換爲度數。 180/M_PI也是從弧度到度的轉換。