• Jan
  • 21

Improve website load time by 500% with 3 lines of code

Major New Update - PHP SPEEDY: latest version available here

PHP Speedy
----------------

There are 4 relatively easy ways by which you can speed up the time it takes a browser to download a page:

  • Make fewer HTTP requests
  • Add a far-future expires header
  • Gzip your page's components
  • Minify your JavaScript, CSS and HTML

Following on from my post on joining CSS and JavaScript files, I have written a PHP script which will automatically do all of the above. All you have to do is call the following at the top of your page:

The code

PHP:
require_once('class.compressor.php'); //Include the class. The full path may be required
$compressor = new compressor('css,javascript,page');

And the following at the bottom of the page:

PHP:
$compressor->finish();

The Demo

That's it! Before I go into the details, the impatient amongst you can check out the test pages here:

The class can be downloaded here: site speed boost class download

Requirements

  • A server running at least PHP4. For JavaScript minification to work as well, PHP5+ is required.
  • You should set the folder where you are running the class from as writable

Setting basic options

Options can be set with a comma separated string as follows:

PHP:
$compressor = new compressor('css,javascript,page');

The string can contain 'css', 'javascript' and 'page' depending on which elements you would like to be compressed. Any element contained in the string will be gzip encoded and minified.

Setting advanced options

Alternatively, an array can be passed to the class constructor with an advanced set of options. This array would set all the compression options to on:

PHP:
$compressor = new compressor(array("javascript"=>array("cachedir"=>'/minify',
"gzip"=>true,
"minify"=>true,
),
"css"=>array("cachedir"=>'/wp-content',
"gzip"=>true,
"minify"=>true,
),
"page"=>array("gzip"=>true,
"minify"=>true
)
));

You can change this array to add or remove all the available options.

Note that "cachedir" should not include a trailing slash. If your CSS files contain relative links for background images, you should set the CSS cachedir to the same directory the standard CSS files are stored in.

Speed gains

On my test page the load time with the compressor turned on was generally below second, and without the compressor between 3-5 seconds. The Yahoo Yslow rating went from F(45) to A(97).

Test page with Firebug, before compression

Firebug before compression

Test page with Firebug, after compression

Firebug before compression

Test page with Yslow, before compression

Firebug before compression

Test page with Yslow, after compression

Firebug before compression

Download

Please see the updated page

Credits

The JavaScript minification uses jsmin-php.

Subscribe

If you liked this tutorial, please subscribe to my RSS feed for more of the same.


Related posts

Posted by admin in php, tutorials
trackback
 

  • Jan
  • 21
Jeromy
Jeromy

Great stuff man! I'm trying to use this on a wordpress site, adding the initial code to the header.php and the finish() code to the footer.php - but I'm getting this error:

Fatal error: Call to a member function finish() on a non-object in /xxxx/web/content/wp-content/themes/ExperienceDynamics/footer.php on line 37

  • Jan
  • 21
admin
Leon

Hi Jeromy,

I tried it out on a wordpress theme. I stored the files in a 'minify' subdirectory, and added this to the index.php at the top:

PHP:
require_once($_SERVER['DOCUMENT_ROOT'].'/minify/class.compressor.php');
$compressor = new compressor('css,javascript,page');
$compressor->start();

and this at the bottom

PHP:
$compressor->finish();

it worked fine.

  • Jan
  • 21
Johnny
Johnny

There is some problem where u use rewrite - script try to fetch ie [controller_name]/[action]/styles/style.css - but declaration in the script is /styles/style.css

  • Jan
  • 21
admin
Leon

Johnny - I'm not sure exactly what you mean, but there are two things I can think of that may help:

1. Try setting the full path to the style sheet when you declare the link E.g:

CODE:
<link href="/styles/style.css" rel="stylesheet" type="text/css" />

2. Configure the compressor with the cachedir option. Eg:

PHP:
$compressor = new compressor(array("javascript"=>array("cachedir"=>'/js',
"gzip"=>true,
"minify"=>true,
),
"css"=>array("cachedir"=>'/styles',
"gzip"=>true,
"minify"=>true,
),
"page"=>array("gzip"=>true,
"minify"=>true
)
));

  • Jan
  • 21
James
James

A 100% decrease in load time would be a load time of zero. You can't decrease something by 500%. Think about it....

  • Jan
  • 21
Bil
Bil

I'm not seeing how this script improved your CDN and ETags grade... Am I missing something?

  • Jan
  • 21
admin
Leon

James - true, but I think I can get away with it. I don't actually say 500% of what, so I could be talking about 500% of the resulting load time. New title:

Decrease web site load time by 500%* in 3 lines of code
(* of resulting compressed load time)

;-)

  • Jan
  • 21
admin
Leon

@Bill - good spot, on the demo the Etags gets an F because of the screen shot images I added. However, the compressed JavaScript and CSS files do pass the ETag test. As for the CDN, I hadn't spotted it got a B on the screenshots! It's because the testing server I use has a CDN, and because the script reduced the total number of files, that rating improved. But yes, you're right, my script has no effect on the CDN.

  • Jan
  • 21
stojce
stojce

Great stuff, but also need to distinct css media type. I have separate css's for screen and print, but your class puts them in one file without media attribute.

  • Jan
  • 21
Vidar Hokstad

This is interesting, but no matter how you try to explain it saying it decreased the load time by 500% is meaningless. It reduced the load time by 80%.

  • Jan
  • 21
admin
Leon

stojce - good point, thanks. Now I have something to add for version 0.2 of the class. I'll get onto it tomorrow. Time for bed now (midnight Spanish time)...

  • Jan
  • 21
