Hello, dear! On the eve did the function of reducing the size of the image. So, it does not work to bring it into working condition. Namely, the $source variable does not want to be passed from the switch as the 2nd parameter to imagecopyresized .

 Notice: Undefined variable: source in .../functions.php on line 97 Warning: imagecopyresized() expects parameter 2 to be resource, null given in .../functions.php on line 97 Notice: Undefined variable: source in .../functions.php on line 111 Warning: imagedestroy() expects parameter 1 to be resource, null given in .../functions.php on line 111 

What could be the reason? I do on a lokalka.

Here is the source code:

 function thumImg($file) { $ext = strtolower(strrchr(basename($file), '.')); $prefix = 'thum_'; $th = "thum/"; // $extension = array('.jpg', '.gif', '.png'); $imgname = basename($file); $sas = getimagesize($file); $width = $sas[0]; $height = $sas[1]; if ($width >= $height) { $newwidth = 200; $newheight = $height / ($width / 200); } else { $newwidth = $width / ($height / 200); $newheight = 200; } $thumb = imagecreatetruecolor($newwidth, $newheight); switch ($ext) { case '.jpg': $source = imagecreatefromjpeg($file); break; case '.gif': $source = imagecreatefromgif($file); break; case '.png': $source = imagecreatefrompng($file); break; } imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); switch ($ext) { case '.jpg': imagejpeg($thumb, $th . $prefix . $imgname); break; case '.gif': imagegif($thumb, $th . $prefix . $imgname); break; case '.png': imagepng($thumb, $th . $prefix . $imgname); break; } imagedestroy($thumb); imagedestroy($source); 

}

I beg you to help with advice. Google did not give anything adequate. Help php too. Also, could you explain the imagecopyresized function to imagecopyresized , namely, what is the 2nd parameter (Source image link resource)? That is the source for the image of what? Thanks in advance.

  • In general, your switch just does not work. Before it, do var_dump($file, $ext) and give the result. A, and an example call. I am plagued by vague doubts) - Sh4dow
  • Doubt about what?) - zvlex
  • You give the result, it is too early to make conclusions - Sh4dow
  • string '... temp \ php3C09.tmp' (length = 40) string '.tmp' (length = 4) added) - zvlex

1 answer 1

Well, here's your answer: switch does not work.

Why? Because you are making a nightstand from a temporary file with the extension .tmp

What we do: before calling the function, copy the file in a normal way, i.e. let's say you get the file via $_FILES['upload']

 $new_name = $_SERVER['DOCUMENT_ROOT'].'/path/to/images/'.$_FILES['upload']['name']; $image = move_uploaded_file($_FILES['upload']['tmp_name'], $new_name); thumImg($new_name); 

ZY: Modify the function yet, so as not to consider it a bydlokod - make the extension check (and better the mimetype , everything is mimetype up in Google) and the return value ( true/false ), add to the switch

 default: return false; break; 

ZZY: images image/jpeg are .jpg and .jpeg

  • I noticed that he does not plow). Thank you Sh4dow, I wouldn’t have come to this myself as soon as I try to add a function, accomplish what came of it.) - zvlex
  • In my case, the extension check is performed by another function, it just forms a thumbnail. - zvlex pm
  • Thank you for such a detailed answer and for pointing out my mistakes). Everything worked) - zvlex