Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: imagerotate - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imagesavealpha" width="11" height="7"/> <imagerectangle
Last updated: Sun, 23 Sep 2007

view this page in

imagerotate

(PHP 4 >= 4.3.0, PHP 5)

imagerotate — 指定された角度で画像を回転する

説明

resource imagerotate ( resource $source_image, float $angle, int $bgd_color [, int $ignore_transparent] )

画像 source_image を、 angle で指定された角度だけ回転します。

回転の中心は画像の中心です。回転された画像はスケールダウンされるので、 回転された画像全体は対象画像にフィットします。縁はクリップされません。

パラメータ

source_image

元画像のリンク。

angle

回転角度。

bgd_color

回転後、カバーされない部分の色。

ignore_transparent

ゼロ以外を指定すると、透過色は無視されます (その他の場合は保持されます)。

返り値

変更履歴

バージョン説明
5.1.0 ignore_transparent が追加されました。

例 1011. 画像を 180 度回転する

この例は、画像を 180 度 - 上下逆さまに回転します

<?php
// ファイルと回転角
$filename = 'test.jpg';
$degrees = 180;

// コンテントタイプ
header('Content-type: image/jpeg');

// 読み込み
$source = imagecreatefromjpeg($filename);

// 回転
$rotate = imagerotate($source, $degrees, 0);

// 出力
imagejpeg($rotate);
?>

上の例の出力は、たとえば 以下のようになります。


注意

注意: この関数は、PHP がバンドル版の GD ライブラリで コンパイルされている場合のみ使用可能です。



imagesavealpha" width="11" height="7"/> <imagerectangle
Last updated: Sun, 23 Sep 2007
 
add a note add a note User Contributed Notes
imagerotate
qrgames at NOSPAMTYrushyo dot com
12-Aug-2007 08:06
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 :)
Thomaschaaf
24-Feb-2007 12:21
function rotate($degrees)
    {
        if(function_exists("imagerotate"))
            $this->image = imagerotate($this->image, $degrees, 0);
        else
        {
            function imagerotate($src_img, $angle)
            {
                $src_x = imagesx($src_img);
                $src_y = imagesy($src_img);
                if ($angle == 180)
                {
                    $dest_x = $src_x;
                    $dest_y = $src_y;
                }
                elseif ($src_x <= $src_y)
                {
                    $dest_x = $src_y;
                    $dest_y = $src_x;
                }
                elseif ($src_x >= $src_y) 
                {
                    $dest_x = $src_y;
                    $dest_y = $src_x;
                }
               
                $rotate=imagecreatetruecolor($dest_x,$dest_y);
                imagealphablending($rotate, false);
               
                switch ($angle)
                {
                    case 270:
                        for ($y = 0; $y < ($src_y); $y++)
                        {
                            for ($x = 0; $x < ($src_x); $x++)
                            {
                                $color = imagecolorat($src_img, $x, $y);
                                imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
                            }
                        }
                        break;
                    case 90:
                        for ($y = 0; $y < ($src_y); $y++)
                        {
                            for ($x = 0; $x < ($src_x); $x++)
                            {
                                $color = imagecolorat($src_img, $x, $y);
                                imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
                            }
                        }
                        break;
                    case 180:
                        for ($y = 0; $y < ($src_y); $y++)
                        {
                            for ($x = 0; $x < ($src_x); $x++)
                            {
                                $color = imagecolorat($src_img, $x, $y);
                                imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
                            }
                        }
                        break;
                    default: $rotate = $src_img;
                };
                return $rotate;
            }
            $this->image = imagerotate($this->image, $degrees);
        }
    }

This one works better than the one below from the 12th February and if the faster function is available it will take that.
It is still not optimized but turning pictures works better..
crymsan
13-Feb-2007 07:44
My php install does not have imagerotate (ubuntu). So, after fiddling with a few others I wrote my own code to rotate images in 90 degree increments. Any speed improvements would be great.