admin
Leon

Vidar - you're right, of course. OK, it's starting to bother me now seeing decreased/500% up there. Title changed! :-)

  • Jan
  • 21
Ian Stewart

Tried to get this working on a WordPress theme. No PHP errors but no improvement with YSlow.

Code above doctype in header.php…

define('THEMELIB', TEMPLATEPATH . '/library');
require_once(THEMELIB . '/compressor/class.compressor.php'); //Include the class. The full path may be required
$compressor = new compressor('css,javascript,page');

The folder compressor is set as writable as well and no change. I doubt it's 'cuz I write such elegant CSS. ;)

  • Jan
  • 21
louis w
louis w

wouldn't it be better to create the file name with the last mod date of each file instead of it's file size?

  • Jan
  • 21
Cahir

Has this ever been tested or included in the popular CMS 'Joomla' ?
I have set up a few sites and find that they tend to run very slowly due to large calling of scripts/css pages and the actual images on a page

Kind of annoying especially when im such a novice and Joomla enables me to freely set up mini websites but prohibits me from easily optimising them

  • Jan
  • 22
k.r
k.r

As a newbie..I am a little lost..

Is there any way you could show the code in a page... I tried the sample page, but I cant find it on that page. Does it recreate my code..or do I add it in addition to my code? Sorry for sounding stupid...

K

  • Jan
  • 22
tristen
tristen

Is there really a need to nerd around the bush over the title to this post? Regardless of whether it ACTUALLY decreases load time by %500 is irrelevant. It was exciting enough of a title for me to read it / check out your website, and I'm glad I did. Great post!

  • Jan
  • 22
Jonathan Moore

Great performance boost! The only potential issue I see with the way it optimizes is the fact that it combines CSS marked for media="screen" and media="print" into one compressed file. If the various media types could be split out into individual archives I would include this optimization in virtually every site I work on.

Just added the optimization to... http://www.newezra.com

  • Jan
  • 22
jameszol

Nice work! I can't wait to test this out myself on advertisements - thank you!

  • Jan
  • 22
Leandro

great, anyone tried with drupal sites?
Or maybe a Drupal module would be great.

  • Jan
  • 22
TooCoolForYou

Hi, first at all thx for share ur knowledge :)
I'm going to test this in one proyect that i got. I'll post the results later.

  • Jan
  • 22
trish

WoW

  • Jan
  • 22
Kolyas
Kolyas

I have to agree with James, 100% decrease in load time would be load time of zero, 500% means that the page was loaded 4 times before I even though of loading it ... one fifth of loading time is equivalent of 100% / 5 * 4 = 80% decrease in load time ...

Excellent tip though!

  • Jan
  • 22
Brian

Doesn't mod_gzip do the exact same thing as this?

  • Jan
  • 22
Jonas
Jonas

Cool! Has anyone seen anything like this but with aspx?

  • Jan
  • 22
Neil

Thanks for this, Leon. I need to find the time to test it on a Wordpress site with a hefty chunk of plugins. WP-Cache has helped, but there's always room for improvement!

Your Y-Slow results suggest you're using several of the YUI components - how are you finding those? I'm a big fan of reset.css. Wouldn't start a site without it.

  • Jan
  • 22
stefan
stefan

Hi. Thanks for that. I'm a little bit confused of implementing it to my site.

am i able to exclude some css files if i have an extra css file for safari browsers? wouldn't be great if the script puts it together with the "original" css file...

  • Jan
  • 22
admin
Leon

Thanks for all the comments, I'm glad this has provoked some interest. To address your comments:

@Ian: I would try putting it at the top and bottom on the index.php file from your theme. You can tell if it's worked by looking at the resulting source code. You should see something like this:

PHP:
<script type="text/javascript" src="http://aciddrop.com/aciddrop/minify/_cmp_javascript_47c609799555c9b22d3c2704399e1075.php"></script>   
<link type="text/css" href="http://aciddrop.com/aciddrop/minify/_cmp_css_c42690f5b13d8daa0cb62ac82481bd43.php" rel="stylesheet" />

@Louis: I did it by file size merely so I could gather some statistics on the size reduction when I was putting it together. In theory though, yes, you're right: although it doesn't happen often, it's possible for the file to change without the file size changing. SOmething for V 0.2, thanks.

@Cahir: I haven't tried this with Joomla. Why not give it a shot and let me know how you got on?

@k.r: Sure. I have zipped up the entire test page for you. Look at the 'compress_me.php' file to see how it's compressed: http://aciddrop.com/aciddrop/aciddrop_compression_test_page.zip

@Jonathan Moore - true, I'm going to add in media recognition for V 0.2, thanks.

@Brian, mod_gzip doesn't join files together to reduce the number of HTTP requests, it doesn't minify, and it doesn't add a far-future expires header to the files. It just gzips.

@Neil - Yes, I like the YUI CSS templates, they've come in very handy. I use http://developer.yahoo.com/yui/fonts/ in every site, for example.

@Stefan - at the moment you can't exclude a CSS file, but that something I could add to v0.2, thanks. You can save the compressed file in the same directory as the original CSS file by setting the "cachedir" option. Have a look at the "settting advanced options" section above.

  • Jan
  • 22
Roberto
Roberto

Great tut, thanks!

  • Jan
  • 22
Quinton

OR

instead of all this you could simply enable Apache mod_deflate module (to minify those js, css and any other plain-text files) on ur behalf

and enable php's zilb compression when ensures PHP output to browser is gzipped

  • Jan
  • 22
admin
Leon

