Foros del Web » Programando para Internet » PHP »

Comprimir y Minificar Archivos js

Estas en el tema de Comprimir y Minificar Archivos js en el foro de PHP en Foros del Web. Buenas gente: Saludos a todos los foreros del web. Les cuento, me tengo un lío porque pos no se que hacer, ya se me cerró ...
  #1 (permalink)  
Antiguo 24/04/2013, 13:43
 
Fecha de Ingreso: diciembre-2008
Mensajes: 33
Antigüedad: 15 años, 4 meses
Puntos: 0
Pregunta Comprimir y Minificar Archivos js

Buenas gente:

Saludos a todos los foreros del web.

Les cuento, me tengo un lío porque pos no se que hacer, ya se me cerró el foco.

Tengo el siguiente código:

Código PHP:
<?php 
/*------------------------------------------------------------------------
# author    Oscar Ruíz
# copyright Copyright © 2011 servicioswebcr.com. All rights reserved.
# @license  http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Website   http://www.servicioswebcr.com
-------------------------------------------------------------------------*/

/*INCLUIR ARCHIVOS A COMPRIMIR*/
$files = array(
    
'jquery.min.js',
    
'bootstrap.js',
    
'countdown.js',
    
'jquery-ui.min.js',
    
'jquery.fittext.js'
);
    
$modified 0;
/*FIN INCLUIR ARCHIVOS A COMPRIMIR*/


foreach($files as $file) {        
    
$age filemtime($file);
    if(
$age $modified) {
        
$modified $age;
    }
}

$offset 60 60 24 7// Memoria caché para una semana
header ('Expires: ' gmdate ("D, d M Y H:i:s"time() + $offset) . ' GMT');

