| Linux premium274.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64 Path : /home/whagcoha/ledger.mwagalwaservices.com/ |
| Current File : /home/whagcoha/ledger.mwagalwaservices.com/profileup.php |
<?php
if ((!isset($_SESSION['gen256'])) ) {
echo "<script>
window.open('login.php','_self');
</script>";
}
?>
<?php
$user_id = $_SESSION['gen256']['id']; // logged in user
// Fetch current image
$result = mysqli_query($conn, "SELECT image FROM users WHERE id='$user_id'");
$user = mysqli_fetch_assoc($result);
$current_image = $user['image'] ?? 'default.png';
// Upload new profile picture
if (isset($_POST['upload']) && isset($_FILES['profile_pic']['name'])) {
$file = $_FILES['profile_pic'];
// Allowed types
$allowed = ['jpg', 'jpeg', 'png', 'gif'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (in_array($ext, $allowed)) {
// Unique filename
$newName = uniqid('profile_', true) . "." . $ext;
$uploadPath = "img/" . $newName;
// Resize and compress
$maxWidth = 500;
$maxHeight = 500;
list($width, $height) = getimagesize($file['tmp_name']);
$ratio = min($maxWidth / $width, $maxHeight / $height);
$newWidth = (int)($width * $ratio);
$newHeight = (int)($height * $ratio);
// Create image resource
switch ($ext) {
case 'jpg':
case 'jpeg':
$src = imagecreatefromjpeg($file['tmp_name']);
break;
case 'png':
$src = imagecreatefrompng($file['tmp_name']);
break;
case 'gif':
$src = imagecreatefromgif($file['tmp_name']);
break;
default:
$src = null;
}
if ($src) {
$dst = imagecreatetruecolor($newWidth, $newHeight);
// Keep transparency
if ($ext == 'png' || $ext == 'gif') {
imagecolortransparent($dst, imagecolorallocatealpha($dst, 0, 0, 0, 127));
imagealphablending($dst, false);
imagesavealpha($dst, true);
}
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// Save compressed
if ($ext == 'jpg' || $ext == 'jpeg') {
imagejpeg($dst, $uploadPath, 75);
} elseif ($ext == 'png') {
imagepng($dst, $uploadPath, 7);
} elseif ($ext == 'gif') {
imagegif($dst, $uploadPath);
}
imagedestroy($src);
imagedestroy($dst);
// Delete old image
if ($current_image !== 'default.png' && file_exists("img/" . $current_image)) {
unlink("img/" . $current_image);
}
mysqli_query($conn, "UPDATE users SET image='$newName' WHERE id='$user_id'");
$current_image = $newName;
echo "<div class='alert alert-success'>Profile picture updated and compressed!</div>";
} else {
echo "<div class='alert alert-danger'>Could not process image.</div>";
}
} else {
echo "<div class='alert alert-danger'>Invalid file type. Use JPG, PNG, or GIF.</div>";
}
}
// Reset
if (isset($_POST['reset'])) {
if ($current_image !== 'default.png' && file_exists("img/" . $current_image)) {
unlink("img/" . $current_image);
}
mysqli_query($conn, "UPDATE users SET image='default.png' WHERE id='$user_id'");
$current_image = 'default.png';
echo "<div class='alert alert-info'>Profile picture reset to default.</div>";
}
?>
<h3 class="mb-3">Update Profile Picture</h3>
<div class="card p-3" style="max-width:1200px;">
<div class="text-center mb-3">
<img src="img/<?php echo htmlspecialchars($current_image); ?>"
class="rounded-circle border"
width="150" height="150" alt="Profile Picture">
</div>
<form method="POST" enctype="multipart/form-data" class="mb-2">
<input type="file" name="profile_pic" class="form-control mb-2" required>
<button type="submit" name="upload" class="btn btn-primary w-100">Upload New Picture</button>
</form>
<form method="POST">
<button type="submit" name="reset" class="btn btn-danger w-100">Remove & Reset to Default</button>
</form>
</div>