Quinton - mod_deflate will just gzip. It won't minify, it won't combine the files, and it won't add expires headers (with automatic file versioning). Plus, it can be a bit fiddly (requires changing httpd.conf etc). The point of this script is to offer a three line solution that simultaneously performs 4 speed boosters.

  • Jan
  • 22
tom rojers
tom rojers

i haven't tried it, but this looks good.

just one request for the next version: can it be made possible to specify a version number to be appended to the file names that are generated. that way when a new compressed file is generated proxies will retrieve the new version since it won't have the far-future expires header of the old file (if you've set them up).

thanks

  • Jan
  • 22
Jeff Badger
Jeff Badger

May want to look at the use of strstr at line 400. The arguments are in the worng order - its haystack, needle for this one. Its causing me some errors as it is.

  • Jan
  • 22
Redemption

Hi, i'm trying to install the on my site that code, I use wordpress platform. I've tryed the code that You insert in 2th comment, it doent any error, but not optimize anything...

Exuse for my poor english :/

  • Jan
  • 22
aNTraX

It dont work fine with external javascript. By example, I have this js in my page:

The script dont work for the first.

  • Jan
  • 22
filex

If it was a plugin, it would be very good. ;)

  • Jan
  • 23
admin
Leon

Thanks for all the comments, a new version is online here:
http://aciddrop.com/2008/01/23/site-speed-boost-script-updated/

@tom - whenever you change one of your JS or CSS files, a new compressed file with a new name is created. This means new versions will always be downloaded by the browser/proxy

@Jeff - many thanks, bug fix in new version

@Redemption - try the new version. If you don't have any luck email me with the code you're using and the source of the page (view source) after adding the class. My email is on the about page.

@antrax - true, it doesn't work for external JS. As a quick fix, just move the external link outside the tags.

@filex - you guessed the topic of my next post!

  • Jan
  • 23
Micah

How does your compression compare with something like YUI Compressor? I'm interested in yours because it sounds like a good solution but YUI Compressor is pretty well established by now.

  • Jan
  • 23
aNTRaX

I have corrected the code for support external js by chaning this code:

//Remove empty sources
foreach($script_array AS $key=>$value) {
preg_match("!" . $options['src'] . "=\"(.*?)\"!is", $value, $src);
if(!$src[1] || (strlen($src[1])>7 && strcasecmp(substr($src[1],0,7),'http://')==0)) {
unset($script_array[$key]);
}
}

and moving it under if(is_array($script_array)) { at line 306

  • Jan
  • 23
admin
Leon

@ Michah. I haven't used YUI Compressor, but the main advantage of my script is that it does it on the fly. You don't have to pass your JS and CSS via the command line - you just set it at the top and bottom of the page and forget it.

@antrax - thanks, I'll add that to the next version of the script.

  • Jan
  • 23
RUDE

I cant get the code working with my WordPress theme, could you please tell me what am I doing wrong?

I uploaded class.compressor.php and the lib folder to the template folder and then I wrote this at the top of header.php:

array("cachedir"=>TEMPLATEPATH.'/minify',
"gzip"=>true,
"minify"=>true,
),
"css"=>array("cachedir"=>TEMPLATEPATH.'/minify',
"gzip"=>true,
"minify"=>true,
),
"page"=>array("gzip"=>true,
"minify"=>true
)
));
?>

and this at the bottom of footer.php:

finish();
?>

And FWI, this is how I load the CSS and the JS on the template:

<link rel="stylesheet" href="/style.css" type="text/css">
<script src="/js/jquery.1.2.1.js" type="text/javascript" charset="utf-8">

  • Jan
  • 23
RUDE

<?
$compressor->finish();
?>

And FWI, this is how I load the CSS and the JS on the template:

<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/style.css" type="text/css">
<script src="<?php bloginfo('template_url'); ?>/js/jquery.1.2.1.js" type="text/javascript" charset="utf-8"></script>

  • Jan
  • 23
admin
Leon

@Rude = you should only edit index.php. At the top of index.php:

First include the script (I have used the TEMPLATEPATH wordpress variable in this case)

PHP:
require_once(TEMPLATEPATH.'/class.compressor.php');

Then set your script options

PHP:
$compressor = new compressor(array("javascript"=>array("cachedir"=>"/wp-content/themes/leon",
                                           "gzip"=>true,
                                           "minify"=>true,                            
                                           ),
                              "css"=>array("cachedir"=>"/wp-content/themes/leon",
                                       "minify"=>true,                                                      
                                           ),
                               "page"=>array("gzip"=>true,
                                             "minify"=>true
                                            )                                          
                              ));

Note you should put in the relative path to your theme for cachedir (not TEMPLATEPATH ).

Then at the bottom of index.php call:

PHP:
$compressor->finish();

  • Jan
  • 23
aNTRaX

And another thing, in the next version you should check if navigator accept gzip encoding. Some versions of IE dont work with class.compresor because it don't support these compression.

Something like this should work fine:

if(stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')!==false)
Compress();
else
NoCompress();

Thanks.

  • Jan
  • 23
RUDE

Ok, I tried editing index.php. First I included

require_once(TEMPLATEPATH.'/class.compressor.php');

and all the other stuff with the relative path for the cachedir, but it gives an error because of the TEMPLATEPATH. So I've set everything with relative paths, the result is this:

<?php
require_once('./wordpress/wp-content/themes/RUDEWORKS/class.compressor.php');
$compressor = new compressor(array("javascript"=>array(
"cachedir"=>"/wordpress/wp-content/themes/RUDEWORKS",
"gzip"=>true,
"minify"=>true,
),
"css"=>array(
"cachedir"=>"/wordpress/wp-content/themes/RUDEWORKS",
"gzip"=>true,
"minify"=>true,
),
"page"=>array(
"gzip"=>true,
"minify"=>true
)
));

/* Short and sweet */
define('WP_USE_THEMES', true);
require('./wordpress/wp-blog-header.php');

$compressor->finish();
?>

It looks like it didn't work, the js and css is remains untouched...

Note that my index.php is in the root of the domain instead of the wordpress folder, I don't know if this could be a problem.

Thanks anyway ;-)

  • Jan
  • 23
