Transparency not completely removed imagerotate

Added an image that shows an original image with transparent background and the modified image after the code has been executedHi, 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);

        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');

        $imagew = imagecreatefrompng('my_image.png');
        $imgcolors = imagecolorstotal($imagew);
        imagecolortransparent($imagew, imagecolorat($imagew, 0, 0));
Shahab AliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Try add 127 to this line
$stubColor = imagecolorallocate($imagew, 0, 255, 0, 127);

Open in new window

Also you are setting green as the colour to use for your transparent areas - are you sure that is what you want.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shahab AliAuthor Commented:
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?
Julian HansenCommented:
Not sure - maybe post a new question and open it up to some other experts.
Shahab AliAuthor Commented:
Okay thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.