Skip to content

Instantly share code, notes, and snippets.

@alixaxel
Created May 13, 2013 12:04
Show Gist options
  • Save alixaxel/5567844 to your computer and use it in GitHub Desktop.
Save alixaxel/5567844 to your computer and use it in GitHub Desktop.
<?php
function gzip_static($path)
{
if ((extension_loaded('zlib') === true) && (is_file($path) === true))
{
$levels = array();
$content = file_get_contents($path);
foreach (range(1, 9) as $level)
{
$levels[$level] = strlen(gzencode($content, $level));
}
if ((count($levels = array_filter($levels)) > 0) && (min($levels) < strlen($content)))
{
if (file_put_contents($path . '.gz', gzencode($content, array_search(min($levels), $levels)), LOCK_EX) !== false)
{
return touch($path . '.gz', filemtime($path), fileatime($path));
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment