Create thumbnail from a multiple upload

Asked

Viewed 95 times

1

Good afternoon, already researched and tried several options and could not.

I’m trying to upload multiple images, and each image generates a thumbnail and record only the original images in the database. But it generates the Thumb of only one image or creates an image with all the names of the upload files, this gives when I change $novaImagem for $img for example:

tbn_1231455.jpg,tbn_3244545.jpg,tbn_8748545.jpg

I’m in need of help, below is my code.

if (isset($_FILES['imagemlocal']))
{       
   $largura = 1024;
   $altura = 1024;
   $tamanho =  200000;
   $tmp = $_FILES['imagemlocal'];
   list($largura,$altura,$tip)=getimagesize($tmp);
   $dimensoes = getimagesize($imagemlocal["tmp_name"]);

   $img = array();
   for ($key = 0; $key < count($tmp['name']); $key++) {
      $path = $_FILES['imagemlocal']['name'][$key];
      $ext = pathinfo($path, PATHINFO_EXTENSION);
      $temp_name = $_FILES['imagemlocal']['tmp_name'][$key];
      $novaImagem = md5(uniqid(time())) . "." . $ext;
      $caminho = 'img/'.$novaImagem;
      move_uploaded_file($temp_name, $caminho);
      $img[] = ("$novaImagem");
   }
   $img = implode(', ', $img);          
}

//thumb

if($tip > 1) {
   $percent = 0.15;
   $new_largura = $largura * $percent;
   $new_altura = $altura * $percent;

   $thundebal = imagecreatefromjpeg($caminho);
}
else {
   $thundebal = imagecreatefrompng($caminho);
}

$Thundeball = imagecreatetruecolor($new_largura, $new_altura);
imagecopyresampled($Thundeball, $thundebal, 0, 0, 0, 0, $new_largura, $new_altura, $largura, $altura);
if($tip > 1) {
    imagejpeg($Thundeball,"img/".'/tbn_'.$novaImagem);
}
else {
    imagepng($Thundeball,"img/".'/tbn_'.$novaImagem);
}
  • 1

    The code that generates Thumb needs to be inside the for

  • The variable: $novaImagem out of the for, will always be the last in the list, you will only generate one ever. Everything has to be inside the loop according to the quantities.

  • Ivan Ferrer and caiocafardo, thank you very much!!! My project was stopped. It worked perfectly well!!!

  • @Ivanferrer I sent a message through your website.

No answers

Browser other questions tagged

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