Zend PDF: calculating coordinates after rotation

Without going into too many details - I get the parameters (x1, x2, y1, y2, a, b, Ξ±) from the web tool, and I need to create a PDF document using the Zend_PDF library, which contains the green image rotated and correctly positioned on the exact coordinates.

enter image description here

What confuses me now is that Zend doesn't allow you to rotate elements, but instead rotates the paper. So, I assume that rotation should be done as follows

$page->rotate($x1 + ($x2 - $x1) / 2, $y1 + ($y2 - $y1) / 2, - deg2rad($rotation));

because we want the center of the image to be a pivot, and we rotate it in the opposite direction so that the resulting image gets the correct rotation.

The hard part that I'm having problems with is drawing it. With a simple call

$page->drawImage($image, $x1, $y1, $x2, $y2);

I get the result as shown in the diagram - the resulting image also needs to be translated, since (x1, y1) and (x2, y2) are no longer the exact coordinates, but I'm not sure how to calculate them? Any ideas?

+5
source share
1 answer

OP confirmed in a comment that he used the same values ​​for ( x1 , y1 ) and ( X2 , y2 ) in his rotate and his drawImage calls. However, his sketches show that the coordinates for the last call should be different.

Fortunately, we know how the green rectangle fits into the rectangle from ( x1 , y1 ) to ( X2 , y2 ), that it has the same center point as this rectangle. In addition, we have dimensions a and b of the green rectangle.

Thus, the drawImage parameters should be changed to:

 $page->drawImage($image, $x1 + ($x2 - $x1) / 2 - $a / 2 , $y1 + ($y2 - $y1) / 2 - $b / 2 , $x1 + ($x2 - $x1) / 2 + $a / 2 , $y1 + ($y2 - $y1) / 2 + $b / 2); 
+2
source

Source: https://habr.com/ru/post/1240103/


All Articles