Created
January 15, 2020 13:39
-
-
Save stephanschuler/212bdf920c3327f78e1024a1bf6c0350 to your computer and use it in GitHub Desktop.
mysql_compress and mysql_uncompress
This file contains 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 | |
declare(strict_types=1); | |
function mysql_compress($base64 = null): string | |
{ | |
if (!$base64) { | |
return ''; | |
} | |
$size = pack('V', strlen((string)$base64)); | |
$body = gzcompress($base64, 9, ZLIB_ENCODING_DEFLATE); | |
return base64_encode($size . $body); | |
} | |
$content = file_get_contents('php://stdin'); | |
echo mysql_compress($content); |
This file contains 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 | |
declare(strict_types=1); | |
function mysql_uncompress($base64 = null): string | |
{ | |
if (!$base64) { | |
return ''; | |
} | |
$binary = base64_decode((string)$base64); | |
$size = current(unpack('V', substr($binary, 0, 4))); | |
$body = substr($binary, 4); | |
// FIXME: Should be `gzuncompress($body, $size)` but some data is invalid. | |
$result = gzuncompress($body); | |
return $result; | |
} | |
$content = file_get_contents('php://stdin'); | |
echo mysql_uncompress($content); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment