0

I am using imagerotate() function to turn the image below to a 30 degree angle.

http://postimage.org/image/b7w6dacel/

The problem is that whenever I rotate the image and save it to a new path, in the new path the image is rotated but it has a black color surrounding it, like below.

http://postimage.org/image/n3n1vtr9p/8501961e/

The original image is 450 * 340 and after rotation, the image becomes 560 * 520. Does anyone know what I am doing wrong. Take a look at my source code below. Thanks.

$filename =  "static\\". $sessionid . "-1.jpg";
$file= $sessionid . "-1.jpg";
$ir = imagecreatefromjpeg($filename);
$degrees = 30;
$flip=imagerotate($ir, $degrees, 0);
$rotated=imagejpeg($flip, 'c:\\xampp\\htdocs\\'. $file);
user875139
  • 1,599
  • 4
  • 23
  • 44
  • This looks like expected behaviour to me. What did you expect would happen? – Halcyon Apr 21 '12 at 18:08
  • i wanted to get the image rotated without an increase in dimension. Because the rotate image looks like a completely new image with the black around it. – user875139 Apr 21 '12 at 18:09
  • So crop it? Keep in mind that the resulting will be smaller than the original unless you want to allow for some 'background' to be shown. – Halcyon Apr 21 '12 at 18:21
  • 1
    The bounding box around a rectangular image rotated through zero degrees is the same size as the original - however the bounding box around a rectangular image rotated through a value that isn't a multiple of 180deg (or 90deg for square images) will always be larger. – halfer Apr 21 '12 at 18:45
  • 1
    Also, don't forget that you can render this to a PNG, and set the background to transparent. – halfer Apr 21 '12 at 18:47

1 Answers1

2

Depending on how much you rotate it, the new image will be larger than the original. You will either have to scale or crop the new image. This code will scale $oldImage to $newWidth and $newHeigth.

$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($oldImage), imagesy($oldImage));

Hope this helps.

Joel
  • 2,654
  • 6
  • 31
  • 46