Resource Optimizer for PHP

There are many tools available to analyze performance of web pages, such as the popular YSlow tool from Yahoo!. In an effort to make it easy to perform some of the optimizations that these tools recommend, White Whale has created an open source ResourceOptimizer script for PHP, and we will soon be using it in our own CMS, LiveWhale.

What does ResourceOptimizer do?

The features that ResourceOptimizer implements include the following:

Is it fast?

A great deal of caching is involved in ResourceOptimizer to make it as fast as possible. There are three ways this is done, and all three are configurable.

Get The Code

Download ResourceOptimizer

To install:

How To Use It

Using the ResourceOptimizer is simple:

include '/path/to/class.resource_optimizer.php'; // include ResourceOptimizer
$ro=new ResourceOptimizer('/web/relative/path/to/resource_optimizer'); // initialize ResourceOptimizer
$page_source=$ro->optimizeResources($page_source); // optimize resources

You may wish to run this code before caching a page, or run it on-the-fly within an output buffer. Additional parameters include:

$ro->CDNS=array('cdn1.domain.com','cdn2.domain.com'); // supply an array of CDNs
$ro->url_cache_ttl=3600; // configure the url cache TTL
$ro->resource_cache_ttl=2592000; // configure the resource cache TTL
$ro->head_cache_ttl=300; // configure the head cache TTL
$ro->enable_deferred_load=false; // disable deferred loading (not recommended)

Make sure to also consider the following commands:

$ro->checkInstallation(); // check for installation issues (errors are set in $ro->error)
$ro->cleanCache(); // should be run periodically to purge old cache entries (for example, via a cron script)
$ro->purgeCache(); // clears all url and head cache entries, forcing them all to immediately recache (for debugging)

You may wish to use the following attributes on resources to control how ResourceOptimizer handles them:

// exclude a specific resource from aggregation:
<link href="/path/to/file.css" rel="stylesheet" type="text/css" data-no-aggregation="true"/>

// aggregate a specific resource but separate it from the previous aggregated resource:
<link href="/path/to/file.css" rel="stylesheet" type="text/css" data-aggregate-break="true"/>

Requirements: In order for ResourceOptimizer to parse the <head> element of your document, it must be able to be parsed as XHTML. The DOM extension for PHP is required for this. You must also have not disabled the "shell_exec" function in PHP (enabled by default). Optional minimization support requires availability of command-line Java on your server. When present, minimization will automatically become active.

Share It

Facebook   Del.icio.us   Digg   Newsvine   Reddit   StumbleUpon   Technorati

Your Feedback Requested

You're free to use this code in any way you please, but I would like to hear back from the PHP community about this tool. Please give it a try, and then use the address below to provide feedback.

Contact Us

"Resource Optimizer" for PHP is written by Alexander Romanovich (alex@whitewhale.net).