2013-02-15 66 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也是从弧度到度的转换。