Reputation: 771
I would like to draw a rectangle at an angle with PHP. I know that you can draw rectangles with PHP using imagefilledrectangle
but how to draw it an an angle.
public function drawshelf($x1, $y1, $x2, $y2, $width, $angle = 'false'){
imagesetthickness ( $this->canvas, 1 );
for ($i=0; $i < $width; $i++){ //HORIZONTAL
imageline( $this->canvas, $x1, $y1, $x2, $y2, $this->color );
$y1++; $y2++;
if( $angle == 'true' ){ $x1--; $x2--; }
}
}
I wrote this function to draw it using lines and a loop but its not coming up right like the red box.
Can someone please tell me what am i doing wrong? And can you even draw it like that?
Upvotes: 1
Views: 3781
Reputation: 2159
I'd suggest using the built in imagerotate along with a rectangle you've created with imagefilledrectangle
.
Here's an example, creating a 20x100 red rectangle rotated 30 degrees:
$width = 20;
$height = 100;
$angle = 30;
$im = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);
// Draw a red rectangle
imagefilledrectangle($im, 0, 0, $width, $height, $red);
// Rotate and fill out background with white
$im = imagerotate($im, $angle, $white);
Upvotes: 1
Reputation:
Use imagepolygon()
or imagefilledpolygon()
to draw non-rectangular shapes using GD. You may need to review a bit of trigonometry to figure out how to position the points to get right-angle corners.
Upvotes: 6