2

HEllo,

I am trying to rotate a circular image around the center and then cut off the sides. I see the imagerotate function, but it does not seem to rotate about centre.

Anyone have any suggestions?

Thank you.

Update: Since it is a circle, I want to cut off the edges and keep my circle in the same dimensions.

Alec Smart
  • 94,115
  • 39
  • 120
  • 184
  • Just as a random sidenote, PHP came out with an update just recently that fixes a security bug with GD imagerotate... Just thought that was an interesting tidbit. – KyleFarris Apr 23 '09 at 13:41

3 Answers3

5

I faced successfully that problem with the following code

    $width_before = imagesx($img1);
    $height_before = imagesy($img1);
    $img1 = imagerotate($img1, $angle, $mycolor);

    //but imagerotate scales, so we clip to the original size

    $img2 = @imagecreatetruecolor($width_before, $height_before);
    $new_width = imagesx($img1); // whese dimensions are
    $new_height = imagesy($img1);// the scaled ones (by imagerotate)
    imagecopyresampled(
        $img2, $img1,
        0, 0,
        ($new_width-$width_before)/2,
        ($new_height-$height_before)/2,
        $width_before,
        $height_before,
        $width_before,
        $height_before
    );
    $img1 = $img2;
    // now img1 is center rotated and maintains original size

Hope it helps.

Bye

blex
  • 24,941
  • 5
  • 39
  • 72
fedeghe
  • 1,243
  • 13
  • 22
4

The documentation says that it does rotate around the center.

Unfortunately it also says that it will scale the image so that it still fits. That means that whatever you do this function will change the size of your internal circular image.

You could (relatively easily) calculate how much scaling down will happen and then prescale the image up appropriately beforehand.

If you have the PHP "ImageMagick" functions available you can use those instead - they apparently don't scale the image.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

According to the PHP manual imagerotate() page:

The center of rotation is the center of the image, and the rotated image is scaled down so that the whole rotated image fits in the destination image - the edges are not clipped.

Perhaps the visible center of the image is not the actual center?

karim79
  • 339,989
  • 67
  • 413
  • 406