
<div class='col-md-3'></div>
    <div class='col-md-6'>
        <div class="card">
             <div class="card-header">	
                <h5 class='card-title'><i class="fas fa-edit"></i>Ubah Profil</h5>
            </div
            <div class='box-body'>
	<form method="post" action="" enctype="multipart/form-data">
            <div class="card-body">
                    <div class="row">
						<div class="col-md-6">
                                <div class="form-group">
								<?php
								$tgl=date('Y-m-d');
                            $query = mysqli_query($koneksi, "select * FROM siswa WHERE id_siswa='$_SESSION[id_siswa]' ");  
                            $pr = (mysqli_fetch_array($query));
                               
                            ?>
							  <center>
							<?php if($pr['foto']==''){ ?>
					  <img src="dist/img/avatar.png" width="200">
							<?php }else{ ?>
					  <img src="foto/fotosiswa/<?= $pr['foto'] ?>" width="200">
					  <?php } ?>
					  </center>
					  
                    </div>
					</div>
					<div class="col-md-6">
					<div class="form-group">
                        <label>Username</label>
                      <input  type="text" class="form-control" value="<?= $pr['username'] ?>" readonly />
                    <input  type="hidden" name="idsiswa" class="form-control" value="<?= $pr['id_siswa'] ?>"  />					  
                    </div>
					<div class="form-group">
                        <label>Password</label>
                      <input  type="text" class="form-control" name="pass" value="<?= $pr['password'] ?>" required />  	                                 
                    </div>
					<div class="form-group">
                        <label>Ambil Photo</label>
                      <input name="MAX_FILE_SIZE" type="hidden" value="3000000" />  
	                                  <input name="file" class="form-control" type="file" accept="image/*" capture / required>
                    </div>
					</div>
					</div>
					<div class="modal-footer">					
                  <button name="gantiphoto" type="submit" class="btn btn-success" > Ubah Profil</button>				  
                </div>
                    </form>
            </div>
              </div>
            </div>
        </div>
 

   <?php            
            if (isset($_POST['gantiphoto'])) {
				function compressImage($source, $destination, $quality) { 
   
    $imgInfo = getimagesize($source); 
    $mime = $imgInfo['mime']; 
  
    switch($mime){ 
        case 'image/jpeg': 
            $image = imagecreatefromjpeg($source); 
            break; 
        case 'image/png': 
            $image = imagecreatefrompng($source); 
            break; 
        case 'image/gif': 
            $image = imagecreatefromgif($source); 
            break; 
        default: 
            $image = imagecreatefromjpeg($source); 
    } 
	 imagejpeg($image, $destination, $quality); 
     
   
    return $destination; 
} 
$pass=$_POST['pass'];
 $guru = $_SESSION['id_siswa'];
	$foto = $_FILES['file']['name'];
	$tmp = $_FILES['file']['tmp_name'];
	$fotobaru = $guru.'.jpg';
	$path = "foto/fotosiswa/".$fotobaru;
     
				
	$query = "SELECT * FROM siswa WHERE id_siswa='".$guru."'";
		$sql = mysqli_query($koneksi, $query); 
		$data = mysqli_fetch_array($sql);

		if(is_file("foto/fotosiswa/".$data['foto'])) 
			unlink("foto/fotosiswa/".$data['foto']); 
		 $compressedImage = compressImage($tmp, $path, 32);
	mysqli_query($koneksi,"UPDATE siswa SET foto='$fotobaru',password='$pass' WHERE id_siswa='$guru'");

	echo "
	  <script type='text/javascript'>
		setTimeout(function () { 	
			swal({
				title: 'Sukses !!!',
				text:  'Photo Profil Berhasil diubah',
				type: 'success',
				timer: 2000,
				showConfirmButton: true
			});		
		},10);	
		window.setTimeout(function(){ 
			window.location.replace('?pg=');
		} ,2000);	
	  </script>";
	
			}
            ?>