Categories > TinyButStrong general >

Compress template realtime

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: testebr
Date: 2006-05-03
Time: 21:45

Compress template realtime

I have this function:

// definition of callback functions
function cb_script($matches)
{
  global $ascript;

  $i = count($ascript) + 1;
  $h = md5($matches[0]);
  $ascript[$i][0] = $matches[0];
  $ascript[$i][1] = $h;

  return $h;
}

function cb_text($matches)
{
  global $atext;

  $i = count($atext) + 1;
  $h = md5($matches[0]);
  $atext[$i][0] = $matches[0];
  $atext[$i][1] = $h;

  return $h;
}

function cb_pre($matches)
{
  global $apre;

  $i = count($apre) + 1;
  $h = md5($matches[0]);
  $apre[$i][0] = $matches[0];
  $apre[$i][1] = $h;

  return $h;
}


// init some variables/arrays
$page_byte = strlen($output);
$ascript   = array();
$atext     = array();
$apre      = array();

// cut from output, and store away for later re-inclusion, everything between <textarea> and <pre> tags
// (we do not want to touch those blocks as they may contain wanted whitespace)
$output=preg_replace_callback('/<textarea.*?[^\btextarea>\b]*?textarea>/si', "cb_text", $output);
$output=preg_replace_callback('/<pre.*?[^\bpre>\b]*?pre>/si', "cb_pre", $output);
// also cut and save <nocompress> tagged text (intentional excluded parts)
$output=preg_replace_callback('/<nocompress.*?[^\bnocompress>\b]*?nocompress>/si', "cb_text", $output);

// strip all whitespace at the beginning of every new line (but leave the linebreak intact)
$output=preg_replace('/(\r)?\n(\s*)/', "\n", $output);

// cut from output, and store away for later re-inclusion, everthing between <script> tags
// (we don't want to strip the linebreaks from script code since that would render the scripts non working)
$output=preg_replace_callback('/<script.*?[^\bscript>\b]*?script>/si', "cb_script", $output);

// strip all linebreaks and HTML-Comments from the output
// (since we first strip all linebreaks, even multi-line comments will get striped this way! :-))
$output = str_replace("\n" ," " ,$output );
$output = preg_replace('/\/\*.*?[^\b\*\/\b]*?.*?\*\//',"" , $output);
$output = preg_replace('/<!--[^\{\[\]*?[^b<!--\b]*?[^\b-->\b]*?-->/i', "", $output);
                                           // We exclude HTML comments containing "{" and "["
                                           // this way CSS definitions and AnyMedia stuff won't get striped

// re-insert everything we saved before
foreach ( $ascript as $block)
{
  $output = str_replace( $block[1] , $block[0] , $output );
}
foreach ( $atext as $block)
{
  $output = str_replace( $block[1] , $block[0] , $output );
}
foreach ( $apre as $block)
{
  $output = str_replace( $block[1] , $block[0] , $output );
}

// last not least: strip any remaining multiple spaces between tags...
$output = preg_replace('/>[\s]+</', "> <" ,$output );
// ...and also strip the <nocompress> tags for keeping any XHTML compliance
$output = str_replace("<nocompress>" ,"" ,$output );
$output = str_replace("</nocompress>" ,"" ,$output );

// calculate some nice looking numbers :-)
$pagenew_byte       = strlen($output);
$page_kilobyte      = number_format(($page_byte/1024),2);
$pagenew_kilobyte   = number_format(($pagenew_byte/1024),2);
$pagesaved_byte     = $page_byte - $pagenew_byte;
$pagesaved_kilobyte = number_format((($pagesaved_byte)/1024),2);
$pagesaved_perc     = number_format(((100*$pagesaved_byte)/$page_byte),2);

// OPTIONAL
//$cmprstats = "<div align='center' class='smallfont'><em>page compression: </em>$pagenew_kilobyte k/$page_kilobyte k (<strong>$pagesaved_perc%</strong>)</div>";

// OPTIONAL
//$output = str_replace('</body>', $cmprstats . "</body>" , $output);


Where $output is my html code.. an simple echo and will show the final litte code.

I'm noob with the TBS class. Can anyone make one 'hack' or an plugin for my code work?

I use the TBS 3.0

This is useful for all.

Thanks advanced
By: Skrol29
Date: 2006-05-04
Time: 00:46

Re: Compress template realtime

You can do a plug-in, but it is not a need.
Here is how you can do simply:
function f_compute(&$output) {
  // your code here, inside the function
  ...
}

...
$TBS->Show(TBS_NOTHING); // Merge without displaying and without quit
f_compute($TBS->Source); // Compute the result
$echo $TBS->Source; // Dislay the result
By: testebr
Date: 2006-05-04
Time: 14:33

Re: Compress template realtime

Thanks a lot man, work fine!

This forum is very useful.
By: testebr
Date: 2006-05-09
Time: 22:30

Re: Compress template realtime

Hi again,

Anyone advanced wants make/convert this functions to one plugin? This is useful for all, it make compress 10%-15% in all pages. This is good to be default in the main TBS class. ;D

I would like have this, but I'm not too much expert yet in php.
By: Sheepy
Date: 2006-05-12
Time: 07:05

Re: Compress template realtime

Yes it would be useful as a plugin, or perhaps even a default part of the HTML plugin, since it's small and is intended for (X)HTML only.
By: Skrol29
Date: 2006-05-12
Time: 11:28

Re: Compress template realtime

Hi,

Ok, I'm preparing a plug-in for this.
I will put it in beta first.
By: TomH
Date: 2006-05-12
Time: 12:05

Re: Compress template realtime

Please help me, I don't undertand the reasoning behind wanting doing this. Is this because you are doing big XHTML pages? 

Servers and browsers now can do page comperssion on the fly - doesn't that produce much better result for file size?

Have you done any tests to see how much added server load it take to render the pages this way? That would be good to know about if you have done it.

It seems to save bandwidth -- but maybe at the expense of much loading the server on every page view.  If this were combined with caching maybe much better then.

By: Scythe
Date: 2006-05-12
Time: 17:35

Re: Compress template realtime

Well, I currently use Gzip to compress files, and that's on the fly.  That takes very little resources and compresses it quite well 25kb to 4.6kb.

However, the idea is to remove all whitespace, comments, etc.  This would probably save the file .6kb.  Now, that .6kb can add up.  Depending on the size of the site, that .6kb can be huge.  I plan on releasing my site to the public and get tons of visitors.

I'd be very interested in this plugin if it was using regex expressions only (fastest, least amount of processing power).  The current setup of that code is pretty large on the Big-O scale.  Anyway,  if someone wants to optimize that code, we could create a very efficient compression.
By: Scythe
Date: 2006-05-12
Time: 17:37

Re: Compress template realtime

Also, sorry about stating this on my last post:  Older browsers and IE especially don't support Gzip.  So, this would be a very useful bit of code for older browsers.
By: Skrol29
Date: 2006-05-12
Time: 17:45

Re: Compress template realtime

Hi,

I must say I agreee with TomH.
But I'll do the plug-in anyway as an illustration of plug-ins utility.
By: Skrol29
Date: 2006-05-14
Time: 01:28

Re: Compress template realtime

Hello,

You can test the Compact plug-in here:
http://www.tinybutstrong.com/dev/tbs_plugin_compact.zip
By: ioguix
Date: 2006-05-18
Time: 12:30

Re: Compress template realtime

Hello,

I'm quite agree with TomH and Skrol29.

Moreover, If you want to save some place in that way, you could just make a release of your pages directly without these spaces etc. It will be done just once by you instead of each time by the server when pages are requested...

my 1.2ยข...

++

--
ioguix
By: LeoC
Date: 2014-06-02
Time: 19:54

Re: Compress template realtime

Funny. This issue is currently discussed in SEO these days. Although the browser / server run a type of compression,
minifying the code (removing the spaces and unnecessary elements) combined with GZip compression
helps to deliver faster pages.

A plugin like this for HTML pages, would be a remarkable feature.