We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7327
    • 195 Posts
    Hi guys,

    I've been reading up on this server-side GZIP compressor thing, but it seems users on shared hosting are out of luck. Luckily though, Hostmonster allows for ZLib compression activation by throwing an editable php.ini file. But Zlib only covers PHP files, so I was looking around for a workaround and then I stumbled on this trick in David Walshe's site:

    http://davidwalsh.name/css-compression-php

    Basically, it says use PHP code to consolidate css files into a single file, squeeze it and take advantage of Zlib's PHP compression to deliver the CSS as a compressed file.

    In my MODX installation, I added a css.css file in my css directory with the following PHP code as instructed (did a little modification here based on the comments there):

    <?php
    $css = '';
    $root = $_SERVER['DOCUMENT_ROOT'].'/assets/path/to/your/css/'; //directory where the css lives
    $files = explode(',',$_SERVER['QUERY_STRING']);
    if(sizeof($files))
    {
      foreach($files as $file)
      {
        $css.= (is_file($root.$file.'.css') ? file_get_contents($root.$file.'.css') : '');
      }
    }
    $css = str_replace(': ', ':', str_replace(';}', '}', str_replace('; ', ';', str_replace(' }', '}', str_replace(' {', '{', str_replace('{ ', '{', str_replace(array("\r\n", "\r", "\n", "\t"), "", preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css))))))));
    header("Content-type: text/css");
    echo $css;
    ?>
    


    I added this condition to my htaccess (top most before rewriteengine is turned on) to treat the css.css file as a php file and interpret it as such:

    <FILES css.css>
      SetHandler  application/x-httpd-php
    </FILES>
    


    Then I replaced my old static css calls with this:
    <link rel="stylesheet" type="text/css" href="assets/path/to/your/css/css.css?your,filenames,sans_extensions,separated,by_commas" />
    


    You can visit http://www.yoursite.com/assets/path/to/your/css/css.css?your,filenames,sans_extensions,separated,by_commas and see what the output is. Saving the text file gave me a 61KB filesize. I checked with Firebug if I was getting any compression benefits out of this, and I was - the css link is reported to have a size of 9.8KB (See attached image).

    Can anyone improve this further or plug any security holes it may have? Or maybe find a way to convert this to a snippet, or give this a sort of caching feature.. Anyone got an idea how much load this code going to hit the CPU?

    UPDATE: This is odd. Sometimes it's compressed, sometimes it's not. I've read in some blog posts that the decision to compress files is dependent on how busy the web server is.. Wonder if this is the case. [ed. note: diatomin last edited this post 13 years, 8 months ago.]
      • 33987 ☆ A M B ☆
      • 197 Posts
      I think you're reinventing the wheel here. This is exactly what Minify does: http://code.google.com/p/minify/
        Sterc Internet & Marketing | MODX Founding Partner | Chairman of the MODX Advisory Board

        In need of a MODX consult? Try our MODX Developers Experts!
        • 37099
        • 338 Posts
        There's also SmartOptimizer http://rtfm.modx.com/display/ADDON/SmartOptimizer

        Which minifies, compresses, concatenates and caches for css and javasript files! (they do have to be in the same folder though)

        It's excellent!

        So I can have different files for different bits of css or javascript code.

        Then just have one call (each one for css one for js) to SmartOptimizer.


        In terms of workflow, you can develop with all the files called separately, so it's easier for developing and debugging.

        Then comment them out with revos unofficial comment syntax: [[# ]] and call them with smart optimizer when you are ready to go live. It's then easy to switch between the two when changes are needed.
          • 1343 ☆ A M B ☆
          • 2,213 Posts
          Quote from: thingstodo at Feb 11, 2013, 01:39 PM

          Then comment them out with revos unofficial comment syntax: [[# ]] and call them with smart optimizer when you are ready to go live. It's then easy to switch between the two when changes are needed.

          Why not use the official comment syntax?
          [[-Commented Stuff]]
            Patrick | Server Wrangler
            About Me: Website | Tweets |  MODX Hosting
            • 37099
            • 338 Posts
            Quote from: AMDbuilder at Feb 11, 2013, 02:19 PM


            Why not use the official comment syntax?
            [[-Commented Stuff]]


            Good idea!
              • 7327
              • 195 Posts
              @thingstodo - I'm gonna try that SmartOptimizer again. I tried it earlier and had trouble working with it, but hey, might be a small detail I missed out on.

              @GP Sietzema - Yeah, I'd hate to redo things that have already been done. I got to this while searching for a way to use GZIP for on-the-fly compression of not just PHP files.

              @AMDbuilder - That's what I've been searching for too.

              Thanks for the feedback guys.
                • 37099
                • 338 Posts
                Quote from: diatomin at Feb 12, 2013, 09:49 AM
                @thingstodo - I'm gonna try that SmartOptimizer again. I tried it earlier and had trouble working with it, but hey, might be a small detail I missed out on.

                The thing that had me scratching my head for a while is the way you call the different files. The files have to be in the same folder.

                Say you have two files
                /assets/css/style_1.css
                &
                /assets/css/style_2.css

                The first one you include the path eg assets/css/style_1.css but the next one just use the filename, like this:

                <link rel="stylesheet" href="[[SmartOptimizer?&files=`assets/css/style_1.css,style_2.css` ]]"/>

                And Bob's your uncle.