PHP scripts to compress HTML,CSS,JS

Discussion in 'PHP' started by FLOOKSpie, Jun 11, 2016.

  1. #1
    Hi guys, is anybody here has PHP scripts that compress CSS, HTML and JS ?
     
    FLOOKSpie, Jun 11, 2016 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    Anveto, Jun 11, 2016 IP
  3. FLOOKSpie

    FLOOKSpie Peon

    Messages:
    4
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #3
    thanks
     
    FLOOKSpie, Jun 11, 2016 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    API that takes care of code and has no limit of minifying (so far).
    You can use this service using curl within php like this:

    1. Create a function for posting your file data to API
    
      function curl_post($url, $post="")
      {
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_POST, $post);
          curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($post) ? http_build_query($post) : $post);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
          $server_output = curl_exec ($ch);
          curl_close ($ch); 
          return $server_output;
      }
    
    PHP:
    2. Minifying your CSS file
    
    $css_data = file_get_contents('my_old.css');
    $new_data = json_decode(curl_post('http://servicebull.com/minify-css', $css_data), TRUE);
    //for display use print $new_data['data']['minified']
    //for saving css back
    file_put_contents('my_new.css', $new_data['data']['minified']);
    
    PHP:
    3. Minifying your JS file
    
    $js_data = file_get_contents('my_old.js');
    $new_data = json_decode(curl_post('http://servicebull.com/minify-js', $js_data), TRUE);
    //for display use print $new_data['data']['minified'];
    //for saving jsback
    file_put_contents('my_new.js', $new_data['data']['minified']);
    
    PHP:
    4. Minifying your HTML file
    
    $html_data = file_get_contents('my_old.html');
    $new_data = json_decode(curl_post('http://servicebull.com/minify-html', $js_data), TRUE);
    //for display use print $new_data['data']['minified'];
    //for saving jsback
    file_put_contents('my_new.html', $new_data['data']['minified']);
    
    PHP:
    BTW : You can print_r($new_data) to see the difference and size reduction as well.

    NOTE: http://servicebull.com/<SERVICE NAME HERE>
    i.e. minify-css / minify-js / minify-html

    I hope it helps.

    stay well....
     
    Last edited: Jun 11, 2016
    Vooler, Jun 11, 2016 IP
  5. FLOOKSpie

    FLOOKSpie Peon

    Messages:
    4
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #5
    Thanks for the replies guys
     
    FLOOKSpie, Jun 11, 2016 IP
    Vooler likes this.
  6. FLOOKSpie

    FLOOKSpie Peon

    Messages:
    4
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #6
    Thanks for the replies guys
     
    FLOOKSpie, Jun 11, 2016 IP
  7. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #7
    One correction

    It should be:
    $new_data['data']['output']
    PHP:
    Instead of
    $new_data['data']['minified']

    stay well...
     
    Vooler, Jun 13, 2016 IP