Last active
June 12, 2016 11:27
-
-
Save lackneets/c79309bea57f04969a366dc4234c27c9 to your computer and use it in GitHub Desktop.
[Recommand] Global managament PNG auto minify on demand using Apache & PHP & pngquant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# OctoberCMS storage/ | |
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
RewriteBase / | |
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$ | |
RewriteRule ^(.*)$ - [E=BASE:%1] | |
# .../xxx.png => minified/.../xxx.png if exists | |
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}minified/$1 -f | |
RewriteRule (.*) %{ENV:BASE}minified/$1 [QSA,L] | |
# .../xxx.png => .../xxx-min.png if exists | |
# RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}$1min/$2-min.$3 -f | |
# RewriteRule ^(.*\/)(.*)\.(png|jpg)$ %{ENV:BASE}$1min/$2-min.$3 [L] | |
# Dont apply if filename match -min.png | |
RewriteCond %{REQUEST_FILENAME} !minified/ | |
RewriteCond %{REQUEST_FILENAME} !-min.png$ | |
RewriteRule ^(.*\.png)$ %{ENV:BASE}pngquant.php [QSA,L] | |
# Not allowed access any php except pngquant.php, redirect to index.php | |
RewriteCond %{REQUEST_FILENAME} .php$ | |
RewriteCond %{REQUEST_FILENAME} !pngquant.php$ | |
RewriteRule ^ index.php [L,NC] | |
# RewriteOptions Inherit | |
# RewriteCond %{REQUEST_URI} !\.png$ | |
# RewriteRule ^ index.php [L,NC] | |
</IfModule> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('display_errors', true); | |
$filename = preg_replace('/\.png(-min)*$/i', '.png', $_SERVER['DOCUMENT_ROOT'] . preg_replace('/\?.+$/', '', $_SERVER['REQUEST_URI'])); | |
$mindir = dirname($filename) . '/min'; | |
$minfile = $mindir . '/' . basename($filename, '.png') . '-min.png'; | |
$minurl = dirname($_SERVER['REQUEST_URI']) . '/min/' . basename($filename, '.png') . '-min.png'; | |
if(!preg_match('/\.png$/', $filename) or !file_exists($filename)){ | |
header("HTTP/1.0 404 Not Found"); exit; | |
} | |
// if mod_rewrite not redirect to minified png | |
if(file_exists($minfile)){ | |
header('location:' . $minurl); exit; | |
} | |
$cmd = 'pngquant --quality 50-70 -'; | |
$descriptorspec = array( | |
0 => array("pipe", "r"), // stdin is a pipe that the child will read from | |
1 => array("pipe", "w"), // stdout is a pipe that the child will write to | |
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to | |
); | |
$process = proc_open($cmd, $descriptorspec, $pipes); | |
if (is_resource($process)) { | |
fwrite($pipes[0], file_get_contents($filename)); // file_get_contents('php://stdin') | |
fclose($pipes[0]); | |
$content = stream_get_contents($pipes[1]); | |
fclose($pipes[1]); | |
@mkdir(dirname($filename) . '/min', 0777); | |
// file_put_contents($minfile, $content); | |
// @chmod($minfile, 0777); | |
// @touch($minfile, filemtime($filename)); | |
$minified = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['BASE'] . 'minified/' . substr($_SERVER['REQUEST_URI'], strlen($_SERVER['BASE'])); | |
@mkdir(dirname($minified), 0777, true); | |
file_put_contents($minified, $content); | |
@chmod($minified, 0777); | |
@touch($minified, filemtime($filename)); | |
$return_value = proc_close($process); | |
// Use apache internal redirection | |
header("Location:" . $_SERVER['REQUEST_URI']); exit; | |
// $fs = stat($minfile); | |
// header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($minfile))." GMT"); | |
// header("Etag: " . sprintf('"%x-%x-%s"', $fs['ino'], $fs['size'],base_convert(str_pad($fs['mtime'],16,"0"),10,16))); | |
// header('Content-type: image/png'); | |
// echo $content; | |
}else{ | |
$fs = stat($filename); | |
header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($filename))." GMT"); | |
header("Etag: " . sprintf('"%x-%x-%s"', $fs['ino'], $fs['size'],base_convert(str_pad($fs['mtime'],16,"0"),10,16))); | |
header('Content-type: image/png'); | |
readfile($filename); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment