Vous êtes ici Codes PHP et MySQL > Système > Compression de fichiers css js kml xml txt html à...

 

Compression de fichiers css js kml xml txt html à l'envoi au navigateur

Pour optimiser les temps de chargement des pages, il est possible de compresser le contenu avant de l'envoyer au navigateur. Ce petit script permet de faire cette compression pour tous les fichiers physiques de type texte.
Pour l'utiliser il faut disposer du rewriting et ajouter à la fin du fichier .htaccess les deux lignes suivantes :
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).(css|js|kml|xml|xsl|txt|html)$ /compresse.php?fichier=$1.$2&extension=$2 [L]

Il faut également nommer le script compresse.php (ou changer la règle de rewriting).
 
forty
Site de l'auteur voir
[21] sources en PHP voir
Code vu 6517 fois
Enregistré le 29 Fév 2008
  • Digg ce code sur digg.com
  • Bookmark ce code sur del.icio.us
  • Bookmark ce code sur Google
  • Bookmark ce code sur Yahoo
  • Ajoute Compression de fichiers css js kml xml txt html à l'envoi au navigateur
  • Partage ce code sur Facebook
 
 
 
 

Code Source


01.
02.
03.
04.
05.
06.
07.
08.
09.
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.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
 
<?php
/******************************************************************************/
/*                                                                            */
/*                       __        ____                                       */
/*                 ___  / /  ___  / __/__  __ _____________ ___               */
/*                / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-<               */
/*               / .__/_//_/ .__/___/\___/\_,_/_/  \__/\__/___/               */
/*              /_/       /_/                                                 */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/* Titre          : Compression de fichiers css js kml xml txt html à...      */
/*                                                                            */
/* URL            : http://www.phpsources.org/scripts350-PHP.htm              */
/* Auteur         : forty                                                     */
/* Date édition   : 29 Fév 2008                                               */
/* Website auteur : http://www.toplien.fr/                                    */
/*                                                                            */
/******************************************************************************/

//nom du script : compresse.php
//ajouter les lignes suivantes à la fin du fichier .htaccess pour utiliser le
// rewriting automatique
//RewriteCond %{REQUEST_FILENAME} -f
//RewriteRule
// ^(.*).(css|js|kml|xml|xsl|txt|html)$    /compresse.php?fichier=$1.$2&extension=$
//2
// [L]

//supprime les magic quotes
if (get_magic_quotes_gpc()) {
  function noEscape($string) {
    $string is_array($string)? array_map('noEscape'$string): stripslashes(
$string);
    return $string;
  }
  $_GET array_map('noEscape'$_GET);
}

if (!empty($_GET['fichier']) && @file_exists($_GET['fichier'])) { //si le
// fichier existe
  $date_modif = @filemtime(dirname(__FILE__) . '/' $_GET['fichier']);
  if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime(preg_replace(
'/;.*$/'''$_SERVER['HTTP_IF_MODIFIED_SINCE'])) < $date_modif)) {
    header('Status: 304 Not Modified'false304);
    header('Last-Modified: ' gmdate('D, d M Y H:i:s'$date_modif) . ' GMT');
    exit;
  } else {
    header('Status: 200 OK'false200);
    header('Last-Modified: ' gmdate('D, d M Y H:i:s'$date_modif) . ' GMT');
  }
  if (!isset($_SERVER['SCRIPT_URL'])) {
    $uri explode('?'$_SERVER['REQUEST_URI']);
    $_SERVER['SCRIPT_URL'] = $uri[];
  }
  if ($_SERVER['SCRIPT_URL'] == $_SERVER['SCRIPT_NAME']) { 
//oblige a passer par
// le rewriting
    die('error');
  }
  $extension = (empty($_GET['extension'])) ? strrchr(basename($_GET['fichier']),
 '.') : $_GET['extension'];
  if ($extension == 'css') {
    header('Content-Type: text/css');
  } elseif ($extension == 'js') {
    header('Content-Type: application/x-javascript');
  } elseif ($extension == 'kml') {
    header('Content-Type: application/vnd.google-earth.kml+xml');
  } elseif ($extension == 'xml') {
    header('Content-Type: text/xml');
  } elseif ($extension == 'xsl') {
    header('Content-Type: text/xml');
  } elseif ($extension == 'txt') {
    header('Content-Type: text/plain');
  } elseif ($extension == 'html') {
    header('Content-Type: text/html');
  } else {
    die('error');
  }
  if ((ini_get('zlib.output_compression') != true) && function_exists('ob_gzh' .
'andler')) {
    ob_start('ob_gzhandler');  //lance la bufferisation avec compression
  } else {
    ob_start();  //lance la bufferisation sans compression
    header('Content-Length: ' filesize($_GET['fichier']));
  } 
  readfile($_GET['fichier']);
  ob_end_flush();  //envoie les données
} else {
  die('error');
}
?>

 

Fonctions du code

: Retourne la configuration actuelle de l'option magic_quotes_gpc - (PHP 4, PHP 5)
: Applique une fonction sur les éléments d'un tableau - (PHP 4 >= 4.0.6, PHP 5)
: Détermine si une variable est un tableau - (PHP 4, PHP 5)
: Supprime les antislashs d'une chaîne - (PHP 4, PHP 5)
: Détermine si une variable contient une valeur non nulle - (PHP 4, PHP 5)
: Vérifie si un fichier ou un dossier existe - (PHP 4, PHP 5)
: Renvoie le nom du dossier - (PHP 4, PHP 5)
: Lit la date de dernière modification du fichier - (PHP 4, PHP 5)
: Rechercher et remplacer par expression rationnelle standard - (PHP 4, PHP 5)
: - (PHP 4, PHP 5)
: Envoie un en-tête HTTP - (PHP 4, PHP 5)
: Formate une date/heure GMT/CUT - (PHP 4, PHP 5)
: Affiche un message et termine le script courant - (PHP 4, PHP 5)
: Détermine si une variable est affectée - (PHP 4, PHP 5)
: Coupe une chaîne en segments - (PHP 4, PHP 5)
: Alias de la fonction exit() - (PHP 4, PHP 5)
: Sépare le nom du fichier et le nom du dossier - (PHP 4, PHP 5)
: Trouve la dernière occurrence d'un caractère dans une chaîne - (PHP 4, PHP 5)
: Indique si une fonction est définie - (PHP 4, PHP 5)
: Lit la valeur d'une option de configuration - (PHP 4, PHP 5)
: Enclenche la tamporisation de sortie - (PHP 4, PHP 5)
: Lit la taille d'un fichier - (PHP 4, PHP 5)
: Affiche un fichier - (PHP 4, PHP 5)
: Envoie les données du tampon de sortie et éteint la tamporisation de sortie - (PHP 4,...
Commentaire
 Bonjour, on peut également utiliser les modules d'apaches (mod_deflate ou mod_gzip et mod_cache) ce qui sera plus performant.
 
Ajouter un commentaire
Code de sécurité

Attention: Les champs marqués d'une étoile * sont obligatoires
 
Librairie PHP

Connexion

 
 

Système

 
 

PHP

 
 
 
 
    Offres d'emploi

Plus de 500 offres d'emploi PHP/MySQL

Offres d'emploi
 
        Publicité