View Single Post
Old 04-28-08, 04:00 AM   #2
NaterGator
Dive on in...
 
NaterGator's Avatar
 
Join Date: Oct 2006
Location: Tampa/Gainesville, FL
TLCA# 18767
Posts: 1,248
Woody,
Here is a fairly painless way to cut down on at least one of the uncacheable server requests: the rotating title image. (No, I'm not saying don't rotate it!) I've seen the source of the rotate.php script that serves the images up, but to be honest it in itself is overkill, and in this case it is not the best approach at all. Why? Because the browser can't ever cache those same images (since, to it, they all originate from rotate.php and Apache doesn't allow it to be cached anyways) so every time the user visits a different page on Mud their browser sends a separate request to the webserver for the contents of rotate.php (which, obviously, change every time it asks for them)

But why have rotate.php in the first place? You're already serving a page from a server side scripting engine which can conjure up dynamic content. Instead, actually insert the relative URI to one of the real image itself. The browser won't care that it changes from page to page; once it's loaded each of them once it will just use them from cache. Doesn't seem like a whole lot from the get-go, but in 100 page views it's approaching a megabyte of unsent data!

So, without further adieu here what you do

Login to your admin cp and go down to "Plugins and Products". Select "Add new plugin". For the product select vBulletin. For the hook (this is really important) select parse_templates. Name it whatever you want, something like "Cacheable banner rotation". Set the execution order to 0. For the code, just copy and paste what's below. Leave everything (even the rotate.php script and the setting in vBulleting about the banner image path) alone. If at any time this code fails, it will fall back to that old method. (K.I.S.S.)
Code:
function hasExt( $file, $ext = '.jpg' ) {
     if( strrev( substr( strrev( $file ), 0, strlen( $ext ) ) ) == $ext )
          return true;
     else
          return false;
}
$images = array_filter( scandir( 'titleimage' ), 'hasExt' );
$stylevar['titleimage'] = 'titleimage/' . $images[array_rand( $images )];
There is a callback function which is used by array_filter() to select only files with a .jpg extension. The array is populated with the contents of the titleimage folder, and then cleaned by the callback function. Then, the style variable is overwritten (it is set to 'titleimage/rotate.php' when it is overwritten) to a random jpg image from the titleimage folder (it operates on the same principle as the rotate.php script so that saving a jpg in that folder instantly adds it into the rotation).

All you gotta do then is select "Yes" to make the plugin active and hit Save. Viola, people will start caching the random images and if Firebug is to be believed a average of a 540ms will be cut from the page load time This should persist through future forum upgrades too.



__________________
'96 LX450, 33" Revos, OME lifted, etc, etc
Love the life you live, live the life you love. -Bob Marley
Selfishness is not living as one wishes to live, it is asking others to live as one wishes to live. -Oscar Wilde

Firefox users, get your youtube on!
NaterGator is offline   Reply With Quote