note for this message : "I was having a problem with rotating a PNG. I used -1 for ignore_transparent and in Firefox the uncovered area was transparent but in IE it showed as grey.
The real problem is IE 6.0 does not support transparent PNGs. So don't blame the imagerotatefunction. I don't know about IE 7 though."
In fact, IE6 doesn't handle the transparency for 24 bits PNG images. But it does for 8 bits PNG. (This problem has been solved with IE7).
Meanwhile, even if the input image is a 8 bits one, the image returned by imagesrotate is a 24 bit one ... so IE6 wont correctly display it.

Just an advice for those who want to create image galleries and want to add a function to rotate pictures.
The way this here works is always to decompress the picture, rotate it and compress it again.
Therefore there _WILL_ always be a loss in quality. The more often you rotate the image the stronger the artefacts will be visible.
Also using ImageMagick, if available does not help, as it also does not support lossless JPG manipulations.
If you need a rotate function, ask your provider to install JPEGTRAN on the machine your server runs on and use the command line tool from your php application.

gareth

It's worth noting this function will only rotate true colour images - If you have a 256 colour pallet based image it will not rotate with this. Use the following code to convert the 256 colour image to truecolour first:
<?php
if (!imageistruecolor($im))
{
$w = imagesx($im);
$h = imagesy($im);
$t_im = imagecreatetruecolor($w,$h);
imagecopy($t_im,$im,0,0,0,0,$w,$h);
$im = $t_im;
}
?>
I have found no mention of this shortcoming of imagerotate anywhere on the Internet. Hopefully this will limit the number of people who, like me, are trying to troubleshoot a perfectly operation PHP install.

qrgames

imagerotate seems to be very fussy about handling transparency when copymerging onto another image. You can use the GD library's other transparency features to cover up the fact imagerotate gets it wrong HOWEVER it will only work if the top-left corner of the image is transparent at all rotations, so make the image a little bigger than it needs to be. This has been tested with png32 but does not work entirely for png8, as a phenomena creates noise around the rotated image.
<?php
$imgImage = imagecreatefrompng("image.png");
$colBlack = imagecolorallocate($imgImage, 0, 0, 0);
$imgImage = imagerotate($imgImage, 360 - $intHeading, 0);
imagefill($imgImage, 0, 0, $colBlack);
imagecolortransparent($imgImage, $colBlack);
imagecopymerge($imgOriginalImage , $imgImage, $intX, $intY, 0, 0, $intHeight, $intWidth, 100);
imagedestroy($imgImage);
?>
Note. $intHeading is in degrees clockwise :)

jon

imagerotate does not preserve the alpha channel, so if you want to rotate a PNG you need to get
creative. I don't see any command to retrieve the alpha information from an image (as far as
I could see,) so you'll have to do a bit of manual labor before hand. In my case I created a
second PNG file with the alpha saved as RGB data and manually "copied" the data from source
to destination:
function alpha_rotate($dst,$src,$rotate,$offsetX,$offsetY){
$top = imagecreatefrompng("image_processing/shadow.png");
$top_alpha = imagecreatefrompng("image_processing/shadow_alpha.png");

Imagerotate apparently destroy transparency information (transparent areas turn to black). For now the only walkaround I found is to use :
imagecolortransparent($image,imagecolorat($image,0,0));
but the result is quite awful if your original picture uses smooth transparency (which is probably the case with PNG pictures)

I was having a problem with rotating a PNG. I used -1 for ignore_transparent and in Firefox the uncovered area was transparent but in IE it showed as grey.
The real problem is IE 6.0 does not support transparent PNGs. So don't blame the imagerotatefunction. I don't know about IE 7 though.

olav-x

I tried the example provided by 'jonathan AT sharpmedia DOT net', and that gave me strange rotations, with offset on the image.
I dont need an advanced rotation script like that, so I used his function wrapper and make a more simple edition.
ps. the code above the function is only for test-purpose.
I'll retrieve srcpic from the db.
the idea here, is that I'll store rotated thumbs of the original thumb. Then the user can click on the thumb which is "correctly rotated" and then the original file will be rotated.
I think I'll make some backup function implemented too!
the idea is that the rotated files will have _ROT<int>.ext
eg. if you rotate the file "bicycle.jpg" 1 rotation (90 degrees), it will be copied to: bicycle_ROT1.jpg, 180 degrees would be _ROT2.jpg, and so on.
There is only 3 rots, I should have removed the ROT0 here, as well as the code might need improvement.
However, I think that simple codes like this ones, are the easiest ones to use as a foundation, as they are - simple!
What you can improve, if you want to do this:
* there is a lot! however, you have to wrap the $dst in something like:
if (!isset($dst)) {
}
eg. so you can over-ride the "save to", when calling the function!
this is beta 0.0000001 however, so play with it, slaughter it, whatever.
good luck!
Olav Alexander Mjelde
<?php
// #### start of test
$srcpic = 'bilder/biler/_tmp/foo.jpg';
echo rotateImage($srcpic, $dstpic, 1, 100);
echo rotateImage($srcpic, $dstpic, 2, 100);
echo rotateImage($srcpic, $dstpic, 3, 100);
// #### end of test
// this function rotates an image
function rotateImage($src, $dst, $count = 1, $quality = 95)
{
if (!file_exists($src)) {
return false;
}
// generate output filename
extension
$dst = substr($src, 0, strrpos($src, ".")) . "_ROT" . $count . substr($src, strrpos($src, "."), strlen($src));
switch ($count) {
case 0:
$degrees = 0;
break;
case 1:
$degrees = 90;
break;
case 2:
$degrees = 180;
break;
case 3:
$degrees = 270;
break;
}
// Load
$source = imagecreatefromjpeg($src);
// Rotate
$rotate = imagerotate($source, 360 - $degrees, 0);
// Output
imagejpeg($rotate, $dst, $quality);
imageDestroy($rotate);
imageDestroy($source);
return true;
}
?>

wulff

I liked the rotateImageBicubic function implemented by darren at lucidtone dot com. But it just snipped off the parts of the image that were outside the original image.
I fixed this, even though I admit that my solution is a bit naive. But it might come in handy for somebody.
Also his bicubic implementation was broken on my machine so I left it out, if you need it just copy and paste it from above.
<?php
// $src_img - a GD image resource
// $angle - degrees to rotate clockwise, in degrees
// returns a GD image resource
// USAGE:
// $im = imagecreatefrompng('test.png');
// $im = imagerotate($im, 15);
// header('Content-type: image/png');
// imagepng($im);
function imageRotate($src_img, $angle, $bicubic=false) {

Here's a neat function for those of us who don't have imagerotate() on our servers. It's based on a comment from ron at korving dot demon dot nl on the manual page for imagecopyresampled.
I'm still not 100% on coping with transparency, but this function seems to cope okay. It doesn't resize to fit within bounds, it just rotates and you lose anything outside the image box.
The bicubic mode is slooow.
If you want to be able to change the background colour, pass in a colour and use it where indicated. The line I used just sets it transparent.
<?
// $src_img - a GD image resource
// $angle - degrees to rotate clockwise, in degrees
// returns a GD image resource
// USAGE:
// $im = imagecreatefrompng('test.png');
// $im = imagerotate($im, 15);
// header('Content-type: image/png');
// imagepng($im);
function imageRotateBicubic($src_img, $angle, $bicubic=false) {