imagejpeg is not saving image

Asked

Viewed 1,388 times

2

Follow the code, then the question.

$localDiretorioFoto = "img/mensagens/";
$img = $_FILES["foto"]["tmp_name"];
$imgg = getimagesize($img);

if($imgg["mime"] == "image/jpeg") {
    $nomeFoto = imagecreatefromjpeg($img);
} else if($imgg["mime"] == "image/png") {
    $nomeFoto = imagecreatefrompng($img);
}

imagejpeg($nomeFoto, $localDiretorioFoto, 50);

I’m trying to save an image, and I’m saving the $name in the comic book, but what it saves is: Resource id #4 and the image is not saved in the folder img/messages/ why?

Should I use header and/or imagedestroy? If so, why?

  • Someone? :(.....

  • I’m trying to create an answer. =)

  • Ah, okay @Qmechanic73! Thank you.

  • I risked an answer, see if that’s it.

1 answer

1


The function imagecreatefromjpeg and derivatives return the image identifier resource if successful, the Resource id #4 indicates that you are manipulating a resource.

Image is not being saved because you are specifying only directory, image name is omitted.

You might be doing something like this:

if (isset($_FILES['foto'])){
    $path = $_FILES["foto"]["tmp_name"];
    $nomeFoto = $_FILES["foto"]["name"];
    $diretorioFoto = "img/mensagens/". $nomeFoto;
    $imageSize = getimagesize($path);

    switch(strtolower($imageSize['mime'])){
        case 'image/jpeg':
          $img = imagecreatefromjpeg($path);
          break;
        case 'image/png':
          $img = imagecreatefrompng($path);
          break;
        default: die();
    }

    if(imagejpeg($img, $diretorioFoto, 50) === true){
        echo "Imagem salva em ". $diretorioFoto;
    } else {
        echo "Erro ao salvar a imagem em ". $diretorioFoto;
    }
}

The function header is not required in this case as you want to save the image only. The imagedestroy is used to release the memory associated with an object. In this case we could use it like this:

imagedestroy($img);
  • Thank you for being willing to help. I tested and continue the same way, Resource id #4 and does not create anything in the img/men folder...

  • @Igor Bah, I updated the answer, I tested it here and it worked, see if it works for you too.

  • 1

    Now it has worked! Thank you so much @Qmechanic73!!!!!

Browser other questions tagged

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