简体   繁体   中英

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);
  }

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM