How to save ". png" images without black background in php?

Asked

Viewed 44 times

0

good morning!

I’m new in php and if anyone can help me, I’d appreciate it. I’m saving the images, but when they have a transparent background, after saving, they have a black background. The function I’m using is this:

    function redimensionarImg($imagem_temporaria, $largura, $altura) {
    $largura_original = imagesx($imagem_temporaria);
    $altura_original = imagesy($imagem_temporaria);

    $nova_largura = $largura ? $largura : floor(($largura_original / $altura_original) * $altura);

    $nova_altura = $altura ? $altura : floor(($altura_original / $largura_original) * $largura);

    $imagem_redimensionada = imagecreatetruecolor($nova_largura, $nova_altura);

    imagecopyresampled($imagem_redimensionada, $imagem_temporaria, 0, 0, 0, 0, $nova_largura, $nova_altura, $largura_original, $altura_original);

    return $imagem_redimensionada;
}
  • This can help you: https://answall.com/questions/291634/problema-ao-criaruma-imagem-com-fundo-transparent

1 answer

0

The following native functions can help you.

$image = imagecreatefrompng($path_to_file);

//Trata transparência para PNG
imagealphablending($image, false);
imagesavealpha($image, true);
imagecolortransparent($image);

//Salva imagem já com fundo transparente
imagepng($image, $path_to_save);
  • With the forgiveness of ignorance I could not use :-(

  • Make the mistakes so we can see how to help you...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.