仅将压缩图像保存到数据库

I am trying to save the compressed image to database as well as to the folder. Now both the images are getting saved. Here is my code

To compress image i used this code referring reduce image size while uploading using the following PHP code used to upload image

Here is my complete code

$k1 = mysqli_query($con, "select img_path from members where     mem_id='".$_SESSION['user_id']."'");
$k2 = mysqli_fetch_array($k1);
$dirname = "pics/".$_SESSION['user_id']."/"; 
$target = $dirname . basename( $_FILES['docs']['name']); 

function thumbnail( $img, $source, $dest, $maxw, $maxh ) {      
    $jpg = $source.$img;

if( $jpg ) {
    list( $width, $height  ) = getimagesize( $jpg ); //$type will return the type of the image
    $source = imagecreatefromjpeg( $jpg );

    if( $maxw >= $width && $maxh >= $height ) {
        $ratio = 1;
    }elseif( $width > $height ) {
        $ratio = $maxw / $width;
    }else {
        $ratio = $maxh / $height;
    }

    $thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
    $thumb_height = round( $height * $ratio );

    $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

    imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );

    $path = $dest.$img."_thumb.jpg"; // I want to save _thumb.php image
    imagejpeg( $thumb, $path, 75 );
}
imagedestroy( $thumb );
imagedestroy( $source );

}

if( isset( $_FILES['docs'] ) ) {
$img = str_replace( " ","_",$_FILES['docs']['name'] );
move_uploaded_file( $_FILES['docs']['tmp_name'], $target );
$source = "pics/".$_SESSION['user_id']."/";
$dest = "pics/".$_SESSION['user_id']."/";
thumbnail( $img, $source, $dest, 480, 400 );
unlink($dirname."/".$k2['img_path']);   
}

$m1 = "UPDATE members SET fname = '".$fname."', lname = '".$lname."',  password='".$password_hash."',  img_path = '".$docs."'  WHERE mem_id='".$_SESSION['user_id']."'";

}

UPDATED

$k1 = mysqli_query($con, "select img_path from members where     mem_id='".$_SESSION['user_id']."'");
$k2 = mysqli_fetch_array($k1);
$dirname = "pics/".$_SESSION['user_id']."/"; 
$target = $dirname . basename( $_FILES['docs']['name']); 

function thumbnail( $img, $source, $dest, $maxw, $maxh, $file) {      
    $jpg = $source.$img;

if( $jpg ) {
   ///list( $width, $height  ) = getimagesize( $jpg ); //$type will return the type of the image
    //$source = imagecreatefromjpeg( $file ); //<---got rid of this.
    $source = imagecreatefromstring(file_get_contents($file)); //<----Added this.
    if( $maxw >= $width && $maxh >= $height ) {
        $ratio = 1;
    }elseif( $width > $height ) {
        $ratio = $maxw / $width;
    }else {
        $ratio = $maxh / $height;
    }

    $thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
    $thumb_height = round( $height * $ratio );

    //$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

    imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );

    $path = $dest.$img."_thumb.jpg"; // I want to save _thumb.php image
    imagejpeg( $thumb, $path, 75 );
}
imagedestroy( $thumb );
imagedestroy( $source );

}

if( isset( $_FILES['docs'] ) ) {
$img = str_replace( " ","_",$_FILES['docs']['name'] );
//move_uploaded_file( $_FILES['docs']['tmp_name'], $target );  <----This is saving the picture you don't want.  
$source = "pics/".$_SESSION['user_id']."/";
$dest = "pics/".$_SESSION['user_id']."/";
$file = $_FILES['docs']['tmp_name']; //<---- This is the file you are making the image with.
thumbnail( $img, $source, $dest, 480, 400, $file);  //<--Added $file to your function.
//unlink($dirname."/".$k2['img_path']);   
}

$m1 = "UPDATE members SET fname = '".$fname."', lname = '".$lname."',  password='".$password_hash."',  img_path = '".$docs."'  WHERE mem_id='".$_SESSION['user_id']."'";

}

As I said in my comment. Your code is saving two files. 1.) move_uploaded_file() is saving a picture. 2.) imagejpeg() is saving a picture with the "_thumb" added to it.

So I made some changes to your code. 1.) Lets not move_uploaded_file. 2.) I changed your thumbnail function to pass the file and created your image object from it directly.

See comments in the code.

$k1 = mysqli_query($con, "select img_path from members where     mem_id='".$_SESSION['user_id']."'");
$k2 = mysqli_fetch_array($k1);
$dirname = "pics/".$_SESSION['user_id']."/"; 
$target = $dirname . basename( $_FILES['docs']['name']); 

function thumbnail( $img, $source, $dest, $maxw, $maxh, $file) {      
    $jpg = $source.$img;

if( $jpg ) {
    //list( $width, $height  ) = getimagesize( $jpg ); //$type will return the type of the image  //<--This won't work anymore. 

    //$source = imagecreatefromjpeg( $file ); //<---got rid of this.
    $source = imagecreatefromstring(file_get_contents($file)); //<----Added this.

    $width   = imagesx($source); //<---Added this
    $height  = imagesy($source); //<---And this  

    if( $maxw >= $width && $maxh >= $height ) {
        $ratio = 1;
    }elseif( $width > $height ) {
        $ratio = $maxw / $width;
    }else {
        $ratio = $maxh / $height;
    }

    $thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
    $thumb_height = round( $height * $ratio );

    $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

    imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );

    $path = $dest.$img."_thumb.jpg"; // I want to save _thumb.php image
    imagejpeg( $thumb, $path, 75 );
}
imagedestroy( $thumb );
imagedestroy( $source );

}

if( isset( $_FILES['docs'] ) ) {
$img = str_replace( " ","_",$_FILES['docs']['name'] );
//move_uploaded_file( $_FILES['docs']['tmp_name'], $target );  <----This is saving the picture you don't want.  
$source = "pics/".$_SESSION['user_id']."/";
$dest = "pics/".$_SESSION['user_id']."/";
$file = $_FILES['docs']['tmp_name']; //<---- This is the file you are making the image with.
thumbnail( $img, $source, $dest, 480, 400, $file);  //<--Added $file to your function.
unlink($dirname."/".$k2['img_path']);   
}

$docs = $dest.$img."_thumb.jpg"; <---Updated path to save to database.

$m1 = "UPDATE members SET fname = '".$fname."', lname = '".$lname."',  password='".$password_hash."',  img_path = '".$docs."'  WHERE mem_id='".$_SESSION['user_id']."'";

}

That should work. Hope that helps.