Search code examples
phpimage-rotationphp-gd

PHP GD - imagerotate for a jpeg image is not working


Title says JPEG. But I tried PNG. It didn't work. GD supports imagerotate function.

 if (function_exists('imagerotate')) { 
     echo "test";
 }

It outputs the word test. So i assume I have imagerotate function.

$im = imagecreatetruecolor($width + 10, $height + 10);
...

I did some image porcess. I can see the processed image without any problem. But i want to rotate the final image. So i did the following.

 imagerotate($im,180,0);
 imagepng($im,$png,9);
 imagedestroy($im);

But I am still getting the image without rotation. I even just tried to rotate a image without doing any process. It didn't work too.


Solution

  • You need to assign the rotated image to another variable before create the png.

    $rotatedImage = imagerotate($im,180,0);
     imagepng($rotatedImage,$png,9);
     imagedestroy($rotatedImage);