Home » Php » Why cant I make the background of a png transparent after rotating it with php?

Why cant I make the background of a png transparent after rotating it with php?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I tried literally all day yesterday trying to figure this out. I rotate an image via imagerotate(). I get a black background where the image isn’t covering anymore. I have tried everything i cant think of to make that background transparent..

here is my current code..

   function rotate($degrees) {
       $image = $this->image;
       imagealphablending($image, false);
       $color = imagecolorallocatealpha($image, 0, 0, 0, 127);
       $rotate = imagerotate($image, $degrees, $color);
       imagecolortransparent($rotate, $color);
       imagesavealpha($image, true);
       $this->image = $rotate;
   }

I am really starting to get ticked off. Can someone show me some working code? please?

Could it be something wrong with my WAMP server and dreamweaver? because i even tried this.. http://www.exorithm.com/algorithm/view/rotate_image_alpha and it still puts out either a black or white background..

How to&Answers:

Try setting imagesavealpha() on your rotated image.

Currently you are running imagesavealpha() on your original image. [ eg. imagesavealpha($image, true); ]

Instead you want to run imagesavealpha() on the rotated image and then set $this->image… try:

   ...
   $rotate = imagerotate($image, $degrees, $color);
   imagecolortransparent($rotate, $color);
   imagesavealpha($rotate, true);  // <- using $rotate instead of $image
   $this->image = $rotate;

}