I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 8132
41749 credits Members referred : 3
« on: Oct 27, 2006, 07:51:21 PM »
Recently I discovered that when a user uploads a custom image for avatar, the SMF see this image as an attachment.
So any time that the avatar is loaded a secondary php process will be opened, load all the non usefull things - users, functions, actions, etc - , will execute some queries, just to display an avatar!
The modification bellow will override the attachment thing, so it will simply display the image path, which is many times faster.
The only requirement for this to work is to hold your attachment directory in a visible from the web directory - actually a sub directory of the board index - which in most SMF installations is that way (even if it is not the script will tell you ).
So here we are. Just create a php file with the above code, and upload it to your forum's root directory. Execute it and everything will be fine.
Code:
<?php //Author : Nick Papanotas //Website : http://www.webdigity.com/ //Optimize SMF by using static avatars
set_time_limit(100); include './Settings.php';
$db_con = @mysql_connect($db_server, $db_user, $db_passwd) or die('Cant connect to DB');
$res = mysql_db_query($db_name,"SELECT value FROM {$db_prefix}settings WHERE variable = 'attachmentUploadDir' LIMIT 1", $db_con); $attachDir = array_pop(mysql_fetch_array($res)); //Is it public? if ( substr($attachDir, 0, strlen($boarddir)) == $boarddir ) { $url = $boardurl . substr($attachDir, strlen($boarddir)) . '/'; $res = mysql_db_query($db_name,"SELECT filename,ID_MEMBER FROM {$db_prefix}attachments WHERE ID_MEMBER > 0", $db_con); $avatars = array(); while ( $rec = mysql_fetch_array($res)) { $avatars[] = array( 'fn' => $rec['filename'], 'id' => $rec['ID_MEMBER'] ); } if ( count($avatars) == 0 )die ('Nothing to do here....'); foreach ( $avatars as $rec ) { mysql_db_query($db_name,"UPDATE {$db_prefix}members SET `avatar` = '".$url.$rec['fn']."' WHERE ID_MEMBER = ".$rec['id']." LIMIT 1", $db_con); } echo 'Done with '.count($avatars).' avatars'; } else die('Propably this is not a public directory. I quit'); ?>