RUDE

Ok, I think I've choose the wrong index.php. I edited the one that is in the template and Safari gives a weird error...

  • Jan
  • 23
admin
Leon

RUDE - I've just updated to version 0.3. Please use that version from now on.
http://aciddrop.com/2008/01/23/site-speed-boost-script-updated/

Here's the the exact code I used to get this working with the default Wordpress template. Try to get this working first, then move onto your own template.

At the very top of index.php in /wp-content/themes/default:

PHP:
require_once(TEMPLATEPATH.'/class.compressor.php');
$compressor = new compressor(array("javascript"=>array("cachedir"=>"/wp-content/themes/default",
                                           "gzip"=>true,
                                           "minify"=>true,                            
                                           ),
                              "css"=>array("cachedir"=>"/wp-content/themes/default",
                                       "minify"=>true,                                                      
                                           ),
                               "page"=>array("gzip"=>false,
                                             "minify"=>true
                                            )                                          
                              ));

Then at the very bottom

PHP:
<?php $compressor->finish(); ?>

Note that I've set it not to gzip the page, so all errors will be displayed.

  • Jan
  • 23
Scott E Winklebleck

I can't get this to work in WP

PHP: require_once(TEMPLATEPATH.'/class.compressor.php'); $compressor = new compressor(array("javascript"=>array("cachedir"=>"/wp-content/themes/default", "gzip"=>true, "minify"=>true, ), "css"=>array("cachedir"=>"/wp-content/themes/default", "minify"=>true, ), "page"=>array("gzip"=>false, "minify"=>true ) ));

Just shows up on the top of my page.

  • Jan
  • 23
admin
Leon

Scott, if you can see it at the top of the page, then perhaps it's not in PHP tags? It should look like this at the top of the index.php that is in the /wp-content/themes/default folder:

PHP:
<?

The compressor code should then follow that tag, and you should close the tag afterwards. Also, don't forget to put

PHP:
<?php $compressor->finish(); ?>

at the bottom of the same page

  • Jan
  • 24
Benjamin
Benjamin

Hello,

My WP blog is in xyz.com/blog.
I have your script with :

CODE:
array("cachedir"=>"/wp-content/themes/Branches/cache",
                                           "gzip"=>true,
                                           "minify"=>true,                           
                                           ),
                              "css"=>array("cachedir"=>"/wp-content/themes/Branches/cache",
                                       "minify"=>true,                                                     
                                           ),
                               "page"=>array("gzip"=>false,
                                             "minify"=>true,
                                            )                                         
                              ));
$compressor->start();
?>

It work on articles (xyz.com/blog/01/01/01/title) , but not in the index page (xyz.com/blog).

CODE:
Warning: fopen(/home/zidane/www/wp-content/themes/branches/cache/_cmp_javascript_d41d8cd98f00b204e9800998ecf8427e.php)

but normay it must be : www/blog/wp-content and not www/wp-content.

have ou an idea ?

Thx

  • Jan
  • 24
admin
Leon

Benjamin - change cachedir to /blog/wp-content/themes/Branches/cache ?

  • Jan
  • 24
Benjamin
Benjamin

Yes but now it works on the Index, but thes articles says :

/www/blog/blog/wp-content/themes/Branches/cache/

blog/blog :/

  • Jan
  • 24
Skeku

Great tool!

But lot of warnings in my index:

CODE:
Warning: fopen(/usr/home/criteriondg.info/web/wp-content/themes/Criterion2/_cmp_javascript_9b2032426be7a2aed9d10d8baa02006c.php) [function.fopen]: failed to open stream: No such file or directory in /usr/home/criteriondg.info/web/wordpress/wp-content/themes/Criterion2/class.compressor.php on line 382

And bla bla bla. I think it's something about the path but no idea how to fix it.

Articles and pages seems fine but the script didn't change nothing.

  • Jan
  • 24
Rafael
Rafael

Good idea and good Job. I have some questions:
- Is necesary Javascript enabled on client browser?
- Is your class capable to work with huge XHTML files (about 8 mbytes)?
- Your RSS is not working just now : (

Thank you

  • Jan
  • 24
admin
Leon

Benjamin. I'm not sure how you have it configured, so I can't test, but it looks like the $_SERVER['DOCUMENT_ROOT'] is being reported differently depending on the page. Two options:

1. Wait for me to produce the Wordpress plugin I'm working on
2. Change line 291 to include the full path.

From

PHP:
$cachedir = $_SERVER['DOCUMENT_ROOT'] . $options['cachedir'];

To

PHP:
/home/zidane/www/wp-content/themes/branches/cache

It's a temporary hack, but it should work.

  • Jan
  • 24
admin
Leon

Skeku - have you made that directory writable? Does that path exist?

  • Jan
  • 24
admin
Leon

Rafael -
1. The class runs in PHP so it doesn't require JavaScript.
2. I haven't tried it with large XML files, but if you're using PHP to produce the XML then this script should GZIP it up.
3. Really? Which link are you clicking? It should redirect to http://feeds.feedburner.com/aciddrop

  • Jan
  • 24
Dmitri

Hi Leon.
I have small proposition.
Your minify_text() function strips just double spaces and comments from css styles. But there's more that could be stripped - spaces before and after {}:;, signs and ; before final bracket. I succesfully used the following function:
function minify_text($txt) {
// Remove comments
$txt = preg_replace('/\/\*.*?\*\//s', '', $txt);
// Remove whitespace
$txt = preg_replace('/;?[\s\n\t]*([{}:;,])[\s\n\t]*/', '\1', trim($txt));
return $txt;
}
Final css lookes like this:
html *{margin:0;padding:0}body{font-size:62.5%;color:#000}

  • Jan
  • 24
admin
Leon

Thanks Dmitri, I'll test it out and add it to the next version.

  • Jan
  • 25
eyn
eyn

I was working on a WordPress plugin that automates all this when I first saw your page on del.icio.us but with no success (only manage to get the whitespace compress to work but even that will invalidate my site as XHTML transitional). Anyway, the problem I faced is the way you wrote your compressor class. You're making use of "document_root" to find the css, js source, cache files etc. but document_root might differ depending on server settings e.g. PHP is run as one user but your site is hosted as another user. A better approach is have them defined in arguments when calling the class and try not to use "document_root" at all except for defaults.

I suggest you use Snoopy class provided in WordPress to fetch those css and js source files before compressing them. It is not reliable to trying to get them from the server locally as there might be lots of path problem depending on server configuration (all is invisible from just the URL in src attribute).

Try
add_action('get_header', 'compressor_start');
add_action('get_footer', 'compressor_end')

when writing your WordPress plugin, this should save the manual modification of template files.

  • Jan
  • 25
TrevorLEe
TrevorLEe

I was able to get this to work (I think, the sites are kind of "small") on some Dreamhost Drupal installs of mine, but on another webhost, which has a fairly "rich" Drupal based site that could maybe use a little help like this, I got the line 382 error.

Great concept...

  • Jan
  • 26
Jake Rutter

Can someone make this into a wordpress plugin? That would be super!

  • Jan
  • 26
Vladimir

Tried it on a wordpress blog, but the css was not properly shown (like some parts of css were ans dome were not). Strange.

I love the idea though.

Two questions:
1. When are you finishing a wordpress plugin?
2. How will it work with WP Super Cache?

  • Jan
  • 26
Fakfps
Fakfps

I aply it on my blog with wordpress and it show something like this:

CODE:
�x^�]KsG�>����ؠf��x��(K�-��=ދ��]Zjt�� O�?��9��/1�c�eV� H4d�ލ���*�*+_f�?y����w/O�(����_��D��[=j��L�?Uc�fq I�� ��0-�4C:�1Mb���D��Fs��]RWunu�v��I�>B�]�F��\��nwwy���|ƾg�������F�� {�I�Į��X�z�W��7z���}�lo/�sE�@�W��;*��s}7�u�,��C0?$��1f�.�h�Ρ�bjO3Q�?�ؑ�������/�=���,�]w4�f���a�Ej�^�m����t� X�cl�ġ��C@Ц�ʖ*6[[  ��#ei��.E .�x �~&�Ð�Kz͟�^�d-Ly�b�D+ �C?�dU$�aP5@_�fC�Kd%,M(�LS" �A5���� ��D� ��zI4+Xz�����) ��¦�z�V��TZ:4[`�b��tȅ�v�����BV�iMo�7�{�>ބk�U���݆�j�6w����f��Ok���ٽރ lYz�贜v�i�T�I������������W�n���hj�FVpc��]aG�ղ��i� ֑P�^��}&��3��FcG�u������s�>��3��@��X� ��c�f�A��vX��M��-H�Y ��z�� �M� !N`�;d͢�2'���-h�1� ]{���x,Z�p��N��j����RCR��P�.R �l�o��OH�HW    �f�7� ~(��۞( �u"&!�U-A��8��@!��:�4���⑱�}U��Ť�ϰՓ��]_�M�47[��AE�j�P��B�W[*Dt�v�H�-@����Q�m�P�E�]j����x�������ba%)uZ�% �}��CtA}�R���I;��fk�:���WyХ%nF\�F�@2��FM�Y��K,�����G�4/�8��o'�4���U������j��#�?��8��>�w��ɚ aW}��d�@5�50ڰ Fpb�&|L� 6f(t��a�Q�lǹ�-y C�A^��@��6(��7��@A>�xI��G��3�`��I� 9�$�ô����v��3xb��Hd���dմ7[0Q�}c� �B\9c`= ���OM�Ze~x�r��]��O�K��"b�쳇������(Ŭ� ����Hf9��r ����c�3�Z��Y�5�k��bֺ�V{�EYl�Z,r��jl8v~%�#!:u��ڄ��`�ʹ�@�w�L�J�D� F1v<����/����\N7��$z��)T��s���R]⠵T���?��ѭ�%K�ʬ��}��<G ���� #�$@�E�W;����<�?��[��ɰ�g���N �3�`d$�=V ��C�}I�r�u/��� ')�tj�o�kL��.g�e������zv��Ì�1F1�j)��Tl�5��!�`�$d�)[|�6����� ���� �9"�H��uPY*`Xdy�)ƒ�6L F$��1�$�J��Te9g�6�?f��t���L?R��ϓ�OX\1M`!�x���1‚!7�TS:�xv07�-=�D�K�D>��X=��K�%�52�ܑ�.5�@�C��n��dq�9r1ZD�� ��P0�I�1Q6D�f�Nf LZ>��K�z`쓦��^��f�h2

  • Jan
  • 26
Ptx_Soccer

Hello. Can i use it with Joomla?

  • Jan
  • 26
admin
Leon

@eyn: Yes, judging from a lot of the comments people have had trouble with the location of the cache directory. I have a better solution for this in mind. Snoopy is a good suggestion, thanks. Yes, the plugin will the fully auto, no manual modification required. Thanks for the tips.

@TrevorLEe: I think the line 382 error is to do with the path problems eyn describes above. This will be addressed in the next version.

@Jake: I'm working on it as we speak! It will be ready probably around the middle of next week, subscribe to my rss feed to keep updated: http://aciddrop.com/feed/

@Vladimir:
1. Around the middle of next week.
2. I will test it with Super Cache and see if there are any compatibility issues.

@Fakfps: That looks like raw gzip content. Try turning page gzipping off.

@Ptx: No idea I'm afraid! Why not test it out on a test page and let us know!

  • Jan
  • 26
Sangesh

Cool tool... i've downloaded this and very excited to use it as well...

Cheers.

  • Jan
  • 26
Ptx_Soccer

I cant test because i dont understand the code and i dont know where i put the code and how :S

  • Jan
  • 27
Daniel

Thanks for the great work Leon,

I have been looking at making this into a CMS integration, but the problem I am having is handling relative image css and multiple css files in different locations.

The reason for the multiple css files is all the extra extensions which plug into a CMS.

Do you think there would be a way to record the relative css correctly if it's across different directories?

The same issue occurs with JS files too.

Would it for example be possible to make it assemble together css or js files which are in the same directory only and leave the new compiled, minimized file in that same directory? (if they were all writable of course)

  • Jan
  • 27
Vaughn
Vaughn

How does this effect the server and bandwidth?

  • Jan
  • 28
admin
Leon

Ptx_Soccer - You could wait for my Wordpress plugin if you use Wordpress. It's Coming very soon.

Daniel - Thanks. Yes, the relative paths are an issue. I have had an idea on how to fix it: the script scans the CSS file and replaces relative image paths with absolute image paths. It's pretty easy to do, the only issue is with paths that are in the ../../ style format. It's something that will be in the next version.

Vaughn - a lot depends on the server. Turning on gzip compression for the page can hurt the CPU a bit, but it obviously saves you bandwidth. It's a case of trying out various setting and working out which works best for you.

  • Jan
  • 28
elzebore

Great work!!... It works perfectly with my wordpress blog...
Nevertheless, I was using "CrazyEgg" to get my statistics and now I can't use it anymore :(

  • Jan
  • 29
video craps
video craps

I really enjoy looking through your website

  • Jan
  • 29
Begreeny

It is great idea, but I have some problems with your php script (0.3.1).

I modified your test page begining to:
require_once('/nfs/extra/b/be/begreeny/wwwroot/t/class.compressor.php');
$compressor = new compressor(array("javascript"=>array("cachedir"=>'wwwroot/t/test_page',
"gzip"=>true,
"minify"=>true,
),
"css"=>array("cachedir"=>'wwwroot/t/test_page',
"gzip"=>true,
"minify"=>true,
),
"page"=>array("gzip"=>true,
"minify"=>true
)
));

The files generated with e.g. these contents in cahe dir (in "wwwroot/t/test_page/"):

ob_start ("ob_gzhandler");
header("Content-type: text/javascript; charset: UTF-8");
header("Cache-Control: must-revalidate");
$offset = 6000000 * 60 ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
The request sended for a javascript is bad, e.g.:
http://begreeny.comwwwroot/t/test_page/_cmp_javascript_219f2added00d9bad3e4c7b679a9c5d1.php

In this case, the javascript couldn't working.

Can you help me, how to setup it?

  • Jan
  • 29
admin
Leon

Begreeny - you should put the relative path as the cachedir:

"cachedir"=>'/test_page',

  • Jan
  • 29
admin
Leon

elzebore - how are you including the CrazyEgg code?

  • Jan
  • 29
Begreeny

Dear Leon!

Thanks for your answer.
I try the followings:
- "cachedir"=>'/test_page'
- "cachedir"=>'/t/test_page'

The error messages:

CODE:
Warning: realpath() [function.realpath]: Unable to access /nfs/extra/b/be/begreeny/t/test_page in  /nfs/extra/prepend/prepend.php(159) : system created function on line 16

Warning: realpath() [function.realpath]: Unable to access /nfs/extra/b/be/begreeny/t/test_page in /nfs/extra/prepend/prepend.php(159) : system created function on line 16

Warning: opendir() [function.opendir]: Unable to access /nfs/extra/b/be/begreeny//t/test_page in /nfs/extra/prepend/prepend.php(159) : system created function on line 17

Warning: opendir(/nfs/extra/b/be/begreeny//t/test_page) [function.opendir]: failed to open dir: Permission denied in /nfs/extra/prepend/prepend.php(159) : system created function on line 17

Warning: readdir(): supplied argument is not a valid Directory resource in /nfs/extra/b/be/begreeny/wwwroot/t/class.compressor.php on line 692

Warning: closedir(): supplied argument is not a valid Directory resource in /nfs/extra/b/be/begreeny/wwwroot/t/class.compressor.php on line 697

Warning: Invalid argument supplied for foreach() in /nfs/extra/b/be/begreeny/wwwroot/t/class.compressor.php on line 668

Warning: realpath() [function.realpath]: Unable to access /nfs/extra/b/be/begreeny/t/test_page in /nfs/extra/prepend/prepend.php(159) : system created function on line 16

Warning: realpath() [function.realpath]: Unable to access /nfs/extra/b/be/begreeny/t/test_page in /nfs/extra/prepend/prepend.php(159) : system created function on line 16

Warning: opendir() [function.opendir]: Unable to access /nfs/extra/b/be/begreeny//t/test_page in /nfs/extra/prepend/prepend.php(159) : system created function on line 17

Warning: opendir(/nfs/extra/b/be/begreeny//t/test_page) [function.opendir]: failed to open dir: Permission denied in /nfs/extra/prepend/prepend.php(159) : system created function on line 17

Warning: readdir(): supplied argument is not a valid Directory resource in /nfs/extra/b/be/begreeny/wwwroot/t/class.compressor.php on line 692

Warning: closedir(): supplied argument is not a valid Directory resource in /nfs/extra/b/be/begreeny/wwwroot/t/class.compressor.php on line 697

Warning: Invalid argument supplied for foreach() in /nfs/extra/b/be/begreeny/wwwroot/t/class.compressor.php on line 668

The real path not /nfs/extra/b/be/begreeny/t/test_page. The real path is: /nfs/extra/b/be/begreeny/wwwroot/t/test_page

  • Jan
  • 29
admin
Leon

Bgreeny - try "cachedir"=>'/wwwroot/t/test_page'

Otherwise, you can hack the code. Change line 292-297:

remove:

CODE:
//Set cachedir   
        if(strstr($options['cachedir'],$_SERVER['DOCUMENT_ROOT'])) {
        $cachedir = $options['cachedir'];
        } else {
        $cachedir = $_SERVER['DOCUMENT_ROOT'] . $options['cachedir'];
        }

add:

CODE:
$cachedir = "/nfs/extra/b/be/begreeny/wwwroot/t/test_page";

I am working on this problem for the next version! Subscribe to my feed for updates: http://aciddrop.com/feed

  • Jan
  • 29
Daniel

Thanks for the reply Leon,

Glad to hear you have eyes on the issue I am talking about.

../ terminology will be difficult I agree, but it will be essential for what I am doing with it, so I will have to add it in even if you don't :)

I was thinking about hard coding in the image locations too, and wondered it that defeated the purpose of minimizing things or not.

I suppose it might be better than my other thought which was to leave them where they are and generate multiple files still.

  • Jan
  • 29
Heather
Heather

Not only has your script worked wonders on my Wordpress blogs, it easily translated over to PHP BB 3 with only minor modifications. The load times have significantly decreased on all sites. Well done!

  • Jan
  • 29
mark

Will this work well with Smarty templates? Cause Smarty has a cache for pages too.

Perhaps I only need to do this for css and js, right?

  • Jan
  • 29
elzebore

Hi Leon,
At my first I've included the "crazyegg code"...
Then I've let it outside the compressor, And finally I only compress css and page...
With no success...
But it isn't very important. When I want to view my cazyegg results, I only have to comment "3 liens of code" ;)

  • Jan
  • 30
Daniel

Leon,

Do you have a donate button anywhere?

I want to drop a few dollars in firstly to say thanks and secondly to provide some motivation to get the image references in multiple CSS files in different locations fixed.

Also feel free to drop me a line if you want a hand. I am not too shabby at php myself.

  • Jan
  • 30
admin
Leon

Daniel - thanks, I may just put up a donate link with the next version. Thanks too for the offer of help.

Heather - thanks!

mark - Yes, this works with Smarty templates. If you want just CSS and JS you could actually use my Smarty plugin: http://aciddrop.com/2008/01/03/automatically-join-your-javascript-and-css-into-a-single-file/
which is how this script first started out.

elzebore - true, but make sure the script isn't removing the CrazyEgg tracking code. You are using the latest version which allows for external javascript, right? Also, moving the crazyegg code outside the HEAD tags might help.

  • Jan
  • 30
mark

Thanks Leon. It eems like a good script/plugin.

I did, however, decide to go with http://code.google.com/p/minify/ as it did the stuff I wanted and was easier to implement for just the files required (it gave me an error for conflicts in some js files, so I included it the normal way).

  • Feb
  • 6
Jordi Hernandez

For java developers I offer a similar, Open Source tool called Jawr. It helps develop java web apps with heavy use of javascript. It does so by allowing you to split the .js codebase in as many files and directories as desired, without compromising page loading times. In fact, it will improve loading times since it will join the files into one or several bundles (which is easily configurable), and afterwards the bundles will be minified and gzipped. This way you can have a nicely modularized codebase that is easy to mantain instead of a couple of huge javascript files.
Jawr has a development mode in which instead of the bundles, you get each member separately and with no minification, so you can use an exploded war directory and test changes on the fly. All without having to change the JSPs you are testing.
Finally, you get the same advantages (modularization and compression) for CSS files.

  • Feb
  • 12
Welcome to Paradise

Hope to use it in my website too. thanks for sharing.

  • Feb
  • 29
Afelfel

Friends any help?
I put three lines like you side but in the template.php which loading each time with any body page in my website but the flashing five times and stop loading in the end
any body have idea about this bug?

  • Feb
  • 29
Afelfel

Friends any help?
I put three lines like you side but in the template.php which loading each time with any body page in my website but it flashing five times and stop loading in the end
any body have idea about this bug?

  • Feb
  • 29
admin
Leon

Afelfel - please use the latest version: http://aciddrop.com/php-speedy

  • Mar
  • 12
gltest
gltest

Great stuff man! I will try it, thank you

  • Mar
  • 14
RAGHAVENDRA MAHENDRAKAR

Hi,

Is this only for PHP based website, or for all normal HTML/XHTML/CSS based websites...

  • Mar
  • 14
acc
acc

Amazing!, great stuff each framework should have a tool like this.

@James
Decreasing a thing 50% means making that thing the middle of its current size, decreasing a thing BY 100% means to make it half its size too. I think is the meaning of the sentence: "Improve website load time by 500%..."

  • Apr
  • 3
afelfel

Ido not have blog or bugin folder to install latest speedy wp only i have this three lines of code but why it reloading the page forever going in the loop
please help friends
im puting the code in the template.php wish inclouded in all pages to vies header and footer so what the error in that

  • Apr
  • 23
rubber2002

Hi I just installed this tool for my pligg based webiste totalpad.com.
Now home page shows error: Could not open ./
Other pages work fine.
Any suggestion please?

  • Apr
  • 25
rodolfo

i m having a problem
Warning: Invalid argument supplied for foreach() in C:\apache2triad\htdocs\opiadeiro\include\php_SPEEDY\controller\compressor.php on line 16 Fatal error: Call to a member function set_paths() on a non-object in C:\apache2triad\htdocs\opiadeiro\include\php_SPEEDY\controller\compressor.php on line 38

:-(

my code is

ini_set('display_errors','On');
require("include/conexao.php");//conexao
session_start();
require_once('C:/apache2triad/htdocs/opiadeiro/include/php_SPEEDY/php_speedy.php'); //Include the class. The full path may be required
$compressor = new compressor('css,javascript,page');
require("include/acesso.php"); //Contador de acesso

  • Apr
  • 29
Angel
Angel

What's???

‹x^ÅZërÚHþTå:J —2‰‹Á€˜Â6Éz6Ž³Æ™ÝL&åj¤šµ" Œ“šÚGÙÇØÚûû{º%¡–ßÈÖš”n_Ÿó{+Ýi0·{/žw§[½n@›ôúo..ûƒçN/Ô“‹sõø²[ ¯Às`4 ·L¾.èÒPN˜'(_ݺDAfxd(YŽÞAæ{> ê³r«Õ8*ë GòMº à¹èö_ðÅYqÃËrùÅóñÂ1Êt~~=¦Žu1šœ²Šè;ªT–uUÓ_q¦ø1Še-lÂíKàVd™D~yƒúÈn•!9`I[²0¶˜/„8G™‹âƒÁ&›£Í"¶›þÐîùüóHDÇ׳öU*å2¯X•°¦‰êÜÚD.n¦ï'UmĬ[^eFØü2ñرÊ&³™×F¯^‹Ÿ0Æ¨«×­ˆ" Øâi›Œƒ9d6ꊦ €¹ÒQøõ†ZÁT\ §„N¦P›ásÞBf6”l®,äÃ\Áæáêµv¨Nè8_Êžnm=­kÛOW·ƒÔÅé¢ exõñí]}|?ˆšˆ/^÷ Ô‡ ´‘æB^fpÛ¦Îô·e‹@ˆaÞ%´æ@Ðàö’ú4 Öî¦lI¼ÍË` â´ˆÍ£­lFSÈ^l ŒˆÍã…obä„6g#jÓ83BŸmÐ=êôæ¾±­§ËQM*¥[b;Sã’u#’é°.%ÕVMý[¢aZÏÇÕ(/ZqXqaÔTÝ©›ptMû)6±ø.¡ZzՐ€œJƒ:®×{öŒ·Y›ÍÄvÌ` ‰Ø“šUHm»›‡ÝÚCß"=&Fn¡¶7ÃZ»308“`Ê s™€ÙÅVt\ØWo¦~óÆ95‡½4õåñ—is2š±«÷'þ<`—oçî_~³f—wöáb4§P”£…xã*ì÷vw–¥RcyO'¹½wƒGtÖªR ·8ôƒZ1@îzïX÷˜²AåTç ÂY’´E—™šëe㱑i_Ïêù^ÿd0^ Óê‡gïúï®ÃnEÜ-vimÝyd]$Yüô5ÿ$Q"H5ô¢ß eY”X¨/ÑöUØxÇBH¾{= r0E¡âÓWä›|K Ú›õ«ùôø¬.özVá{ CiðÉBУdæɤÁþ_ öí~°ú|W.VŸ¿9ëÿ@<æý.£¾¯&)†8Œëh&CH$1çĦæC™Á+Îp“¸|bU8`¶]ÐJìƒÞ7ÅtÕÒt¥3Å6º²^ TOé¼)rÎci‘¤}î/[,¸K¤Ã´H yà{R3…Ԕǃǵ~KGi–2…/ÃR´QT?{ö´`ÎÖÍíU³–˜Ã®ƒ oPÔ‘‡þŽN˜{+JªjZ •Ñƒ×.þRP˜ ¼Ò$Þ’¿É–7®ïçâ½+)ÐTµíRñ]/x÷–ÚV’ly—ó܉£Ë»ûÉ»ûàȃý>8У'ûg{$ÌûÈûðõ~ÞòèR@ì#þ£€ä©uää³ΑT÷Á‘§ÆMœtâáíã(úñÿ…þ Yú·c6$

  • Jun
  • 2
Md. Maksudul Islam
Md. Maksudul Islam

I tried to use the class. It works well. except some small problems.

Here are some problems:

(1) After compressing the site some of library like Lightview, Modalbos detect as virus in some anitivirus.

(2) Some CSS style dosen't work.

(3) Some javascript dosen' work.

Please check and fix these.

  • Jun
  • 5
admin
Leon

Md. Maksudul Islam - please see here for the latest version http://aciddrop.com/php-speedy/ Please leave all comments on that page.

Comments are closed