![]() ![]() |
Aug 20 2008, 05:57 PM
Post
#1
|
|
|
Member ![]() ![]() Group: Members Posts: 125 Joined: 5-February 08 Member No.: 801 |
I had been playing with the gzip function on my site. With some help from friends (and days of google searches) I realized I didnt need gzip with my server configuration. My server has mod_deflate enabled and I can set the "gzip" compressing of pages via cpanel with the "optimize website" function. This compresses pages without having gzip turned on in the osc admin.
I've also compiled eaccelerator with php on my server which caches some of the php. This has helped with page load times a bit too. I also do not have a page cache contribution installed (just havent gotten around to it). So I hacked together some code from here and there and added it to my application_top.php at the top, just after Released under the GNU General Public License */ CODE /* initialize compress function for whitespace removal */ ob_start("compress"); /* Begin function compress */ function compress($buffer) { # strip blank lines (blank, with tab or whitespaces) $buffer = preg_replace("/[\r\n]+[\s\t]*[\r\n]+/","\n",$buffer); # join lines $buffer = str_replace("[\r\n]", ' ', $buffer); # strip whitespace between > and < return preg_replace('/\>[\s\t]+\</', '> <', $buffer); /* remove unnecessary spaces */ $buffer = str_replace('{ ', '{', $buffer); $buffer = str_replace(' }', '}', $buffer); $buffer = str_replace('; ', ';', $buffer); $buffer = str_replace(', ', ',', $buffer); $buffer = str_replace(' {', '{', $buffer); $buffer = str_replace('} ', '}', $buffer); $buffer = str_replace(': ', ':', $buffer); $buffer = str_replace(' ,', ',', $buffer); $buffer = str_replace(';', ';', $buffer); return $buffer; } To test this code on your site, look at the source of your page first. Add the code then check the source again. Most of the white space and such should be condensed. It has really helped my pages to load a little quicker. I rarely have something to share these days so I thought I would share this tip since I've received excellent help from everyone in the past. I am sure the code could be edited to clean the page even further. I believe Chemo's page cache has similar code, so you may not need this if your pages are already "cleaned" by another modification. |
|
|
|
Dec 15 2008, 09:15 PM
Post
#2
|
|
|
Newbie ![]() Group: Members Posts: 2 Joined: 15-December 08 Member No.: 1,749 |
You might also add this:
CODE $buffer = str_replace(" ", '', $buffer); $buffer = str_replace("\n", '', $buffer); The page cache from chemo actually saves dynamic files to static files, so you can't compare that functionality with your script. |
|
|
|
Dec 22 2008, 06:26 AM
Post
#3
|
|
![]() Member ![]() ![]() Group: Members Posts: 215 Joined: 1-September 07 From: echo '/usa/kentucky' > /dev/null Member No.: 148 |
The Page Cache contribution is one that I wished I never released. The code is monstrous...and abomination to PHP4. As any coder will tell you when looking back on previous work there is always a better or more elegant way.
Just a cursory glance at your code there are two things that jump out at me:
No disrespect intended...just some observations. Bobby -------------------- |
|
|
|
Jan 3 2009, 10:55 PM
Post
#4
|
|
|
Member ![]() ![]() Group: Members Posts: 125 Joined: 5-February 08 Member No.: 801 |
Thanks for looking at this Bobby. Since I know the bare minimum about how the codes "work" I just read what I can and try to make it work for me. I found those lines here and there and put them all in there.
No disrespect taken, the code worked for tightening up my code a little and I thought I'd share BTW: If ya have a quick way to clean that up you'd like to share I'd be happy to use it! |
|
|
|
Jan 11 2009, 09:07 PM
Post
#5
|
|
|
Development Team Group: Team Member Posts: 299 Joined: 25-November 08 From: Ipswich, UK Member No.: 1,649 |
CODE ob_start("compress"); /* Begin function compress */ function compress($buffer) { # strip blank lines (blank, with tab or whitespaces) /* remove comments */ $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); # strip whitespace between > and < $buffer = preg_replace('/\>[\s\t]+\</', '> <', $buffer); /* remove unnecessary spaces, tabs and newlines */ $bad_array = array("{ ", " }","; ",", "," {","} ",": "," ,",";"); $good_array= array("{", "}",";",",","{","}",":",",",";"); $buffer = str_replace($bad_array, $good_array, $buffer); return $buffer; } Not by any means perfect. but a marginal improvement on the original with a single str_replace pass through. -------------------- |
|
|
|
Jan 15 2009, 05:27 AM
Post
#6
|
|
![]() Member ![]() ![]() Group: Members Posts: 215 Joined: 1-September 07 From: echo '/usa/kentucky' > /dev/null Member No.: 148 |
preg_replace can also take arrays for the pattern and replacement parameters. Might as well combine all the expressions into one function call...but you knew the direction I would head in.
Bobby -------------------- |
|
|
|
Feb 18 2009, 03:09 PM
Post
#7
|
|
![]() Member ![]() ![]() Group: Members Posts: 215 Joined: 1-September 07 From: echo '/usa/kentucky' > /dev/null Member No.: 148 |
Here you go...enjoy!
CODE <?php // In includes/application_top.php add this just after the require construct for general.php ob_start('endCompress'); // It should look like this: /* // define general functions used application-wide require(DIR_WS_FUNCTIONS . 'general.php'); require(DIR_WS_FUNCTIONS . 'html_output.php'); ob_start('endCompress'); */ // Paste this function in includes/functions/general.php: /** * Callback function for output buffering to compress output * {@source} * @author Bobby Easland * @copyright Copyright (c) 2008-2009, Bobby Easland * @license http://www.opensource.org/licenses/gpl-2.0.php GNU Public License * @link http://www.oscommercespecialist.com/ osCommerce Specialist * @param string $buffer Current buffer layer * @return string */ function endCompress($buffer){ preg_match_all("!<style[^>]+>.*?</style>!is", $buffer, $match); $styles = $match[0]; preg_match_all("!<script[^>]+>.*?</script>!is", $buffer, $match); $scripts = $match[0]; preg_match_all("!<pre[^>]*>.*?</pre>!is", $buffer, $match); $pre = $match[0]; preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $buffer, $match); $textareas = $match[0]; $search = array("!<style[^>]+>.*?</style>!is", "!<script[^>]+>.*?</script>!is", "!<pre[^>]*>.*?</pre>!is", "!<textarea[^>]+>.*?</textarea>!is"); $replace = array('@TRIM:STYLE@', '@TRIM:SCRIPT@', '@TRIM:PRE@', '@TRIM:TEXTAREA@'); $buffer = preg_replace($search, $replace, $buffer); $buffer = trim(preg_replace(array('/((?<!\?>)\n)[\s]+/m', '/\>\s+\</'), array('\1', '><'), $buffer)); $searches = array('@TRIM:STYLE@' => $styles, '@TRIM:SCRIPT@' => $scripts, '@TRIM:PRE@' => $pre, '@TRIM:TEXTAREA@' => $textareas); foreach($searches as $search => $replace ){ $len = strlen($search); $pos = 0; $count = count($replace); if ($count < 1){ continue; } for ($i = 0; $i < $count; $i++) { if ( false !== ($pos = strpos($buffer, $search, $pos)) ) { $buffer = substr_replace($buffer, $replace[$i], $pos, $len); } else { continue; } } } # end foreach return $buffer; } # end function ?> In addition, I've attached a PHP file with the above so the whitespace won't be mangled. Bobby
Attached File(s)
-------------------- |
|
|
|
Feb 18 2009, 03:58 PM
Post
#8
|
|
|
Member ![]() ![]() Group: Members Posts: 21 Joined: 14-December 07 Member No.: 742 |
Bobby,
Would you use GZIP or this? I assume you would not use both. |
|
|
|
Feb 18 2009, 05:10 PM
Post
#9
|
|
![]() Member ![]() ![]() Group: Members Posts: 215 Joined: 1-September 07 From: echo '/usa/kentucky' > /dev/null Member No.: 148 |
Bobby, Would you use GZIP or this? I assume you would not use both. My personal preference is to not use gz_handler for the buffer callback and instead use Apache mod_deflate. Let the web server do the work to send compressed content...not PHP. However, if you don't have that option you can still use the ob_start('gz_handler') higher in application_top.php. The second call to ob_start('endCompress') starts adds a second buffer layer to the output stack which will cascade the stripped HTML to the GZIP handler. Bobby -------------------- |
|
|
|
Jan 20 2010, 08:35 PM
Post
#10
|
|
|
Member ![]() ![]() Group: Members Posts: 194 Joined: 28-September 09 From: Sweden Member No.: 2,558 |
Hi Bobby!
I have installed compress.php. I download the code into my editor pspad. I dont know why but these ending lines show up in bold: } } # end foreach return $buffer; } # end function ?> is it pspad itself, so I can ignore it? Kind regards Sara By the way, I found a snippet once which I am using (REMOVE THE SESSION ID's from the search engine index for pages already indexed) I believe you are the writer? Many thanks! |
|
|
|
Jan 20 2010, 10:18 PM
Post
#11
|
|
![]() Advanced Member Group: Team Member Posts: 645 Joined: 27-February 09 From: Hull, UK Member No.: 1,954 |
Hi Bobby! I have installed compress.php. I download the code into my editor pspad. I dont know why but these ending lines show up in bold: } } # end foreach return $buffer; } # end function ?> is it pspad itself, so I can ignore it? Kind regards Sara By the way, I found a snippet once which I am using (REMOVE THE SESSION ID's from the search engine index for pages already indexed) I believe you are the writer? Many thanks! Hi Sara, I use Booby's code above and it does a fantastic job. As long as you have followed his steps, there will be no issues with the install. It seems as if it may jus be pspad so you can most probably ignore it. If you have a local dev environmet, test the code their first before applying to your live site. Once the code has been applied, view your source code (right click and choose view source) - if all the html is "squashed" together, the install will have been performed correctly. -------------------- Tired of coding? Need caffeine?! Try - The Blending Room
|
|
|
|
Jan 21 2010, 06:02 AM
Post
#12
|
|
|
Member ![]() ![]() Group: Members Posts: 194 Joined: 28-September 09 From: Sweden Member No.: 2,558 |
Hi Sara, I use Booby's code above and it does a fantastic job. As long as you have followed his steps, there will be no issues with the install. It seems as if it may jus be pspad so you can most probably ignore it. If you have a local dev environmet, test the code their first before applying to your live site. Once the code has been applied, view your source code (right click and choose view source) - if all the html is "squashed" together, the install will have been performed correctly. Hi! Yes, the code is really compressed! A side effect (I think) that it will be tough for those who copy codes. So, the bold character haven't affected my site. Many thanks for your quick answer Sara |
|
|
|
Jan 21 2010, 09:17 PM
Post
#13
|
|
![]() Advanced Member Group: Team Member Posts: 645 Joined: 27-February 09 From: Hull, UK Member No.: 1,954 |
Hi! Yes, the code is really compressed! A side effect (I think) that it will be tough for those who copy codes. So, the bold character haven't affected my site. Many thanks for your quick answer Sara If the source code is compressed, it seems as if you have it working! -------------------- Tired of coding? Need caffeine?! Try - The Blending Room
|
|
|
|
Feb 9 2010, 09:22 PM
Post
#14
|
|
|
Member ![]() ![]() Group: Members Posts: 194 Joined: 28-September 09 From: Sweden Member No.: 2,558 |
If the source code is compressed, it seems as if you have it working! Hello again You know what? I found a bug in chemos script: . . /* Bug fix inserted a backslash at position 48, after the second ? -mark */ $buffer = trim(preg_replace(array('/((?<!\?\>)\n)[\s]+/m', '/\>\s+\</'), array('\1', '><'), $buffer)); $searches = array('@TRIM:STYLE@' => $styles, '@TRIM:SCRIPT@' => $scripts, '@TRIM:PRE@' => $pre, '@TRIM:TEXTAREA@' => $textareas); foreach($searches as $search => $replace ){ . . After I inserted the backslash, the rendering of the characters as bold disappeared and I think that the speed of my site increased. Regards Sara |
|
|
|
Feb 10 2010, 02:55 AM
Post
#15
|
|
![]() Moderator Group: Team Member Posts: 446 Joined: 27-October 07 From: Texas Member No.: 684 |
Good catch Sara.
-------------------- |
|
|
|
Feb 10 2010, 07:48 AM
Post
#16
|
|
|
Member ![]() ![]() Group: Members Posts: 194 Joined: 28-September 09 From: Sweden Member No.: 2,558 |
Good catch Sara. Godmorning and Thank You! I don't know if you are familiar with PSPAD? I like it a lot, although it's not a wysiwyg editor, I will not use any other because of its features. I haven't explored all of them, though. By the way, at the moment I am trying to format the content of the search box. I would like to have the text "Advanced search" and the arrow image separated so that the image always have the same position regardless of choosen language. The text "Advanced search" have different length in different languages. Where should I look to fix it? Kind regards Sara |
|
|
|
Feb 10 2010, 08:42 AM
Post
#17
|
|
![]() Moderator Group: Team Member Posts: 446 Joined: 27-October 07 From: Texas Member No.: 684 |
I like to use WeBuilder http://www.blumentals.net/webuilder/ Very functional, easy to find your closing brackets, and a great price.
I looked at your search box, it is different than the standard box. You have a reference in your source code to <a class="adv_search_title">Advanced Search </a> It should be <a class="adv_search_title" href="http://www.ji-fashion.com/advanced_search.php">Advanced Search</a> Although it will use the actual FILE_NAME in your code. You should be able to use that adv_search_title class to align it where you want, rather than using   . The button code right next to there could have a class added to it in the same way, and then float it or text-align it to the right. Most builds just use the text link and no button in that area. -------------------- |
|
|
|
Feb 10 2010, 01:01 PM
Post
#18
|
|
![]() Advanced Member Group: Team Member Posts: 645 Joined: 27-February 09 From: Hull, UK Member No.: 1,954 |
Hello again You know what? I found a bug in chemos script: . . /* Bug fix inserted a backslash at position 48, after the second ? -mark */ $buffer = trim(preg_replace(array('/((?<!\?\>)\n)[\s]+/m', '/\>\s+\</'), array('\1', '><'), $buffer)); $searches = array('@TRIM:STYLE@' => $styles, '@TRIM:SCRIPT@' => $scripts, '@TRIM:PRE@' => $pre, '@TRIM:TEXTAREA@' => $textareas); foreach($searches as $search => $replace ){ . . After I inserted the backslash, the rendering of the characters as bold disappeared and I think that the speed of my site increased. Regards Sara Good catch! -------------------- Tired of coding? Need caffeine?! Try - The Blending Room
|
|
|
|
Feb 10 2010, 04:58 PM
Post
#19
|
|
|
Member ![]() ![]() Group: Members Posts: 194 Joined: 28-September 09 From: Sweden Member No.: 2,558 |
I like to use WeBuilder http://www.blumentals.net/webuilder/ Very functional, easy to find your closing brackets, and a great price. I looked at your search box, it is different than the standard box. You have a reference in your source code to <a class="adv_search_title">Advanced Search </a> It should be <a class="adv_search_title" href="http://www.ji-fashion.com/advanced_search.php">Advanced Search</a> Although it will use the actual FILE_NAME in your code. You should be able to use that adv_search_title class to align it where you want, rather than using   . The button code right next to there could have a class added to it in the same way, and then float it or text-align it to the right. Most builds just use the text link and no button in that area. I will have a look at webuilder, thanks. I finaly got the search box the way I like it. The solution was to introduce a table in search.php So, now the style is conformant with the rest of my site without using a lot of Thanks for your advice! Sara |
|
|
|
May 28 2010, 01:59 PM
Post
#20
|
|
![]() Advanced Member Group: Team Member Posts: 645 Joined: 27-February 09 From: Hull, UK Member No.: 1,954 |
I've just found this very useful link that may be of use to people Best Practices for Speeding Up Your Website
Reading through this, has anyone used the PHP function flush() on their site and if so, have you noticed any performance differences? [EDIT] - Another couple of links that may be usefull, minifying your JS and CSS -> CSS Compressor - Minify Javascript Just be sure you debug your stylesheet and javascript before using the compressors and keep a backup copy of your original code, just in case you do need debug / change in the future. -------------------- Tired of coding? Need caffeine?! Try - The Blending Room
|
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 31st July 2010 - 06:38 AM |