if (isset(
$_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $modified) {
    
header("HTTP/1.0 304 Not Modified");
    
header ('Cache-Control:');
} else {
    
header ('Cache-Control: max-age=' $offset);
    
header ('Content-type: text/javascript; charset=UTF-8');
    
header ('Pragma:');
    
header ("Last-Modified: ".gmdate("D, d M Y H:i:s"$modified )." GMT");
    
    function 
compress($buffer) {
        
/* eliminar los comentarios */
        
$buffer preg_replace("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/"""$buffer);
        
/* eliminar tabulaciones, espacios, saltos de línea, etc. */
        
$buffer str_replace(array("\r\n","\r","\t","\n",'  ','    ','     '), ''$buffer);
        
/* eliminar otros espacios antes / después ) */
        
$buffer preg_replace(array('(( )+\))','(\)( )+)'), ')'$buffer);
        return 
$buffer;
    }
    
    
ob_start('ob_gzhandler');

        foreach(
$files as $file) {
            if(
strpos(basename($file),'.min.')===false) { //comprimir archivos que no se minified
                
ob_start("compress");
                    include(
$file);
                
ob_end_flush();
            } else {
                include(
$file);
            }
        }
    
    
ob_end_flush();
}
?>
El problema que me tengo es que no me lee los archivos javascript me dice que la conexión ha sido reiniciada y viendo los log de mi servidor (Wamp Server) me dice lo siguiente:

Cita:
Wed Apr 24 04:05:14 2013] [notice] Parent: child process exited with status 255 -- Restarting.
[Wed Apr 24 04:05:14 2013] [notice] Apache/2.2.22 (Win32) PHP/5.4.3 configured -- resuming normal operations
[Wed Apr 24 04:05:14 2013] [notice] Server built: May 13 2012 13:32:42
[Wed Apr 24 04:05:14 2013] [notice] Parent: Created child process 8012
[Wed Apr 24 04:05:14 2013] [notice] Child 8012: Child process is running
[Wed Apr 24 04:05:14 2013] [notice] Child 8012: Acquired the start mutex.
[Wed Apr 24 04:05:14 2013] [notice] Child 8012: Starting 64 worker threads.
[Wed Apr 24 04:05:14 2013] [notice] Child 8012: Starting thread to listen on port 80.
[Wed Apr 24 04:05:14 2013] [notice] Child 8012: Starting thread to listen on port 80.
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP Warning: Cannot modify header information - headers already sent by (output started at D:\\Template\\js\\template.js.php:3) in D:\\Template\\js\\template.js.php on line 30
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP Stack trace:
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP 1. {main}() D:\\Template\\js\\template.js.php:0
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP 2. header() D:\\Template\\js\\template.js.php:30
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP Warning: Cannot modify header information - headers already sent by (output started at D:\\Template\\js\\template.js.php:3) in D:\\Template\\js\\template.js.php on line 32
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP Stack trace:
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP 1. {main}() D:\\Template\\js\\template.js.php:0
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP 2. header() D:\\Template\\js\\template.js.php:32
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP Warning: Cannot modify header information - headers already sent by (output started at D:\\Template\\js\\template.js.php:3) in D:\\Template\\js\\template.js.php on line 38
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP Stack trace:
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP 1. {main}() D:\\Template\\js\\template.js.php:0
[Wed Apr 24 04:05:14 2013] [error] [client 127.0.0.1] PHP 2. header() D:\\Template\\js\\template.js.php:38
[Wed Apr 24 04:05:14 2013] [notice] Parent: child process exited with status 255 -- Restarting.
[Wed Apr 24 04:05:14 2013] [notice] Apache/2.2.22 (Win32) PHP/5.4.3 configured -- resuming normal operations
[Wed Apr 24 04:05:14 2013] [notice] Server built: May 13 2012 13:32:42
[Wed Apr 24 04:05:14 2013] [notice] Parent: Created child process 3604
[Wed Apr 24 04:05:14 2013] [notice] Child 3604: Child process is running
[Wed Apr 24 04:05:14 2013] [notice] Child 3604: Acquired the start mutex.
[Wed Apr 24 04:05:14 2013] [notice] Child 3604: Starting 64 worker threads.
[Wed Apr 24 04:05:14 2013] [notice] Child 3604: Starting thread to listen on port 80.
[Wed Apr 24 04:05:14 2013] [notice] Child 3604: Starting thread to listen on port 80.
Por favor si alguien me puede ayudar a ver que rayos esta pasando.

Gracias de antemano a todos mi gente.

Saludos
  #2 (permalink)  
Antiguo 24/04/2013, 14:36
Avatar de dashtrash
Colaborador
 
Fecha de Ingreso: abril-2007
Ubicación: Ni en Sevilla,ni en Sanlúcar..qué más da..
Mensajes: 927
Antigüedad: 17 años
Puntos: 270
Respuesta: Comprimir y Minificar Archivos js

Lo único que dice ese log es que estás intentando enviar headers, desde el fichero D:\\Template\\js\\template.js.php:32) después de que haya comenzado la salida html en D:\\Template\\js\\template.js.php:3)

Por cierto..Mejor si comprimes el fichero si no existe, y si existe, lo envias tal cual..No tienes que recrearlo en cada request..
  #3 (permalink)  
Antiguo 24/04/2013, 14:52
 
Fecha de Ingreso: diciembre-2008
Mensajes: 33
Antigüedad: 15 años, 4 meses
Puntos: 0
Pregunta Respuesta: Comprimir y Minificar Archivos js

Cita:
Iniciado por dashtrash Ver Mensaje
Lo único que dice ese log es que estás intentando enviar headers, desde el fichero D:\\Template\\js\\template.js.php:32) después de que haya comenzado la salida html en D:\\Template\\js\\template.js.php:3)

Por cierto..Mejor si comprimes el fichero si no existe, y si existe, lo envias tal cual..No tienes que recrearlo en cada request..
Hola dashtrash, muchas gracias por la pronta respuesta.

Suena interesante lo que me dijistes con respecto a comprimir el fichero, como se podría hacer...?

Gracias

Etiquetas: comprimir
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:20.