Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate image *without* resizing it

Tags:

php

I would like to rotate an image (with a user-defined angle of rotation), but without making the image smaller.

  1. is what happens already, saving the shaded area as a smaller image
  2. is what I would like, the dashed line being the new image size.

The server has PHP 5.3+. Links, code and explanations all very welcome.

like image 384
PaulAdamDavis Avatar asked Feb 28 '12 11:02

PaulAdamDavis


2 Answers

This is not complete answer but I would take the four corners as coordinates rotate them by your angle and then calculate the new bounding box based on the extent of the new coordinates. (assuming coordinates with origin on the bottom left).

corners = rotate_each ( [(left,top) (left,bottom), (right,top), (right,bottom)], angle)
new_bb_left = min([corners[0].x, corners[1].x, corners[2].x, corners[3].x])
new_bb_right = max([corners[0].x, corners[1].x, corners[2].x, corners[3].x])
new_bb_bottom = min([corners[0].y, corners[1].y, corners[2].y, corners[3].y])
new_bb_top = max([corners[0].y, corners[1].y, corners[2].y, corners[3].y])
like image 58
Charles Beattie Avatar answered Oct 07 '22 17:10

Charles Beattie


This could be a way to do it. Calculate the diagonal width.

Check this image for the formula

PHP has a Square root function: http://se2.php.net/manual/en/function.sqrt.php In that way you should have the diagonal width which you could apply on the transformed image.

like image 32
musse1 Avatar answered Oct 07 '22 17:10

musse1