Olá.
Recebo fotos tiradas pela câmera de celular via formulário/upload que serão exibidas em um "Feed de Notícias".
Na exibição desse Feed, posso ter 5, 10, 40 imagens carregadas na mesma página, e se todas estiverem com tamanho original/grande, 7MB por exemplo (4000x4000), o carregamento e consequentemente utilização ficaria inviável.
Devido a isso, utilizo uma função para redimensiona-las proporcionalmente com limite de largura e altura de 1200px.
A questão é que imagens grandes verticalmente estão sendo rotacionadas, aparentemente no carregamento imagecreatefromjpeg().
Pesquisei e testei inúmeros códigos/funções por aí. Testei vários exemplos desse site e todos continuam rotacionando: https://hotexamples.com/pt/examples/-/-/imagecopyresampled/php-imagecopyresampled-function-examples.html
Aqui no fórum, encontrei esse tópico de 2015 onde esse usuário passou pelo mesmo problema, inclusive ele cita a mesma questão que ocorre por aqui.
A imagem original tem 3472 de largura, 4624 de altura, mas a função getimagesize() retorna invertido, 4624 de largura x 3472 de altura.
Minha função:
function redimensionarImagem($largura_max = null, $altura_max = null, $caminho = null) {
if($largura_max != null && $altura_max != null && $caminho != null) {
$maxDimW = intval($largura_max);
$maxDimH = intval($altura_max);
$file_name = $caminho;
list($width, $height, $type, $attr) = getimagesize($file_name);
if($width > $maxDimW || $height > $maxDimH) {
// Com o tipo de imagem $type
// Carrega imagem com funcao especifica
switch ($type) {
case IMAGETYPE_JPEG:
$src = imagecreatefromjpeg($file_name);
break;
case IMAGETYPE_GIF:
$src = imagecreatefromgif($file_name);
break;
case IMAGETYPE_PNG:
$src = imagecreatefrompng($file_name);
break;
default:
return "TIPO DE ARQUIVO não SUPORTADO";
}
// Define medidas da nova imagem a ser criada
// Calculando a proporção
$ratio_orig = $width / $height;
if ($maxDimW/$maxDimH > $ratio_orig)
$maxDimW = $maxDimH * $ratio_orig;
else
$maxDimH = $maxDimW / $ratio_orig;
$new_width = round($maxDimW);
$new_height = round($maxDimH);
$dst = imagecreatetruecolor($new_width, $new_height);
// Para png, corrige background 'black'
switch ($type) {
case IMAGETYPE_PNG:
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($dst, 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($dst, $background);
// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($dst, false);
// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dst, true);
//
break;
default:
break;
}
//
imagecopyresampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagedestroy($src);
// Conforme o tipo de imagem enviada
// Salva a nova imagem por cima da foto original
switch ($type) {
case IMAGETYPE_JPEG:
imagejpeg($dst, $file_name);
break;
case IMAGETYPE_GIF:
imagegif($dst, $file_name);
break;
case IMAGETYPE_PNG:
imagepng($dst, $file_name);
break;
default:
return "ERRO - FORMATO DE SAIDA não IDENTIFICADO";
}
imagedestroy($dst);
return "CONVERSAO OK";
}
else
return "SEM NECESSIDADE DE CONVERSAO";
}
else
return "PARAMETROS não INICIADOS";
}
Alguém tem uma luz? rs