Passing parameter from switch

Hello dears! The day before I did the function of reducing the image size. So, there is no way to get it to work.
Namely, the $sourcevariable does not want to be passed from switchas 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 it locally.

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);
        case '.gif':
            $source = imagecreatefromgif($file);
        case '.png':
            $source = imagecreatefrompng($file);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    switch ($ext) {
        case '.jpg':
            imagejpeg($thumb, $th . $prefix . $imgname);
        case '.gif':
            imagegif($thumb, $th . $prefix . $imgname);
        case '.png':
            imagepng($thumb, $th . $prefix . $imgname);


Please help with advice. Google didn’t turn up anything relevant. PHP help too.
Also, could you explain to me the imagecopyresizedfunction, namely, what is the 2nd parameter (Source image link resource)? That is, the source for the image of what?
Thanks in advance.

Answer 1, authority 100%

Well, here’s the answer for you: switch doesn’t work.

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

What we do: before calling the function, we copy the file in a normal way, i.e. let’s say you get a 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);

PSY: Refine the function further so as not to consider it a redundant code – check the extensions (or rather mimetype, everything is searched in Google) and the return value (true/false) , in switchadd

default: return false; break;

ZZY: image/jpegimages are .jpgand .jpeg