Link to home
Start Free TrialLog in
Avatar of Shahab Ali
Shahab Ali

asked on

Transparency not completely removed imagerotate

User generated imageHi, I am trying to rotate an image using php image rotate and my goal is to retain the transparency of the image after the rotation. I tried the following method according to a solution posted by techtonik but it doesn't completely make the image transparent as the original image. There are still some parts with the color. I have added an image which shows my results. The green color can be seen in the square image.


        $imagew = imagecreatefrompng('new_image.png')
        $stubColor = imagecolorallocate($imagew, 0, 255, 0);
        imagefill($imagew, 0, 0, $stubColor);
        $imgcolors = imagecolorstotal($imagew);

        $rotimg = imagerotate($imagew, 25, $stubColor);
        imagedestroy($imagew);

        imagetruecolortopalette($rotimg, false, $imgcolors);
        imagecolortransparent($rotimg, imagecolorat($imagew, 0, 0));

        for ($levels = 0; $levels < 10; $levels++) {
            $stubColor = imagecolorclosest($rotimg, 128, 100, 100);
            imagecolordeallocate($rotimg, $stubColor);
            imagecolorallocate($rotimg, 0, 0, 0);
        }

        imagepng($rotimg, 'my_image.png');
        imagedestroy($rotimg);

        $imagew = imagecreatefrompng('my_image.png');
        $imgcolors = imagecolorstotal($imagew);
        imagecolortransparent($imagew, imagecolorat($imagew, 0, 0));
        imagepng('my_image.png1');
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shahab Ali
Shahab Ali

ASKER

Now there is no extra color on the edges when the image is rotated. Thank you but there is one small problem. Now the images have a thin black border line. Any idea how that can be removed?
Not sure - maybe post a new question and open it up to some other experts.
Okay thanks