When generating the thumbnail, the image goes to the All Black directory

The code below is for creating thumb. I would like not to use ready-made libraries, but the generated thumb is going to the all black folder. Gd2 is active in php. See:

$foto = "imagens/fox.jpg";

$diretorioNormal = "imagens/normal/";
$diretorioThumb = "imagens/thumb/";

// Tamanho do arquivo
$tamanhoMaximo = 1024 * 1024 * 3; // 3Mb
$tamanhoArquivo = filesize($foto);

// Extensao da foto
list($arquivo,$extensao) = explode(".",$foto);

// Dimensões da imagem
list($largura,$altura) = getimagesize($foto);

if($tamanhoArquivo > $tamanhoMaximo){
    $erro = "O arquivo não pode ser superior a 3Mb";
}else if($extensao != 'jpg' && $extensao != 'png'){
    $erro = "A extensão do arquivo tem que ser jpg ou png";
}else{

    // Criando e codificando padronizando para extensão jpg
    $codificarFoto = md5($arquivo.time()).".jpg";   

    // Novas dimensões da imagem
    $novaLargura = 200;
    $novaAltura = 200;

    // Gerar a miniatura
    $miniatura = imagecreatetruecolor($novaLargura, $novaAltura);
    $imagem = imagecreatefromjpeg($codificarFoto);
    imagecopyresampled($miniatura, $imagem, 0, 0, 0, 0, $novaLargura, $novaAltura, $largura, $altura);

    // Qualidade da imagem
    //copy($codificarFoto, $diretorioThumb.$codificarFoto);
    imagejpeg($miniatura,$diretorioThumb.$codificarFoto,50);

    // destruir a imagem
    imagedestroy($miniatura);
}
Author: Fox.11, 2016-02-07

1 answers

Your problem is in the following Line:

$imagem = imagecreatefromjpeg($codificarFoto);

See, the function (imagecreatefromjpeg)is to create an image from a jpeg.

The variable"cod encode" is just a string that you created earlier to name the new image that will be generated, the image does not yet exist in HD and cannot give rise to another image.

Change the line to the following code:

$imagem = imagecreatefromjpeg($foto);

See, the variable"foto photo" represents an image that fact exists on your disk, it is from it that a new image will be generated.

 2
Author: Filipe Moraes, 2016-02-07 15:04:48