PHP GD how to draw text line by line

The end result should look like the image (HELLO WORLD): Ignore the line thickness Please ..

That's what I'm doing: -

$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 angle 

And the current output is as follows:

Current output

Can anyone tell what happened to my code?

thanks

+7
source share
3 answers

Good, played. Try replacing:

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

FROM

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

Output with your values:

enter image description here

Output with other values:

enter image description here

+1
source

This is not what you might be looking for, but I believe it will help you.

 $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 angle header('Content-Type: image/png'); imagepng($im); imagedestroy($im); 
Values

hardcoded. Here the angle is set as -45 and for this angle, if you want the text to appear above the line, you need to reduce the value from the starting position Y of your line. And you have to write code to calculate the angle with the line x1, x2y1, y2

+1
source

2 thoughts can't test right now.

  • Where 0 (x = 1, y = 0) or (x = 0, y = 1). It seems your text is turned off 90 degrees, which usually means that your assumption 0 is straightforward when it's possible straight up or vice versa.

  • (rad2deg(atan2($delta_y,$delta_x)) * 180 / M_PI)-360; Are you doing double work here? rad2deg converted from radians to degrees. 180 / M_PI also a conversion from radians to degrees.

+1
source

All Articles