Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
Upload an avatar
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Mods Completed

2026-01-03 23:31:16
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.
/// 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
                               ? " &nbsp;|&nbsp; <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
default_avatar.zip
Download

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)
Last edited by MicroMonkey on 2026-01-10 22:10:16
2026-01-03 23:59:48
You may need this in your login_block also
    function getUserAvatar($userAvatar, $siteUrl) {
       $avatar = htmlspecialchars($userAvatar);
       return $avatar ?: "$siteUrl/avatars/default_avatar.webp'";
   }
   $avatar = getUserAvatar($CURUSER["avatar"], $site_config["SITEURL"]);

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)
2026-01-07 19:51:22
This seems to work for converting existing avatars to WebP. Just save it as avatarfix.php in your root and execute it. This will nullify any existing GIF images and only save the first frame as WebP
<?php
require_once __DIR__ . '/backend/functions.php';

dbconn();
loggedinonly();

if ($CURUSER['class'] < 8) {
   show_error_msg("Access denied", "Admins only.", 1);
}

$avatarDir = __DIR__ . '/' . trim($site_config['avatar_dir'], '/');
$avatarUrl = rtrim($site_config['SITEURL'], '/') . '/' . trim($site_config['avatar_dir'], '/');

$maxWidth  = 80;
$maxHeight = 80;
$quality   = (int)($site_config['avatar_webp_quality'] ?? 80);

$batchSize = 10; // avatars per load time

if (!is_dir($avatarDir) || !is_writable($avatarDir)) {
   show_error_msg("Server error", "Avatar directory not writable", 1);
}

$res = SQL_Query_exec("
   SELECT id, avatar
   FROM users
   WHERE avatar != ''
     AND avatar NOT LIKE '%.webp'
   LIMIT {$batchSize}
");

stdhead("Avatar Conversion");
begin_frame("Avatar Conversion");

if (mysqli_num_rows($res) === 0) {
   print("<p class='text-center'><b>All avatars have been converted</b></p>");
   print("<p class='text-center'>You can now safely delete this file.</p>");
   end_frame();
   stdfoot();
   exit;
}

print("<ul>");

while ($row = mysqli_fetch_assoc($res)) {

   $userId = (int)$row['id'];
   $oldUrl = $row['avatar'];
   $oldFile = basename($oldUrl);
   $oldPath = $avatarDir . '/' . $oldFile;

   if (!is_file($oldPath)) {
       print("<li>User {$userId}: file missing</li>");
       continue;
   }

   $info = getimagesize($oldPath);
   if ($info === false) {
       print("<li>User {$userId}: invalid image</li>");
       continue;
   }

switch ($info[2]) {
   case IMAGETYPE_JPEG:
       $src = imagecreatefromjpeg($oldPath);
       break;

   case IMAGETYPE_PNG:
       $src = imagecreatefrompng($oldPath);
       break;

   case IMAGETYPE_GIF:
       $src = imagecreatefromgif($oldPath);
       break;

   case IMAGETYPE_WEBP:
       print("<li>User {$userId}: already WebP</li>");
       continue 2;

   default:
       print("<li>User {$userId}: unsupported type</li>");
       continue 2;
}


   if (!$src) {
       print("<li>User {$userId}: failed to load image</li>");
       continue;
   }
   $srcW = imagesx($src);
   $srcH = imagesy($src);

   $scale = min($maxWidth / $srcW, $maxHeight / $srcH, 1);
   $newW  = (int)floor($srcW * $scale);
   $newH  = (int)floor($srcH * $scale);

   $dst = imagecreatetruecolor($newW, $newH);
   imagealphablending($dst, false);
   imagesavealpha($dst, true);

   imagecopyresampled(
       $dst, $src,
       0, 0, 0, 0,
       $newW, $newH,
       $srcW, $srcH
   );

   $newName = $userId . '.webp';
   $newPath = $avatarDir . '/' . $newName;

   if (!imagewebp($dst, $newPath, $quality)) {
       print("<li>User {$userId}: failed to save WebP</li>");
       imagedestroy($src);
       imagedestroy($dst);
       continue;
   }

   imagedestroy($src);
   imagedestroy($dst);

   SQL_Query_exec(
       "UPDATE users
        SET avatar = " . sqlesc($avatarUrl . '/' . $newName) . "
        WHERE id = {$userId}"
   );

   if ($oldFile !== $newName) {
       @unlink($oldPath);
   }

   print("<li>User {$userId}: converted</li>");
}

print("</ul>");

print("
   <p class='text-center'>
       <a href='avatarfix.php'><b>Continue next batch</b></a>
   </p>
");

end_frame();
stdfoot();

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)
Last edited by MicroMonkey on 2026-01-07 19:58:36
2026-01-10 08:46:00
Bonjour
Impeccable  ๐Ÿ‘
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 3
  • Members: 0
  • Guests online: 3
  • Comments: 3
  • Messages: 1,728