Boa tarde galera, estou tendo problemas nas cores e tamanho do arquivo no processamento do resize com PNG.
Segui tutoriais e achei que estava ok, mas depois percebi que algumas cores mudam e o tamanho do arquivo png aumenta muito.
Vou postar o código aqui e continuar fazendo testes.
Caso percebam algo de errado, por favor, me deem uma luz. Valeu!
<?php
// Redimensiona fotos
function redimensionaImg($type, $img, $max_x, $max_y, $nome_foto)
{
list($width, $height) = getimagesize($img);
$original_x = $width;
$original_y = $height;
// se a largura for maior que altura
if($original_x > $original_y)
{
$porcentagem = (100 * $max_x) / $original_x;
}
// se a altura for maior que a largura
else
{
$porcentagem = (100 * $max_y) / $original_y;
}
$tamanho_x = $original_x * ($porcentagem / 100);
$tamanho_y = $original_y * ($porcentagem / 100);
if (($type == 'image/jpeg') OR ($type == 'image/pjpeg'))
{
$image = imagecreatefromjpeg($img);
$image_p = imagecreatetruecolor($tamanho_x, $tamanho_y);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $tamanho_x, $tamanho_y, $width, $height);
return imagejpeg($image_p, $nome_foto, 100);
}
else if (($type == 'image/png') OR ($type == 'image/x-png'))
{
$image = imagecreatefrompng($img);
$is_true_color = imageistruecolor($img);
if ($is_true_color)
{
$image_p = imagecreatetruecolor($tamanho_x, $tamanho_y);
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
}
else
{
$image_p = imagecreate($tamanho_x, $tamanho_y);
imagealphablending($image_p, false);
$colorTransparent = imagecolorallocatealpha($image_p, 0, 0, 0, 127);
imagefill($image_p, 0, 0, $colorTransparent);
imagesavealpha($image_p, true);
imagealphablending($image_p, true);
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $tamanho_x, $tamanho_y, $width, $height);
return imagepng($image_p, $nome_foto, 0);
}
}
?>
Com JPEG está tranquilo, o problema ocorre somente com PNG mesmo...