When generating the thumbnail, the image goes to the whole black directory

Asked

Viewed 316 times

1

The code below is for the creation of Thumb. I would like to not use ready libraries, but the generated Thumb is going to the whole 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);
}
  • Probably your script is not finding the image "fox.jpg", so a black image is generated. Use the "file_exists" function to check if the system finds the image.

  • Philip. The image exists, I’ve already taken the test.

  • What is the width and height of the image "fox.jpg"?

  • the actual size is 960 X 720...

  • See if this topic helps (English): http://stackoverflow.com/questions/19551747/why-does-this-basic-imagejpeg-resizer-returns-a-black-image

  • Give me a hint as to what the question should be... because I posed the problem I’m going through at the moment

Show 1 more comment

1 answer

2


Your problem is on the following line:

$imagem = imagecreatefromjpeg($codificarFoto);

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

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

Exchange the line for the following code:

$imagem = imagecreatefromjpeg($foto);

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

  • Bingo Filipe... exactly this... thank you so much for your help...

Browser other questions tagged

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