While mileage may vary with minifying HTML it is not done automatically like minifying css and js. However there is a background service running for anyone wishing to use it on the server free of charge. Below is the php code to access the service.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
function minifyhtml($html){ $post = array( 'html' => $html ); $url = '127.0.0.1/minhtml.php'; $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_POST, 1); curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($_POST)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_HEADER, 1); $response = curl_exec( $ch ); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($response, 0, $header_size); $body = substr($response, $header_size); $headers = array(); $tmp = explode("n",trim($header)); foreach($tmp as $line){ if (trim($line) != ''){ if (strstr($line,':')){ $line = explode(':',$line); $key = trim($line[0]); unset($line[0]); $line = trim(implode(':',$line)); $headers[$key] = $line; } } } return array($body,$headers); } |
Combined with the above code you can use the below code to retrieve the minified html body as well as the original bytes, minified bytes, and saved percent by minifying the html.
1 2 3 4 5 6 |
list($body,$headers) = minifyhtml($src); $srcBytes = $headers['X-SRCBytes']; $minOutputBytes = $headers['X-OutputBytes']; $savedPercent = $headers['X-SavedPercent']; |