This is a continuation of this https://torrenttrader.uk/post/3317 You can upload an avatar to your sever directly rather than using a weblink to the image, which Is removed from this code. Images are converted to WebP using the GD library that your server already has, which is better than PNG or JPG. This code will not allow GIF, because thats just a bad idea anyway.
In account.php under edit_settings. Put this wherever you like.
Then create a file called upavatar.php and put it in the root folder of your site
in backend/config add (you may have a few of these lines, so just replace it)
You will also need this in your config. I use defines. Before the array starts $site_config = [];
I attached a default_avatar.webp to replace the png that you probably have. Add this image to /avatars folder in the root as this code looks there for avatars, not the images folder
In account.php under edit_settings. Put this wherever you like.
/// AVATAR UPLOAD ///
if ($site_config['AVATARUPLOAD']) {
$default_avatar = $site_config['SITEURL'] . '/avatars/default_avatar.webp';
$avatar = $CURUSER['avatar'];
if (empty($avatar)) {
$avatar = $default_avatar;
}
$show_remove = ($avatar !== $default_avatar);
$avatar = htmlspecialchars($avatar, ENT_QUOTES, 'UTF-8');
$max_avatar_size = (int)$site_config['avatar_max_filesize'];
if ($max_avatar_size >= GB) {
$max_avatar_size_text = round($max_avatar_size / GB, 2) . ' GB';
} elseif ($max_avatar_size >= MB) {
$max_avatar_size_text = round($max_avatar_size / MB, 2) . ' MB';
} else {
$max_avatar_size_text = round($max_avatar_size / KB, 2) . ' KB';
}
print("
<tr>
<td align='right' class='table_col1'><b>" . T_('AVATAR') . ":</b></td>
<td align='left' class='table_col1'>
<table>
<tr>
<td>
<img src='{$avatar}' width='40' height='40' alt='Avatar'>
</td>
<td>
<a href='upavatar.php?action=upload'><b>" . T_('UP_AVATAR') . "</b></a>"
. ($show_remove
? " | <a href='upavatar.php?action=remove'><b>Remove Avatar</b></a>"
: ""
) . "
<br>
<small>
Images are converted to WebP ยท Max size: {$max_avatar_size_text}
</small>
</td>
</tr>
</table>
</td>
</tr>
");
}
/// END UPLOAD AVATAR ///Then create a file called upavatar.php and put it in the root folder of your site
<?php
require_once "backend/functions.php";
dbconn();
loggedinonly();
$action = $_GET['action'] ?? '';
if (!$site_config['AVATARUPLOAD']) {
stdhead("Error");
write_log("<b>DIRECT FILE ACCESS</b> - User <b>{$CURUSER['username']}</b> tried to upload avatar while disabled");
show_error_msg("Error", "Avatar uploading is disabled by the site admin!", 1);
stdfoot();
exit;
}
$maxWidth = 80;
$maxHeight = 80;
$maxSize = (int)$site_config['avatar_max_filesize'];
$avatarDir = __DIR__ . '/' . trim($site_config['avatar_dir'], '/');
$avatarUrl = rtrim($site_config['SITEURL'], '/') . '/' . trim($site_config['avatar_dir'], '/');
if (!extension_loaded('gd')) {
show_error_msg("Server error", "PHP GD extension is not installed", 1);
}
$gdInfo = gd_info();
if (empty($gdInfo['WebP Support'])) {
show_error_msg("Server error", "GD WebP support is required for avatars", 1);
}
if ($action === 'post') {
if (!isset($_FILES['avatar']) || $_FILES['avatar']['error'] !== UPLOAD_ERR_OK) {
autolink ("upavatar.php?action=upload", "<b><font color='#ff0000'>No file selected...</font></b>");
}
if ($_FILES['avatar']['size'] > $maxSize) {
show_error_msg(
"Invalid file size",
"Image must be less than " . mksize($maxSize),
1
);
}
if (!is_dir($avatarDir) || !is_writable($avatarDir)) {
show_error_msg("Server error", "Avatar directory is not writable", 1);
}
$imageInfo = getimagesize($_FILES['avatar']['tmp_name']);
if ($imageInfo === false) {
write_log("<b>DIRECT FILE ACCESS</b> - User <b>{$CURUSER['username']}</b> uploaded non-image file");
show_error_msg("Invalid file", "Uploaded file is not a valid image", 1);
}
$allowedTypes = $site_config['avatar_allowed_types'] ?? [];
if (!isset($allowedTypes[$imageInfo[2]])) {
autolink(
"upavatar.php?action=upload",
"<b><font color='#ff0000'>Invalid type. Allowed formats: " .
implode(', ', $allowedTypes) .
"</font></b>"
);
}
switch ($imageInfo[2]) {
case IMAGETYPE_JPEG:
$srcImage = imagecreatefromjpeg($_FILES['avatar']['tmp_name']);
break;
case IMAGETYPE_PNG:
$srcImage = imagecreatefrompng($_FILES['avatar']['tmp_name']);
break;
case IMAGETYPE_WEBP:
$srcImage = imagecreatefromwebp($_FILES['avatar']['tmp_name']);
break;
default:
show_error_msg("Error", "Unsupported image format", 1);
}
if (!$srcImage) {
show_error_msg("Error", "Failed to process image", 1);
}
$srcWidth = $imageInfo[0];
$srcHeight = $imageInfo[1];
$scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight, 1);
$newW = (int)floor($srcWidth * $scale);
$newH = (int)floor($srcHeight * $scale);
$dstImage = imagecreatetruecolor($newW, $newH);
/* Preserve transparency */
imagealphablending($dstImage, false);
imagesavealpha($dstImage, true);
imagecopyresampled(
$dstImage,
$srcImage,
0, 0, 0, 0,
$newW, $newH,
$srcWidth, $srcHeight
);
/* Save as WebP */
$avatarName = (int)$CURUSER['id'] . '.webp';
$avatarPath = $avatarDir . '/' . $avatarName;
if (!imagewebp($dstImage, $avatarPath, 80)) {
imagedestroy($srcImage);
imagedestroy($dstImage);
show_error_msg("Error", "Failed to save avatar image", 1);
}
imagedestroy($srcImage);
imagedestroy($dstImage);
$avatarFullUrl = $avatarUrl . '/' . $avatarName;
SQL_Query_exec(
"UPDATE users SET avatar = " . sqlesc($avatarFullUrl) . " WHERE id = " . (int)$CURUSER['id']
);
stdhead("Upload Avatar");
begin_frame(T_("UP_AVATAR"));
print("
<br>
<p class='text-center'>
" . T_("AVATAR_OK") . "
<a href='account.php?action=edit_settings&do=edit'><b>" . T_("ACCOUNT") . "</b></a>
</p>
");
end_frame();
stdfoot();
exit;
}
if ($action === 'upload') {
stdhead("Upload Avatar");
begin_frame(T_("UP_AVATAR"));
$allowedFormatsText = implode(', ', $site_config['avatar_allowed_types']);
print("
<form method='post' action='?action=post' enctype='multipart/form-data'>
<table class='table_col1' align='center' width='550' cellpadding='5'>
<tr>
<td class='table_col2' align='right'><b>" . T_("AVATAR_FILE") . ":</b></td>
<td class='table_col1'><input type='file' name='avatar' size='60'></td>
</tr>
<tr>
<td class='table_col2' align='right'><b>" . T_("DISCLAIMER") . ":</b></td>
<td class='table_col1'>
" . T_("AVATAR_TXT") . " " . mksize($maxSize) . "<br>
<small>Allowed formats: {$allowedFormatsText}</small>
</td>
</tr>
<tr>
<td class='table_col1' colspan='2' align='center'>
<input type='submit' value='" . T_("UP_AVATAR") . "' class='btn'>
</td>
</tr>
</table>
</form>
");
end_frame();
stdfoot();
exit;
}
if ($action === 'remove') {
$default_avatar = rtrim($site_config['SITEURL'], '/') . '/avatars/default_avatar.webp';
// Fetch current avatar
$res = SQL_Query_exec(
"SELECT avatar FROM users WHERE id = " . (int)$CURUSER['id']
);
$row = mysqli_fetch_assoc($res);
$current_avatar = $row['avatar'] ?? '';
// If empty or already default, nothing to remove
if (empty($current_avatar) || $current_avatar === $default_avatar) {
autolink(
"account.php?action=edit_settings&do=edit",
"<b>Nothing to remove.</b>"
);
exit;
}
// Convert URL to local filesystem path
$avatar_dir = realpath(__DIR__ . '/' . trim($site_config['avatar_dir'], '/'));
$avatar_path = parse_url($current_avatar, PHP_URL_PATH);
if (!$avatar_path) {
show_error_msg("Error", "Invalid avatar path", 1);
}
$full_path = realpath($_SERVER['DOCUMENT_ROOT'] . $avatar_path);
// safety check to prevent directory traversal
if (
!$full_path ||
!$avatar_dir ||
strpos($full_path, $avatar_dir) !== 0 ||
!is_file($full_path)
) {
write_log("<b>SECURITY:</b> Avatar delete blocked for user {$CURUSER['username']}");
show_error_msg("Error", "Invalid avatar file", 1);
}
// Delete avatar
@unlink($full_path);
// Reset avatar
SQL_Query_exec(
"UPDATE users SET avatar = " . sqlesc($default_avatar) . " WHERE id = " . (int)$CURUSER['id']
);
autolink(
"account.php?action=edit_settings&do=edit",
"<b>Avatar removed successfully.</b>"
);
exit;
}
stdhead("Error");
write_log("<b>DIRECT FILE ACCESS</b> - User <b>{$CURUSER['username']}</b> tried to access upavatar.php directly");
print("
<br>
<p class='text-center'><b>Direct access not allowed!</b></p>
<p class='text-center'>
<a href='account.php?action=edit_settings&do=edit'><b>" . T_("ACCOUNT") . "</b></a>
</p>
<br>
");
stdfoot();in backend/config add (you may have a few of these lines, so just replace it)
// Avatar Configuration (avatars are converted to WebP format in upavatar.php)
$site_config['AVATARUPLOAD'] = true; // Enable/disable avatar uploads
$site_config['avatar_dir'] = '/avatars'; // Avatar storage directory (chmod 777)
$site_config['avatar_max_filesize'] = 3 * MB; // Maximum avatar file size
$site_config['avatar_webp_quality'] = 80; // WebP avatar quality (0-100)
$site_config['avatar_allowed_types'] = [ // Permitted avatar image formats
IMAGETYPE_JPEG => 'JPG',
IMAGETYPE_PNG => 'PNG',
IMAGETYPE_WEBP => 'WebP',
];
You will also need this in your config. I use defines. Before the array starts $site_config = [];
// File Size Constants - DO NOT MODIFY
define('KB', 1024);
define('MB', 1024 * KB);
define('GB', 1024 * MB);
// Time Constants - DO NOT MODIFY
define('MINUTE', 60);
define('HOUR', 60 * MINUTE);
define('DAY', 24 * HOUR);
define('WEEK', 7 * DAY);I attached a default_avatar.webp to replace the png that you probably have. Add this image to /avatars folder in the root as this code looks there for avatars, not the images folder
TTv3 Micro Edition
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Last edited by MicroMonkey on 2026-01-10 22:10:16