Sharpless512
Sharpless512

Reputation: 3222

PNG has black background

I'm using a lib to easy save images.

http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

But the problem is that it saves transparant png with black background. I have search and found something that should work but is not working for me.

imagecolortransparent() should normally do it. But for me I still see the black background.

What I'm I doing wrong?

$imagename = $_FILES['file']['name'];           
$target = "source/images/".$imagename;
$image->save($target);


function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null){

  if( $image_type == IMAGETYPE_PNG ){

      $image = $this->image;

      $black = imagecolorallocate($image, 0, 0, 0);
      imagecolortransparent($image, $black);

      imagepng($image,$filename);
  }

}

Upvotes: 0

Views: 554

Answers (1)

tlenss
tlenss

Reputation: 2609

You should probably change

$black = imagecolorallocate($im, 0, 0, 0);

to

$black = imagecolorallocate($image, 0, 0, 0);

Try setting image alpha blending

imagealphablending($image, false);
imagesavealpha($image, true);

Upvotes: 1

Related Questions