Results 1 to 3 of 3
-
2nd Jul 2010, 05:16 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.com[php] Compression | Output Buffer control
Heya guys, I just built this small tool so you can add into your sites to compress your content and and modify it to save bandwidth.
How it works:
This class will watch your script so when your echoing content it will do some modifications and can also take your own modifications aswell
The Class:
PHP Code:<?php
class OutputBuffer
{
private $callbacks = array(); //Holder for callbacks
private $content;
function __construct()
{
ob_start(array($this,'handler'));
}
function addCallback($name,$item)
{
if(is_callable($item))
{
$this->callbacks[$name] = $item;
return true;
}
return false;
}
function handler($contents,$bitAtchion = null)
{
if($bitAtchion & PHP_OUTPUT_HANDLER_START)
{
$this->content = ''; //A New Buffer
}
if($bitAtchion & PHP_OUTPUT_HANDLER_CONT)
{
$this->content .= $contents;
}
if($bitAtchion & PHP_OUTPUT_HANDLER_END)
{
$this->content .= $contents;
//AS this is a final request we want to run the callbacks
foreach($this->callbacks as $this->callback)
{
try
{
$this->content = call_user_func($this->callback,$this->content);
} catch(Exception $e){}
}
return $this->_compress($this->content); //Compress and push out
}
return $this->content;
}
function _compress($string)
{
if(headers_sent() || function_exists('gzencode') === false)
{
return $string; //Cant compress
}
$ctype = strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'x-gzip') ? 'x-gzip' : strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip') ? 'gzip' : false;
if($ctype !== false)
{
header('Content-Encoding: '.$ctype);
return gzencode($string);
}
return $string;
}
}
?>
If you already have the function ob_start() in your script then replace with the code below, otherwise add the code below before your session_start() or at the very start of your script.
PHP Code:include 'OutputBuffer.php';
$buffer = new OutputBuffer();
Adding Callbacks:
You can add your own custom callbacks to modify the end content such as rudes whitespace, add timestamps etc etc.
Example of callback:
PHP Code:function addTimeStamp($content)
{
return $content . '<!--' . time() . '-->';
}
You can add it to your Output Controller like so:
PHP Code:$buffer->addCallback('timestamp','addTimeStamp');
Code:addCallback( string $name[, mixed $callback]])
Example:
PHP Code:$buffer->addCallback('tidyHtml',array($object,'method'));
PHP Code:function _trimwhitespace($source)
{
// Pull out the script blocks
preg_match_all("!<script[^>]*?>.*?</script>!is", $source, $match);
$_script_blocks = $match[0];
$source = preg_replace("!<script[^>]*?>.*?</script>!is",'@@@FILTER_TRIM_SCRIPT@@@', $source);
// Pull out the pre blocks
preg_match_all("!<pre[^>]*?>.*?</pre>!is", $source, $match);
$_pre_blocks = $match[0];
$source = preg_replace("!<pre[^>]*?>.*?</pre>!is",'@@@FILTER_TRIM_PRE@@@', $source);
// Pull out the textarea blocks
preg_match_all("!<textarea[^>]*?>.*?</textarea>!is", $source, $match);
$_textarea_blocks = $match[0];
$source = preg_replace("!<textarea[^>]*?>.*?</textarea>!is",'@@@FILTER_TRIM_TEXTAREA@@@', $source);
// remove all leading spaces, tabs and carriage returns NOT
// preceeded by a php close tag.
$source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source));
// replace textarea blocks
_outputfilter_trimwhitespace_replace("@@@FILTER_TRIM_TEXTAREA@@@",$_textarea_blocks, $source);
// replace pre blocks
_outputfilter_trimwhitespace_replace("@@@FILTER_TRIM_PRE@@@",$_pre_blocks, $source);
// replace script blocks
_outputfilter_trimwhitespace_replace("@@@FILTER_TRIM_SCRIPT@@@",$_script_blocks, $source);
return $source;
}
//HELPER FUNCTION, NEEDED ABOVE.
function _outputfilter_trimwhitespace_replace($search_str, $replace, &$subject)
{
$_len = strlen($search_str);
$_pos = 0;
for ($_i=0, $_count=count($replace); $_i<$_count; $_i++)
if (($_pos=strpos($subject, $search_str, $_pos))!==false)
$subject = substr_replace($subject, $replace[$_i], $_pos, $_len);
else
break;
}
PHP Code:$buffer->addCallback('trim_white_space','_trimwhitespace');
Code:<html> <head> <title>my Website</title> </head> <body> <p>.....</p> </body> </html>
Code:<html><head><title>my Website</title></head><body><p>.....</p></body></html>
P.S, The above function was taken from a smarty plugin and slightly modifiedlitewarez Reviewed by litewarez on . [php] Compression | Output Buffer control Heya guys, I just built this small tool so you can add into your sites to compress your content and and modify it to save bandwidth. How it works: This class will watch your script so when your echoing content it will do some modifications and can also take your own modifications aswell The Class: <?php class OutputBuffer { Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
7th Jul 2010, 03:49 PM #2Member
Thanks added to streamscene, page loads seem a little faster
-
7th Jul 2010, 06:14 PM #3OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comYour welcome dood.
Anything else you might need just let me know ill see what i can do.Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Looking for VPS or RDP that allows RAR compression
By PopSmith in forum Hosting DiscussionReplies: 6Last Post: 8th Dec 2011, 06:24 PM -
compression tool
By xiona_marie in forum Technical Help Desk SupportReplies: 7Last Post: 26th Jul 2011, 10:05 AM -
Scc buffer
By sapa50 in forum General DiscussionReplies: 0Last Post: 3rd Jul 2011, 06:24 PM -
Zoom Uploader Buffer Size
By Brenton in forum Tutorials and GuidesReplies: 5Last Post: 30th Jun 2011, 02:24 PM -
[Selling] [WTS] My legit empornium.us account with 537GB buffer (ratio 789)
By JE7 in forum Completed TransactionsReplies: 0Last Post: 29th Dec 2009, 09:34 PM
themaRegister - register to forums...
Version 3.54 released. Open older version (or...