0
I have a class that generates thumbnail correctly, but when sending photos vertically, it is stretched. As I would inside this class I crop or resize the photo so that it fits in the thumbnail dimensions?
/**
* Cria thumbnail das imagens
* @return $diretorioThumb.$codificarFoto
* @param $foto
*/
public function gerarThumb($foto){
$diretorioNormal = "produtos/";
$diretorioThumb = "produtos/thumb/";
$fotoDir = $diretorioNormal.$foto;
list($largura,$altura) = getimagesize($fotoDir);
$novaLargura = 240;
$novaAltura = 165;
$codificarFoto = $foto;
list($arquivo,$extensao) = explode(".",$foto);
if($extensao == "jpg" || $extensao == "jpeg" || $extensao == "JPG"){
$miniatura = imagecreatetruecolor($novaLargura, $novaAltura);
$imagem = imagecreatefromjpeg($fotoDir);
imagecopyresampled($miniatura, $imagem, 0, 0, 0, 0, $novaLargura, $novaAltura, $largura, $altura);
imagejpeg($miniatura,$diretorioThumb.$codificarFoto,90);
}
if($extensao == "png"){
$miniaturaPNG = imagecreatetruecolor($novaLargura, $novaAltura);
$imagemPNG = imagecreatefrompng($fotoDir);
imagecopyresampled($miniaturaPNG, $imagemPNG, 0, 0, 0, 0, $novaLargura, $novaAltura, $largura, $altura);
imagepng($miniaturaPNG,$diretorioThumb.$codificarFoto,null,90);
}
return $diretorioThumb.$codificarFoto;
} // fim do método gerarThumb
Hello Fabio. I tried to use this library, but it gives the following error: Fatal error: Cannot declare class Wideimage_invalidimagehandleexception, because the name is already in use in
– user24136
There is no way to do this directly in PHP without libraries? VI which has imagecrop(), but how to use this function?
– user24136
There is, but there’s no point in having a job if there’s something ready. This is the purpose of libraries, cool you already get this habit because they are good practices. But if you still want the job, take a look at how the library code works.
– Fabio Luis Alexandre