imagejpeg is not saving image

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 $ filename in bd, but what it saves is: Resource id # 4 and the image is not saved in the img / / Why?

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

    
asked by anonymous 14.03.2015 / 21:31

1 answer

1

The function imagecreatefromjpeg and derivatives return the image identifier feature if they succeed, the < in> Resource id # 4 indicates that you are manipulating a resource.

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

You may 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 header function is not required in this case because you want to save the image only. The imagedestroy is used to free the memory associated with an object. In this case we could use it like this:

imagedestroy($img);
    
15.03.2015 / 01:21