Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Boa tarde,
Estou com problemas para manipular imagem png transparente. A imagem só sobe com fundo preto. Se puderem olhar, por favor:
public function saveImg($filename) {
list( $largura, $altura ) = getimagesize($filename);
if ($largura > $this->max_img_width || $altura > $this->max_img_height) {
return $this->ResizeImg($filename);
}
$novo_nome = $this->path . $this->randName($filename);
move_uploaded_file($filename, $novo_nome);
return $novo_nome;
}
protected function ResizeImg($filename) {
list( $largura, $altura ) = getimagesize($filename);
$img_type = $this->getImgType($filename);
if ($largura > $altura) {
$nova_largura = $this->max_img_width;
$nova_altura = round(($nova_largura / $largura) * $altura);
} elseif ($altura > $largura) {
$nova_altura = $this->max_img_height;
$nova_largura = round(($nova_altura / $altura) * $largura);
} else {
$nova_altura = $nova_largura = $this->max_img_width;
}
$src_img = call_user_func('imagecreatefrom' . $img_type, $filename);
$dst_img = imagecreatetruecolor($nova_largura, $nova_altura);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nova_largura, $nova_altura, $largura, $altura);
$nome_img = $this->path . $this->randName($filename);
if ($img_type == 'jpeg') {
imagejpeg($dst_img, $nome_img, $this->img_quality);
} else {
call_user_func('image' . $img_type, $dst_img, $nome_img);
}
imagedestroy($src_img);
imagedestroy($dst_img);
return $nome_img;
}
Obrigado pela atenção.
Bom dia Hugo, estava olhando sua classe, parece atender bem minha necessidade. Só que uso esta classe em um site até grande, e gostaria de não tem que alterar a estrutura, por isso a manutenção desta classe que estou usando.
Bom Williams, da forma que sugeriu, a imagem toda fica preto, png ou jpg.
Obrigado pela atenção.
Segue um link, de uma olhada e tente adaptar.
Consegui sim, obrigado Williams. O código ficou assim:
protected function ResizeImg($filename) {
list( $largura, $altura ) = getimagesize($filename);
$img_type = $this->getImgType($filename);
if ($largura > $altura) {
$nova_largura = $this->max_img_width;
$nova_altura = round(($nova_largura / $largura) * $altura);
} elseif ($altura > $largura) {
$nova_altura = $this->max_img_height;
$nova_largura = round(($nova_altura / $altura) * $largura);
} else {
$nova_altura = $nova_largura = $this->max_img_width;
}
$src_img = call_user_func('imagecreatefrom' . $img_type, $filename);
$dst_img = imagecreatetruecolor($nova_largura, $nova_altura);
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nova_largura, $nova_altura, $largura, $altura);
$nome_img = $this->path . $this->randName($filename);
if ($img_type == 'jpeg') {
imagejpeg($dst_img, $nome_img, $this->img_quality);
} else {
imagepng($dst_img,$nome_img);
}
imagedestroy($src_img);
imagedestroy($dst_img);
return $nome_img;
}
Obrigado.
imagecopyresampled é um problema com png transparente, tente com imagecolorallocatealpha