Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Gd rotate image

Tags:

php

gd

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.

like image 249
Alec Smart Avatar asked Jul 14 '26 16:07

Alec Smart


1 Answers

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

like image 129
fedeghe Avatar answered Jul 16 '26 07:07

fedeghe