<?php
function imageRotate($src_img, $angle) {

   
$src_x = imagesx($src_img);
   
$src_y = imagesy($src_img);
    if (
$angle == 90 || $angle == -910) {
       
$dest_x = $src_y;
       
$dest_y = $src_x;
    } else {
       
$dest_x = $src_x;
       
$dest_y = $src_y;
    }

   
$rotate=imagecreatetruecolor($dest_x,$dest_y);
   
imagealphablending($rotate, false);

    switch (
$angle) {
        case
90:
            for (
$y = 0; $y < ($src_y); $y++) {
                for (
$x = 0; $x < ($src_x); $x++) {
                   
$color = imagecolorat($src_img, $x, $y);
                   
imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
                }
            }
            break;
        case -
90:
            for (
$y = 0; $y < ($src_y); $y++) {
                for (
$x = 0; $x < ($src_x); $x++) {
                   
$color = imagecolorat($src_img, $x, $y);
                   
imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
                }
            }
            break;
        case
180:
            for (
$y = 0; $y < ($src_y); $y++) {
                for (
$x = 0; $x < ($src_x); $x++) {
                   
$color = imagecolorat($src_img, $x, $y);
                   
imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
                }
            }
            break;
        default:
$rotate = $src_img;
    };
    return
$rotate;
}
?>
cedric dot francoys at easy-cms dot org
08-Jan-2007 03:26
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.
http://whitemarker.blogspot.com
04-Jan-2007 08:06
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.
m dot quinton at gmail dot com
15-Sep-2006 01:30
with large file, where imagerotate is missing, you can use, when possible "convert" command from ImageMagick. Here is a sample script.

<?php

error_reporting
(E_ALL);

header("Content-type: image/png");

$file = 'images/test/imgp2498.jpg';

image_rotate_with_convert($file, 90);

function
image_rotate_with_convert($file, $angle){
   
passthru("convert -rotate $angle $file -");
}

?>
barbarism at oscillatewildly dot com
18-Apr-2006 11:00
If you pass "-1" into the last param of imagecreatefrompng, it preserves PNG transparently properly!

Exampe:

    $filename = './documents/135.pdf.png';
    $degrees = 40;

    header('Content-type: image/png');

    $source = imagecreatefrompng($filename);

    $rotate = imagerotate($source, $degrees, -1);

    imagealphablending($rotate, true);
    imagesavealpha($rotate, true);

    imagepng($rotate);
Longhair
14-Apr-2006 06:03
<?

//KOMPASS //PHP4.0 //GD2

$wert=0; //0-360 degree
$null=46; //Offset
$bgcolor=0x0033CC;
$grad=$wert+$null;
$kompass=imagecreatefromjpeg("bilder/kompass_ohne.jpg");
$nadel=imagecreatefromjpeg("bilder/nadel_bleu.jpg");

$mov_nadel= imagerotate($nadel, $grad, $bgcolor);

$xKom=imagesx($kompass);
$yKom=imagesy($kompass);

$x2Kom=$xKom/2;
$y2Kom=$yKom/2;

$xNad=imagesy($mov_nadel);
$yNad=imagesy($mov_nadel);

$y2Nad=$yNad/2+$x2Kom-$yNad; //92/2=46 + 100-92
$x2Nad=$xNad/2+$y2Kom-$xNad;

imagecopy ( $kompass, $mov_nadel, $x2Nad, $y2Nad, 0, 0, $xNad, $xNad );

header("Content-type: image/jpg");
//wird als JEPG-Bild Ausgegeben
//imagepng($kompass);
imagejpeg($kompass);
//imagegif($nadel);
imagedestroy($nadel);
imagedestroy($kompass);

?>
gareth at crxchaos dot co dot uk
03-Mar-2006 12:19
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.
olav-x at volvo-power.net
18-Nov-2005 09:17
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;
}

?>
simon_nuttall at hotmail dot com
28-Sep-2005 02:06
The following is potentially useful. It extracts the central largest circle of an image into a square of specified size, and optionally rotates it. The rest of the square is made transparent, so useful for drawing over other images. I've named it after binocular effect because on some old TV shows whenever they show someone looking through binoculars the screen shows a big circular image with black edges.

<?php

function image_binocular_effect($src, $bearing, $out_square) {
 
// the source image is resampled to fit within the specified square, and rotated clockwise by bearing.
 // the largest circle within the image is retained, the rest made transparent.
 
$out = imagecreatetruecolor($out_square, $out_square);
 
$width=imagesx($src);
 
$height=imagesy($src);
 
$square=min($width, $height);
 
imagecopyresampled($out, $src, 0, 0, ($width - $square)/2 , ($height - $square)/2, $out_square, $out_square, $square, $square);

 
$mask = imagecreatetruecolor($out_square, $out_square);
 
$black = ImageColorAllocate ($mask, 0, 0, 0);
 
$white = ImageColorAllocate ($mask, 255, 255, 255);
 
imagefilledrectangle($mask , 0, 0, $out_square, $out_square, $white);
 
$centrexy=$out_square / 2;
 
imagefilledellipse($mask, $centrexy, $centrexy, $out_square, $out_square, $black);
 
ImageColorTransparent($mask, $black);
 
imagecopymerge($out, $mask0, 0, 0, 0, $out_square, $out_square, 100);
 if (
$bearing != 0) {
 
$rotated_img=imagerotate($out , 360-$bearing, $white);
 
// take off only the rotated width
 
$rotated_map_width = imagesx($rotated_img);
 
$rotated_map_height = imagesy($rotated_img);
 
imagecopy($out, $rotated_img, 0, 0, ($rotated_map_width - $out_square) / 2, ($rotated_map_height - $out_square) / 2, $out_square, $out_square);
  }
 
ImageColorTransparent($out, $white);
 return
$out;
}

// Create a sample image to demonstrate the effect, but looks much better on real photos.

$src = imagecreatetruecolor(200, 50);
imagefilledrectangle($src, 0, 0, 200, 50, imagecolorallocate($src, 255, 255, 255));
ImageString($src, 3, 10, 10, "This is a sample image to illustrate the binocular effect", imagecolorallocate($im, 192, 0, 0));
$img=image_binocular_effect($src, 72, 50);
ImagePNG($img,"test.png");

?>
kumm at webstar dot hu
22-Aug-2005 04:59
A better bugfix for jonathan AT sharpmedia DOT net's rotateImage function

you need to replace:

// Now copy tmp2 to $out;
imagecopy($out, $tmp2, 0, 0, ($angle == 270 ? abs($w - $h) : 0), 0, $h, $w);

with:
// Now copy tmp2 to $out;
if ($h>$w) {
   imagecopy($out, $tmp2, 0, 0, 0, ($angle == 90 ? abs($w - $h) : 0), $h, $w);
} else {
   imagecopy($out, $tmp2, 0, 0, ($angle == 270 ? abs($w - $h) : 0), 0, $h, $w);
}

otherwise the image gets moved when rotating a standing up image
jmichel at faeryscape dot com
27-Apr-2005 03:56
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)
oflashp at bk dot ru
22-Apr-2005 10:25
standart code rotate image(only Jpeg)
<?
$r
=0; //rotate
$img="254.jpg" //image
$source = imagecreatefromjpeg($img);
$img = imagerotate($source, $r, 0);
imagejpeg($img);
?>
12-Apr-2005 01:58
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.
wulff at fyens dot dk
02-Mar-2005 06:22
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) {
 
  
// convert degrees to radians
  
$angle = $angle + 180;
  
$angle = deg2rad($angle);
 
  
$src_x = imagesx($src_img);
  
$src_y = imagesy($src_img);
 
  
$center_x = floor($src_x/2);
  
$center_y = floor($src_y/2);

  
$cosangle = cos($angle);
  
$sinangle = sin($angle);

  
$corners=array(array(0,0), array($src_x,0), array($src_x,$src_y), array(0,$src_y));

   foreach(
$corners as $key=>$value) {
    
$value[0]-=$center_x;        //Translate coords to center for rotation
    
$value[1]-=$center_y;
    
$temp=array();
    
$temp[0]=$value[0]*$cosangle+$value[1]*$sinangle;
    
$temp[1]=$value[1]*$cosangle-$value[0]*$sinangle;
    
$corners[$key]=$temp;   
   }
  
  
$min_x=1000000000000000;
  
$max_x=-1000000000000000;
  
$min_y=1000000000000000;
  
$max_y=-1000000000000000;
  
   foreach(
$corners as $key => $value) {
     if(
$value[0]<$min_x)
      
$min_x=$value[0];
     if(
$value[0]>$max_x)
      
$max_x=$value[0];
  
     if(
$value[1]<$min_y)
      
$min_y=$value[1];
     if(
$value[1]>$max_y)
      
$max_y=$value[1];
   }

  
$rotate_width=round($max_x-$min_x);
  
$rotate_height=round($max_y-$min_y);

  
$rotate=imagecreatetruecolor($rotate_width,$rotate_height);
  
imagealphablending($rotate, false);
  
imagesavealpha($rotate, true);

  
//Reset center to center of our image
  
$newcenter_x = ($rotate_width)/2;
  
$newcenter_y = ($rotate_height)/2;

   for (
$y = 0; $y < ($rotate_height); $y++) {
     for (
$x = 0; $x < ($rotate_width); $x++) {
      
// rotate...
      
$old_x = round((($newcenter_x-$x) * $cosangle + ($newcenter_y-$y) * $sinangle))
         +
$center_x;
      
$old_y = round((($newcenter_y-$y) * $cosangle - ($newcenter_x-$x) * $sinangle))
         +
$center_y;
     
       if (
$old_x >= 0 && $old_x < $src_x
            
&& $old_y >= 0 && $old_y < $src_y ) {

          
$color = imagecolorat($src_img, $old_x, $old_y);
       } else {
        
// this line sets the background colour
        
$color = imagecolorallocatealpha($src_img, 255, 255, 255, 127);
       }
      
imagesetpixel($rotate, $x, $y, $color);
     }
   }
  
  return(
$rotate);
}

?>
jonathan AT sharpmedia DOT net
18-Feb-2005 03:28
This method rotates an image in 90 degree increments (eg count should be between 1 and 3) and avoids the problems of image scaling that imageRotate has...

<?php
function rotateImage($src, $count = 1, $quality = 95)
{
    if (!
file_exists($src)) {
        return
false;
    }

    list(
$w, $h) = getimagesize($src);

    if ((
$in = imageCreateFromJpeg($src)) === false) {
        echo
"Failed create from source<br>";
        return
false;
    }

   
$angle = 360 - ((($count > 0 && $count < 4) ? $count : 0 ) * 90);

    if (
$w == $h || $angle == 180) {
       
$out = imageRotate($in, $angle, 0);
    } elseif (
$angle == 90 || $angle == 270) {
       
$size = ($w > $h ? $w : $h);
       
// Create a square image the size of the largest side of our src image
       
if (($tmp = imageCreateTrueColor($size, $size)) == false) {
            echo
"Failed create square trueColor<br>";
            return
false;
        }

       
// Exchange sides
       
if (($out = imageCreateTrueColor($h, $w)) == false) {
            echo
"Failed create trueColor<br>";
            return
false;
        }

       
// Now copy our src image to tmp where we will rotate and then copy that to $out
       
imageCopy($tmp, $in, 0, 0, 0, 0, $w, $h);
       
$tmp2 = imageRotate($tmp, $angle, 0);

       
// Now copy tmp2 to $out;
       
imageCopy($out, $tmp2, 0, 0, ($angle == 270 ? abs($w - $h) : 0), 0, $h, $w);
       
imageDestroy($tmp);
       
imageDestroy($tmp2);
    } elseif (
$angle == 360) {
       
imageDestroy($in);
        return
true;
    }

   
imageJpeg($out, $src, $quality);
   
imageDestroy($in);
   
imageDestroy($out);
    return
true;
}
?>
Borszczuk
05-Jan-2005 10:21
Here's a function that implements right angle (multiplicity of 90 degs - 90, 180, 270) rotation if you need one but lacks native imagerotate() or you don't want non-square images to be scaled down as with imagerotate(). As you probably noticed it's not self contained function, as 180 rotation is handled by ImageFlip() function to gain the performance. The ImageFlip() function used is published here: http://php.net/imagecopy in the comment of mine placed on  05-Jan-2005 04:30

Please note: that in case of 0 degrees rotation handle to imgSrc is returned which may lead to problems if you imagedestroy() it undonditionaly. To solve that you shall add imagecopy($imgDest, $imgSrc, 0,0, 0,0,$srcX, $srcY)  in proper place which I have intentionally ommited to save memory resources

<?php

// $imgSrc - GD image handle of source image
// $angle - angle of rotation. Needs to be positive integer
// angle shall be 0,90,180,270, but if you give other it
// will be rouned to nearest right angle (i.e. 52->90 degs,
// 96->90 degs)
// returns GD image handle of rotated image.
function ImageRotateRightAngle( $imgSrc, $angle )
{
   
// ensuring we got really RightAngle (if not we choose the closest one)
   
$angle = min( ( (int)(($angle+45) / 90) * 90), 270 );

   
// no need to fight
   
if( $angle == 0 )
        return(
$imgSrc );

   
// dimenstion of source image
   
$srcX = imagesx( $imgSrc );
   
$srcY = imagesy( $imgSrc );

    switch(
$angle )
        {
        case
90:
           
$imgDest = imagecreatetruecolor( $srcY, $srcX );
            for(
$x=0; $x<$srcX; $x++ )
                for(
$y=0; $y<$srcY; $y++ )
                   
imagecopy($imgDest, $imgSrc, $srcY-$y-1, $x, $x, $y, 1, 1);
            break;

        case
180:
           
$imgDest = ImageFlip( $imgSrc, IMAGE_FLIP_BOTH );
            break;

        case
270:
           
$imgDest = imagecreatetruecolor( $srcY, $srcX );
            for(
$x=0; $x<$srcX; $x++ )
                for(
$y=0; $y<$srcY; $y++ )
                   
imagecopy($imgDest, $imgSrc, $y, $srcX-$x-1, $x, $y, 1, 1);
            break;
        }

    return(
$imgDest );
}
?>
darren at lucidtone dot com
08-Dec-2004 01:43
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) {
   
   
// convert degrees to radians
   
$angle = $angle + 180;
   
$angle = deg2rad($angle);
   
   
$src_x = imagesx($src_img);
   
$src_y = imagesy($src_img);
   
   
$center_x = floor($src_x/2);
   
$center_y = floor($src_y/2);
   
   
$rotate = imagecreatetruecolor($src_x, $src_y);
   
imagealphablending($rotate, false);
   
imagesavealpha($rotate, true);

   
$cosangle = cos($angle);
   
$sinangle = sin($angle);
   
    for (
$y = 0; $y < $src_y; $y++) {
      for (
$x = 0; $x < $src_x; $x++) {
   
// rotate...
   
$old_x = (($center_x-$x) * $cosangle + ($center_y-$y) * $sinangle)
      +
$center_x;
   
$old_y = (($center_y-$y) * $cosangle - ($center_x-$x) * $sinangle)
      +
$center_y;
   
    if (
$old_x >= 0 && $old_x < $src_x
        
&& $old_y >= 0 && $old_y < $src_y ) {
      if (
$bicubic == true) {
       
$sY  = $old_y + 1;
       
$siY  = $old_y;
       
$siY2 = $old_y - 1;
       
$sX  = $old_x + 1;
       
$siX  = $old_x;
       
$siX2 = $old_x - 1;
       
       
$c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
       
$c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
       
$c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
       
$c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));
       
       
$r = ($c1['red']  + $c2['red']  + $c3['red']  + $c4['red']  ) << 14;
       
$g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
       
$b = ($c1['blue']  + $c2['blue']  + $c3['blue']  + $c4['blue'] ) >> 2;
       
$a = ($c1['alpha']  + $c2['alpha']  + $c3['alpha']  + $c4['alpha'] ) >> 2;
       
$color = imagecolorallocatealpha($src_img, $r,$g,$b,$a);
      } else {
       
$color = imagecolorat($src_img, $old_x, $old_y);
      }
    } else {
         
// this line sets the background colour
     
$color = imagecolorallocatealpha($src_img, 255, 255, 255, 127);
    }
   
imagesetpixel($rotate, $x, $y, $color);
      }
    }
    return
$rotate;
}
?>
jon at driestone dot com
08-Oct-2004 12:42
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");
               
        imagecopyresampled($top,$src,0,0,0,0,100,100,100,100);
               
        $top = imagerotate($top,$rotate,0x000000);
        $top_alpha = imagerotate($top_alpha,$rotate,0x000000);
   
   
        for ($theX=0;$theX<imagesx($top);$theX++){
            for ($theY=0;$theY<imagesy($top);$theY++){
   
                $rgb = imagecolorat($top,$theX,$theY);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;
               
                $rgb = imagecolorat($top_alpha,$theX,$theY);
                $a = $rgb & 0xFF;
                $a = 127-floor($a/2);

                $myColor = imagecolorallocatealpha($top,$r,$g,$b,$a);
                imagesetpixel($dst,($theX+$offsetX),($theY+$offsetY),$myColor);   
            }
        }
    }
christoph (at) raketenbasis (dot) de
04-Oct-2004 04:41
The default direction of imageRotate() is counter clockwise. Heres a little function which solves the problem.

<?php

   
function rotate(&$image_source, $rotate_value, $rotate_clockwise = true) {
        if(
$rotate_clockwise == true) {
           
$rotate_value = 360 - $rotate_value;
        }
       
$image_source = imageRotate($image_source, $rotate_value, 0);
    }
   
?>

imagesavealpha" width="11" height="7"/> <imagerectangle
Last updated: Sun, 23 Sep 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites