图像库将变量传递到标题位置不起作用?

This below mentioned code is written localhost/uploads/gallery.php file. I am including it in another page so it can be a part of header and footer including all my styling. I did it by include('uploads/gallery.php'); into a div.

Everything is working fine except the header location. When I go to the page where gallery is included which is "localhost/index.php?page=media"(localhost/views/media.php). It does not display thumbnails. I have to refresh the page to see thumbnails. Note: the code is written to display thumbnails when they are generated without refreshing the page.

My concern for asking question is, why i have to refresh page to load thumbnails. My header location is not passing variables correctly. I am a complete noob. please help me with this. Shukriya.

<?php
if(isset($_GET['img'])){

    if(file_exists("uploads/{$_GET['img']}")) {

        ignore_user_abort(true);

        set_time_limit(120);

       ini_set('memory_limit', '256M');

        $src_image = getimagesize("uploads/{$_GET['img']}");
        var_dump($src_image);

        if($src_image === false) {

            die("Wrong Image");

        } # Wrong File Size

        // Fixed Thumbnail Size
        $thumb_width = 144;

        $thumb_height = 144;

        $thumb_ratio = round (($thumb_width / $thumb_height), 1);

        $src_ratio = round (($src_image[0] / $src_image[1]), 1);

        if ($src_ratio > $thumb_ratio) {
            //Landescape Image dynamic width...
            $new_size = array(($src_image[0] * $thumb_height)/$src_image[1], $thumb_height);
            $new_pos = array(($new_size[0] - $thumb_width)/2,0);

        } elseif ($src_ratio < $thumb_ratio) {
            //PORTRAIT Image dynamic Height.....
            $new_size = array($thumb_width, ($src_image[1] * $thumb_width)/$src_image[0]);
            $new_pos = array(0, ($new_size[1] - $thumb_height)/2);

        } elseif($src_ratio == $thumb_ratio) {
            //Square
            $new_size = array($thumb_width, $thumb_height);
            $new_pos = array(0, 0);
        }

        if($new_size[0] < 1) $new_size[0] = 1;
        if($new_size[1] < 1) $new_size[1] = 1;

        $imgz = 'uploads/thumbs/'.$_GET['img'];

        //GETTING Variable for Source image type. to create resample
        if($src_image['mime'] === "image/jpeg"){
            $src = imagecreatefromjpeg('uploads/'.$_GET['img']);
        } elseif($src_image['mime'] === "image/png"){
            $src = imagecreatefrompng('uploads/'.$_GET['img']);
        } elseif($src_image['mime'] === "image/gif"){
            $src = imagecreatefromgif('uploads/'.$_GET['img']);
        }


        $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
        imagecopyresampled($thumb, $src, 0,0, $new_pos[0], $new_pos[1], $new_size[0], $new_size[1], $src_image[0], $src_image[1]);

        //GETTING Variable for Destination thumb image type. to create resample
        if($src_image['mime'] === "image/jpeg"){
            imagejpeg($thumb, $imgz);
        } elseif($src_image['mime'] === "image/png"){
            imagepng($thumb, $imgz);
        } elseif($src_image['mime'] === "image/gif"){
            imagegif($thumb, $imgz);
        }

     header("Location: uploads/thumbs/{$_GET['img']}");


     } /* If file exists */ die();

 } // ALL SCRIPT ENDED



if(is_dir('uploads/thumbs') === false) {

    mkdir('uploads/thumbs', 0744);

} // Check & Create Directory

$images = glob('uploads/'.'*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE); 

foreach($images as $image){
    $imgs = 'uploads/thumbs/'.substr($image,8);
    $nme = substr($image,8);

  if(file_exists("$imgs")){

    echo "<a href=\"#\"><img src=\"$imgs\" alt=\"$nme\" class=\"img-thumbnail\" /></a>"; //uploads/{$img} , data-toggle=\"modal\" data-target=\"#myModal\" 

     } else {

    echo "<a href=\"\"><img src=\"?page=media&img=$nme\" alt=\"$nme\" class=\"img-thumbnail\" /></a>";

  }

}

?>