LZW compression algorithm-php Tutorial

Source: Internet
Author: User
LZW compression algorithm
LZW compression algorithm implemented by PHP

  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 ("\ 0", "\ 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 ("\ 0", "\ 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. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.