Skip to content

Instantly share code, notes, and snippets.

@MidnightLightning
Created February 10, 2016 19:41
Show Gist options
  • Select an option

  • Save MidnightLightning/07bda7acdf5972e6fc45 to your computer and use it in GitHub Desktop.

Select an option

Save MidnightLightning/07bda7acdf5972e6fc45 to your computer and use it in GitHub Desktop.
Comparison of compress and deflate
<?php echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP compressions</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="content-style-type" content="text/css" />
<style type="text/css">
th { text-align:right; }
.line_number { font-weight:bold; }
p.hex { font-family:"Courier New"; font-size:10pt; white-space:pre; }
</style>
</head>
<body>
<?php
// Test the difference in DEFLATE vs. COMPRESS
$str = pack("c*", 50, 60, 100, 255, 255, 255, 0, 0, 16, 16, 50, 60, 100, 255, 255, 255, 0, 0, 16, 16, 50, 60, 100, 255, 255, 255, 0, 0, 16, 16, 50, 60, 100, 255, 255, 255, 0, 16, 16, 16, 50, 60, 100, 255, 255, 255, 0, 1, 16, 10, 50, 60, 100, 235, 255, 245, 0, 0, 16, 16, 50, 60, 100, 255, 255, 255, 0, 0, 16, 16, 214, 186, 196, 72, 40, 92, 129, 184, 139, 27, 193, 42, 228, 129, 174, 156, 207, 157, 104, 207, 84, 108, 137, 108, 75, 67, 83, 11, 9, 198, 136, 34, 90, 68, 81, 117, 209, 101, 25, 144, 85, 137, 8, 85, 19, 141, 161, 45, 88, 53, 55, 58, 2, 195, 153, 249, 114, 53, 62, 16, 211, 4, 102, 100, 121, 254, 19, 196, 4, 194, 238, 68, 216, 192, 187, 20, 240, 141, 20, 57, 99, 135, 213, 27, 105, 249, 58, 55, 211, 43, 161, 226, 172, 6, 197, 226, 132, 108, 11, 49);
echo "<h1>Original</h1>";
echo showHex($str);
$compress = gzcompress($str, 1);
echo "<h1>COMPRESS 1</h1>";
echo showHex($compress);
$compress = gzcompress($str, 5);
echo "<h1>COMPRESS 5</h1>";
echo showHex($compress);
$compress = gzcompress($str, 9);
echo "<h1>COMPRESS 9</h1>";
echo showHex($compress);
$deflate = gzdeflate($str, 1);
echo "<h1>DEFLATE 1</h1>";
echo showHex($deflate);
$deflate = gzdeflate($str, 5);
echo "<h1>DEFLATE 5</h1>";
echo showHex($deflate);
$deflate = gzdeflate($str, 9);
echo "<h1>DEFLATE 9</h1>";
echo showHex($deflate);
function showHex($data, $lineSize = 32) {
$out = "<p class=\"hex\">";
for($i=0; $i<strlen($data); $i+=$lineSize) {
$out .= "<span class=\"line_number\">".sprintf("%04X: ", $i)."</span>";
for($n=0; $n<$lineSize; $n++) {
$x = $n+$i;
$out .= ($x<strlen($data))? sprintf("%02X", ord($data[$x])) : "&nbsp;&nbsp;";
$out .= (fmod($x+1, 8) == 0)? "&emsp;" : "&thinsp;";
}
$out .= "|&nbsp;";
for ($n=0; $n<$lineSize; $n++) {
$x = $n+$i;
$out .= ($x<strlen($data))? htmlentities(readableASCII($data[$x])) : "&nbsp;";
$out .= (fmod($x+1, 8) == 0)? "&emsp;" : "";
}
$out .= "\n";
}
$out .= "</p>";
return $out;
}
function readableASCII($chr) {
if (ord($chr[0]) >= 32 && ord($chr[0]) <= 126) {
return $chr[0];
} else {
return ".";
}
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment