Home > Backend Development > PHP Tutorial > LZW compression algorithm

LZW compression algorithm

WBOY
Release: 2016-07-25 09:07:59
Original
1639 people have browsed it
PHP实现的LZW压缩算法
  1. /**
  2. * @link http://code.google.com/p/php-lzw/
  3. * @author Jakub Vrana, http://php.vrana.cz/
  4. * @copyright 2009 Jakub Vrana
  5. * @license http: //www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  6. */
  7. /**LZW compression
  8. * @param string data to compress
  9. * @return string binary data
  10. */
  11. function lzw_compress($string) {
  12. // compression
  13. $dictionary = array_flip(range("", "xFF"));
  14. $word = "";
  15. $codes = array();
  16. for ($i=0; $i <= strlen($string); $i++) {
  17. $x = $string[$i];
  18. if (strlen($x) && isset($dictionary[$word . $x])) {
  19. $word .= $x;
  20. } elseif ($i) {
  21. $codes[] = $dictionary[$word];
  22. $dictionary[$word . $x] = count($dictionary);
  23. $word = $x;
  24. }
  25. }
  26. // convert codes to binary string
  27. $dictionary_count = 256;
  28. $bits = 8; // ceil(log($dictionary_count, 2))
  29. $return = "";
  30. $rest = 0;
  31. $rest_length = 0;
  32. foreach ($codes as $code) {
  33. $rest = ($rest << $bits) + $code;
  34. $rest_length += $bits;
  35. $dictionary_count++;
  36. if ($dictionary_count > (1 << $bits)) {
  37. $bits++;
  38. }
  39. while ($rest_length > 7) {
  40. $rest_length -= 8;
  41. $return .= chr($rest >> $rest_length);
  42. $rest &= (1 << $rest_length) - 1;
  43. }
  44. }
  45. return $return . ($rest_length ? chr($rest << (8 - $rest_length)) : "");
  46. }
  47. /**LZW decompression
  48. * @param string compressed binary data
  49. * @return string original data
  50. */
  51. function lzw_decompress($binary) {
  52. // convert binary string to codes
  53. $dictionary_count = 256;
  54. $bits = 8; // ceil(log($dictionary_count, 2))
  55. $codes = array();
  56. $rest = 0;
  57. $rest_length = 0;
  58. for ($i=0; $i < strlen($binary); $i++) {
  59. $rest = ($rest << 8) + ord($binary[$i]);
  60. $rest_length += 8;
  61. if ($rest_length >= $bits) {
  62. $rest_length -= $bits;
  63. $codes[] = $rest >> $rest_length;
  64. $rest &= (1 << $rest_length) - 1;
  65. $dictionary_count++;
  66. if ($dictionary_count > (1 << $bits)) {
  67. $bits++;
  68. }
  69. }
  70. }
  71. // decompression
  72. $dictionary = range("", "xFF");
  73. $return = "";
  74. foreach ($codes as $i => $code) {
  75. $element = $dictionary[$code];
  76. if (!isset($element)) {
  77. $element = $word . $word[0];
  78. }
  79. $return .= $element;
  80. if ($i) {
  81. $dictionary[] = $word . $element[0];
  82. }
  83. $word = $element;
  84. }
  85. return $return;
  86. }
  87. $data = "";
  88. $compressed = lzw_compress($data);
  89. var_dump($data === lzw_decompress($compressed));
  90. ?>
复制代码


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template