Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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..

like image 819
Chris Avatar asked Nov 11 '10 18:11

Chris


1 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;

}

like image 114
KorreyD Avatar answered Oct 13 '22 20:10

KorreyD