Chris
Chris

Reputation: 155

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

Upvotes: 8

Views: 1065

Answers (1)

KorreyD
KorreyD

Reputation: 1294

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;

}

Upvotes: 1

Related Questions