user1105934
user1105934

Reputation:

How can I set gzip compression in zend framework website

I am new to zend. I have developed a website using zend framework. Now, I want to set gzip compression in my website. Would you please guide me step wise to implement this.

Thanks in advance. kamal Arora

Upvotes: 4

Views: 3808

Answers (3)

Syed Abidur Rahman
Syed Abidur Rahman

Reputation: 947

Honoring the answer of Bruno Pitteli, I think you can compress in the following way:

$search = array(
    '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
    '/[^\S ]+\</s',  // strip whitespaces before tags, except space
    '/(\s)+/s',       // shorten multiple whitespace sequences
    '#(?://)?<![CDATA[(.*?)(?://)?]]>#s' //leave CDATA alone
);

$replace = array(
    '>',
    '<',
    '\\1',
    "//&lt;![CDATA[n".'1'."n//]]>"
);

$content = preg_replace($search, $replace, $content);

So the full code sample now looks like:

public function onBootstrap(MvcEvent $e)
{
    $eventManager = $e->getApplication()->getEventManager();
    $eventManager->attach("finish", array($this, "compressOutput"), 100);
}

public function compressOutput($e) 
{
    $response = $e->getResponse();
    $content = $response->getBody(); 
    $content = preg_replace(array('/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s', '#(?://)?<![CDATA[(.*?)(?://)?]]>#s'), array('>', '<', '\\1', "//&lt;![CDATA[n".'1'."n//]]>"), $content);

    if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
        header('Content-Encoding: gzip');
        $content = gzencode($content, 9);
    }

    $response->setContent($content);
}

Upvotes: 1

I made for zend framework 2 (zf2) with your tip

public function onBootstrap(MvcEvent $e)
{
    $eventManager = $e->getApplication()->getEventManager();
    $eventManager->attach("finish", array($this, "compressOutput"), 100);
}

public function compressOutput($e) 
{
    $response = $e->getResponse();
    $content = $response->getBody();
    $content = str_replace("  ", " ", str_replace("\n", " ", str_replace("\r", " ", str_replace("\t", " ", $content))));

    if(@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)
    {
        header('Content-Encoding: gzip');
        $content = gzencode($content, 9);
    }

    $response->setContent($content);
}

Upvotes: 5

Mehdi Fanai
Mehdi Fanai

Reputation: 4059

There are two methods to gzip output in your website.

  1. Using Webserver.If your webserver is apache you can refer here for a good documentation on how to enable mod_deflate on your server.

  2. Using zend framework. Try the following code which is from this website. Create a gzip compressed string in your bootstrap file.

Code:

try {
 $frontController = Zend_Controller_Front::getInstance();
 if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
    ob_start();
    $frontController->dispatch();
    $output = gzencode(ob_get_contents(), 9);
    ob_end_clean();
    header('Content-Encoding: gzip');
    echo $output;
} else {
    $frontController->dispatch();
}
} catch (Exeption $e) {
if (Zend_Registry::isRegistered('Zend_Log')) {
    Zend_Registry::get('Zend_Log')->err($e->getMessage());
}
$message = $e->getMessage() . "\n\n" . $e->getTraceAsString();
/* trigger event */
}

GZIP does not compress images, just the raw HTML/CSS/JS/XML/JSON code from the site being sent to the user.

Upvotes: 5

Related Questions