Fit picture when uploading

2

Colleagues.

I have a system where I created an upload method:

public function gerarThumb($foto){
   $diretorioNormal = "../../produtos/";
   $novaLargura = 997;
   $novaAltura = 665;
   $miniatura = imagecreatetruecolor($novaLargura, $novaAltura);
   $imagem = imagecreatefromjpeg($foto);
   imagecopyresampled($miniatura, $imagem, 0, 0, 0, 0, $novaLargura,   $novaAltura, $largura, $altura);
   imagejpeg($miniatura,$diretorioNormal.$foto,90);
   return $foto;
} // Fim do método gerarThumb

So far so good, all right, but if a person does not take the photo horizontally, it stretches. An example:

I would like to know if you have how to adjust photos, regardless of whether the photo is horizontal or vertical, but keeping them all the same height and width, even if you create white borders on the side in case the image is vertical.

    
asked by anonymous 10.05.2017 / 21:19

1 answer

1

Hello, you should resize the image without distorting it. You can not use fixed width or height as you are doing on lines 3 and 4.

Begin by retrieving the width and height of the image with the getimagesize () function, eg

$imgInfo = getimagesize($pathImg);
$larg = $imgInfo[0];
$alt = $imgInfo[1];

Then you should check which is larger, if it is the height or the width, and the smaller one you recalculate proportionally (not to distort).

    
10.05.2017 / 21:46