Gzip et PHP [ Compression ] - Programmation
Marsh Posté le 14-02-2002 à 18:28:41
Une petite class qui fait ça tout bien : 
 
Ta page à compresser : 
<?php 
$HTTP_Z = new HTTP_Compress(); 
$HTTP_Z->start(); 
//ici le code de ta page// 
$HTTP_Z->output(); 
?> 
 
La class : 
<? 
class HTTP_Compress { 
     
    /** 
     * Start the output buffer, and make sure that implicit flush is 
     * off so that data is always buffered. 
     * @access public 
     */ 
    function start() 
    { 
        ob_start(); 
        ob_implicit_flush(0); 
    } 
     
    /** 
     * Output the contents of the output buffer, compressed if 
     * desired, along with any relevant headers. 
     * 
     * @param boolean $compress (optional) Use gzip compression, if the browser supports it. 
     * @param boolean $use_etag Generate an ETag, and don't send the body if the browser has the same object cached. 
     * @param boolean $send_body Send the body of the request? Might be false for HEAD requests. 
     * @access public 
     */ 
    function output($compress = true, $use_etag = true, $send_body = true) 
    { 
        $min_gz_size = 1024; 
        $page = ob_get_contents(); 
        $length = strlen($page); 
        ob_end_clean(); 
         
        if ($compress && extension_loaded('zlib' && (strlen($page) > $min_gz_size) && isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_ACCEPT_ENCODING'])) {
 && (strlen($page) > $min_gz_size) && isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_ACCEPT_ENCODING'])) { 
            $ae = explode(',', str_replace(' ', '', $GLOBALS['HTTP_SERVER_VARS']['HTTP_ACCEPT_ENCODING'])); 
            $enc = false; 
            if (in_array('gzip', $ae)) { 
                $enc = 'gzip'; 
            } else if (in_array('x-gzip', $ae)) { 
                $enc = 'x-gzip'; 
            } 
             
            if ($enc) { 
                $page = gzencode($page); 
                $length = strlen($page); 
                header('Content-Encoding: ' . $enc); 
                header('Vary: Accept-Encoding' ;
; 
            } else { 
                $compress = false; 
            } 
        } else { 
            $compress = false; 
        } 
         
        if ($use_etag) { 
            $etag = '"' . md5($page) . '"'; 
            header('ETag: ' . $etag); 
            if (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_IF_NONE_MATCH'])) { 
                $inm = explode(',', $GLOBALS['HTTP_SERVER_VARS']['HTTP_IF_NONE_MATCH']); 
                foreach ($inm as $i) { 
                    if (trim($i) == $etag) { 
                        header('HTTP/1.0 304 Not Modified' ;
; 
                        $send_body = false; 
                        break; 
                    } 
                } 
            } 
        } 
         
        if ($send_body) { 
            header('Content-Length: ' . $length); 
            echo $page; 
        } 
    } 
     
} 
?> 
Marsh Posté le 14-02-2002 à 18:51:57
merci  
  
 
et comment calculer la taille en ko du fichier ?* 
 
parce que, j'ai ajouté 
$contenue = ob_get_contents(); 
echo "poids de la page = ".strlen($contenue)." Ko"; 
 
juste avant le $HTTP_Z->output();  
et il me donne le poinds que j'ai quand je ne compresse pas 
[jfdsdjhfuetppo]--Message édité par GhzMsnet--[/jfdsdjhfuetppo]
Marsh Posté le 14-02-2002 à 18:14:38
<?
ob_start("ob_gzhandler" );
?>
<html>
<body>
txt
<? print "text"; ?>
</body>
</html>
dans ce cas pas ex, TOUT doit etre compressé en Gzip ?
parce que quand j'execute un exemple dans ce style, avec plus de texte, il n'y aucune compression
A moins, que ca soit mon script pour calculer la taille du fichier
mais, je pense pas