• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 28
  • Last Modified:

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 Ali
Shahab Ali
  • 2
  • 2
1 Solution
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.
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now