Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend PDF: calculating coordinates after rotation

Without getting into too many details - I'm getting parameters (x1,x2,y1,y2,a,b,α) from a web tool and I need to generate a PDF document, by using Zend_PDF library which contains green image rotated and positioned properly on the exact coordinates.

enter image description here

Now, what confuses me is that Zend does not allow elements to be rotated, but instead rotates paper. So, I assume the rotation needs to be done like this

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

because we want the center of the image to be the rotation point, and we rotate it in the reverse orientation so the resulting image will get proper rotation.

The tricky part I'm having trouble with is drawing it. With the simple call

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

I'm getting the result as displayed on the diagram - the resulting image needs to be translated as well, since (x1,y1) and (x2,y2) are not exact coordinates anymore, but I'm not sure how to calculate them? Any ideas?

like image 668
Relja Avatar asked Oct 30 '22 11:10

Relja


1 Answers

The OP confirmed in a comment that he used the same values for (x1,y1) and (X2,y2) in his rotate and his drawImage calls. It is pretty obvious from his sketches, though, that the coordinates for the latter call must differ.

Fortunately we know from the way the green rectangle is inscribed in the rectangle from (x1,y1) to (X2,y2) that it has the same center point as that rectangle. Furthermore, we have the dimensions a and b of the green rectangle.

Thus, the drawImage parameters must 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);
like image 68
mkl Avatar answered Nov 15 '22 08:11

mkl