Adjust photo when uploading

Asked

Viewed 52 times

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 horizontal photo, it is stretched. An example:

inserir a descrição da imagem aqui

I wonder if you have how to adjust the photos, regardless of whether the photo is horizontal or vertical, but keeping all the same height and width, even if you create white borders next door in case the image is vertical.

1 answer

1


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

Start by recovering the width and height of the image with the function getimagesize(), example:

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

Then you should check which one is bigger, whether it’s height or width, and which one is smaller you recalculate proportionally (not to distort).

Browser other questions tagged

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