Welcome Guest, Not a member yet? Register   Sign In
how to compress the html/js/style files in codeigniter ???
#1

[eluser]mythilisubramanyam[/eluser]
can i use gzip or defalte to compress html/js/style/image files????
or is there any other option to compress them ???
#2

[eluser]bretticus[/eluser]
Yes, but even though this can be done with PHP it is much more efficient to let your webserver do this. mod deflate in apache can perform this for you. If this is a shared hosting server, well, unless your hosting supports this (they may already) I wouldn't worry about it. image files are usually compressed as much as they will compress and plain text files should be relatively small for a well-designed website. I'm not saying that compression isn't a good thing. I'm just saying that if you have to do it with code, it's too much expense for a little gain.
#3

[eluser]Johan André[/eluser]
Well, js, css and image are usually static so combine and squeeze (using minify and cssmin) them and cache the result.
Serve the (minified) file from cache.

With javascript and css there tends to be alot of http-requests if you have alot of files. Combining all js to one http-request usually gives a more responsive site...
#4

[eluser]John_Betong[/eluser]
 
With regards to compression I tried changing this setting ./config/config.php
Code:
config['compress_output'] = TRUE; // FALSE; // default
The result was that it produces a blank page...
 
 
I investigated further in the forum and came across this code that I adapted for my own benefit. On average it reduces the $result by about 10%.
Code:
if (LOCALHOST) // DO NOT compress result
{
    // DO NOT COMPRESS
}else{
  $_SESSION['output_before'] = strlen($result);
        $search = array
            (
                '/\>[^\S ]+/s',    //strip whitespaces after tags, except space
                '/[^\S ]+\</s', //strip whitespaces before tags, except space
                '/(\s)+/s'        // shorten multiple whitespace sequences
            );
        $replace = array(
            '>',
            '<',
            '\\1'
            );
            $result = preg_replace($search, $replace, $result);

        $result .= sprintf('%6.3f', $_SESSION['zzz_after'] / $_SESSION['zzz_before']);
    $_SESSION['output_after'] = strlen($result);
}//endcompress

echo $result;
&nbsp;
I am curious to know what happens to the css/js files that are referenced in the $result. Are they expanded then compressed or does the reference get passed through to the browser where it is finally loaded?
&nbsp;
&nbsp;
&nbsp;
#5

[eluser]Nick Husher[/eluser]
[quote author="mythilisubramanyam" date="1252323375"]can i use gzip or defalte to compress html/js/style/image files????
or is there any other option to compress them ???[/quote]

You can definitely use gzip to compress files. That's a server configuration, though, and has nothing to do with CodeIgniter. Check out the relevant web server documentation on turning on a gzip filter.

There are a heap of tools for minifying javascript and css. I've heard the best javascript minifier out there is the one put out Yahoo, but it requires a certain amount of java handwaving. Still, if bit weight is your primary concern, writing a simple compressor shell script to run the YUI compressor on all your script/css files might be worthwhile. For images, you can use smushit, which doesn't further compress your lossy-compressed images (jpeg, gif, etc), but it rips out certain bits of metadata as well as optimizes in some ways that major image programs (Photoshop, I'm lookin' at you) miss when you export them into web-friendly formats.
#6

[eluser]Spir[/eluser]
On my projects I usually make a sub domain for static content : js, css and images.
I have something like this :

/system
/application
/static → static content goes here (http://static.example.com)
/www → index.php goes here (http://example.com)

So I have 2 htaccess

one for static content, another for www (my CI app)

Here is an example of what I usually put in htacces for static content (could be better):

Code:
# Add deflate to static content
AddOutputFilterByType DEFLATE text/css text/plain text/xml application/javascript application/json

# remove ETag from images
Header unset ETag
FileETag none

# Add cace expire to files
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault A86400
    ExpiresByType image/x-icon A2419200
    ExpiresByType image/gif A604800
    ExpiresByType image/png A604800
    ExpiresByType image/jpeg A604800
    ExpiresByType text/css A604800
    ExpiresByType application/x-javascript A604800
    ExpiresByType text/plain A604800
    ExpiresByType application/x-shockwave-flash A604800
    ExpiresByType application/pdf A604800
    ExpiresByType text/html A900
    ExpiresByType text/php "access plus 6 hours"
</IfModule>
Now If I want my css and js to be minify I use that awesome script :
http://code.google.com/p/minify

It permits to minify my CSS and JS on the fly and cache the result so it will be quicker next time.
Example : you have 4 js files :
jquery-1.3.2.min.js
jquery-ui-1.7.2.min.js
jquery-ui-1.7.2.datepicker.min.js
my_script.js

You may have put all files in one JS file : great. But fomr some maintenance and reusage you may keep them in one file. This will result in calling 4 files like this :
Code:
type="text/javascript" src="http://static.example.com/js/jquery-1.3.2.min.js"

Using that code (http://code.google.com/p/minify) you could dynamically minify them in one file like this :
Code:
type="text/javascript" src="http://static.example.com/min/?f=js/jquery-1.3.2.min.js,js/jquery-ui-1.7.2.min.js,js/jquery-ui-1.7.2.datepicker.min.js,js/my_script.js"

Same goes for CSS.

Here is what I have for my www/ folder :

Code:
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond $1 !^(index\.php|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 /index.php
</IfModule>

But you see my content delivered by my app do not use deflate.
Before printing my &lt;html&gt; tag I put this :
ob_start("ob_gzhandler");
and then right after my &lt;/head&gt; tag I put this :
ob_flush(); flush();

You can learn more about this there : http://www.phpied.com/progressive-render...e-flushes/
Now this is not all, I should also minify my html code. Here are some tips :
http://maestric.com/doc/php/codeigniter_compress_html

This few tasks will make your website quicker.




Theme © iAndrew 2016 - Forum software by © MyBB