PHP imitation GD generated BMP image

Source: Internet
Author: User
Tags bmp image
PHP imitation GD generated BMP image
  1. Function Imagebmp (& $im, $filename = ", $bit = 8, $compression = 0)
  2. {
  3. if (!in_array ($bit, Array (1, 4, 8, 16, 24, 32)))
  4. {
  5. $bit = 8;
  6. }
  7. else if ($bit = = +)//todo:32 bit
  8. {
  9. $bit = 24;
  10. }
  11. $bits = POW (2, $bit);
  12. Adjust Palette
  13. Imagetruecolortopalette ($im, True, $bits);
  14. $width = Imagesx ($im);
  15. $height = Imagesy ($im);
  16. $colors _num = Imagecolorstotal ($im);
  17. if ($bit <= 8)
  18. {
  19. Color Index
  20. $rgb _quad = ";
  21. for ($i = 0; $i < $colors _num; $i + +)
  22. {
  23. $colors = Imagecolorsforindex ($im, $i);
  24. $rgb _quad. = Chr ($colors [' Blue ']). Chr ($colors [' green ']). Chr ($colors [' Red ']). "The";
  25. }
  26. Bitmap data
  27. $bmp _data = ";
  28. Non-compressed
  29. if ($compression = = 0 | | $bit < 8)
  30. {
  31. if (!in_array ($bit, Array (1, 4, 8)))
  32. {
  33. $bit = 8;
  34. }
  35. $compression = 0;
  36. The number of bytes per line must be a multiple of 4, padded.
  37. $extra = ";
  38. $padding = 4-ceil ($width/(8/$bit))% 4;
  39. if ($padding% 4! = 0)
  40. {
  41. $extra = Str_repeat ("n", $padding);
  42. }
  43. for ($j = $height-1; $j >= 0; $j-)
  44. {
  45. $i = 0;
  46. while ($i < $width)
  47. {
  48. $bin = 0;
  49. $limit = $width-$i < 8/$bit? (8/$bit-$width + $i) * $bit: 0;
  50. for ($k = 8-$bit; $k >= $limit; $k-= $bit)
  51. {
  52. $index = Imagecolorat ($im, $i, $j);
  53. $bin |= $index << $k;
  54. $i + +;
  55. }
  56. $bmp _data. = Chr ($bin);
  57. }
  58. $bmp _data. = $extra;
  59. }
  60. }
  61. RLE8 compression
  62. else if ($compression = = = 1 && $bit = = 8)
  63. {
  64. for ($j = $height-1; $j >= 0; $j-)
  65. {
  66. $last _index = "n";
  67. $same _num = 0;
  68. for ($i = 0; $i <= $width; $i + +)
  69. {
  70. $index = Imagecolorat ($im, $i, $j);
  71. if ($index!== $last _index | | $same _num > 255)
  72. {
  73. if ($same _num! = 0)
  74. {
  75. $bmp _data. = Chr ($same _num). Chr ($last _index);
  76. }
  77. $last _index = $index;
  78. $same _num = 1;
  79. }
  80. Else
  81. {
  82. $same _num + +;
  83. }
  84. }
  85. $bmp _data. = "\0\0";
  86. }
  87. $bmp _data. = "\0\1";
  88. }
  89. $size _quad = strlen ($rgb _quad);
  90. $size _data = strlen ($bmp _data);
  91. }
  92. Else
  93. {
  94. The number of bytes per line must be a multiple of 4, padded.
  95. $extra = ";
  96. $padding = 4-($width * ($bit/8))% 4;
  97. if ($padding% 4! = 0)
  98. {
  99. $extra = Str_repeat ("n", $padding);
  100. }
  101. Bitmap data
  102. $bmp _data = ";
  103. for ($j = $height-1; $j >= 0; $j-)
  104. {
  105. for ($i = 0; $i < $width; $i + +)
  106. {
  107. $index = Imagecolorat ($im, $i, $j);
  108. $colors = Imagecolorsforindex ($im, $index);
  109. if ($bit = = 16)
  110. {
  111. $bin = 0 << $bit;
  112. $bin |= ($colors [' Red '] >> 3) << 10;
  113. $bin |= ($colors [' Green '] >> 3) << 5;
  114. $bin |= $colors [' Blue '] >> 3;
  115. $bmp _data. = Pack ("V", $bin);
  116. }
  117. Else
  118. {
  119. $bmp _data. = Pack ("c*", $colors [' Blue '], $colors [' Green '], $colors [' Red '];
  120. }
  121. Todo:32bit;
  122. }
  123. $bmp _data. = $extra;
  124. }
  125. $size _quad = 0;
  126. $size _data = strlen ($bmp _data);
  127. $colors _num = 0;
  128. }
  129. Bitmap file Header
  130. $file _header = "BM". Pack ("V3", si + $size _quad + $size _data, 0, + $size _quad);
  131. Bitmap Information Header
  132. $info _header = Pack ("v3v2v*", 0x28, $width, $height, 1, $bit, $compression, $size _data, 0, 0, $colors _num, 0);
  133. Write file
  134. if ($filename! = ")
  135. {
  136. $fp = fopen ($filename, "WB");
  137. Fwrite ($fp, $file _header);
  138. Fwrite ($fp, $info _header);
  139. Fwrite ($fp, $rgb _quad);
  140. Fwrite ($fp, $bmp _data);
  141. Fclose ($FP);
  142. return true;
  143. }
  144. Browser output
  145. Header ("Content-type:image/bmp");
  146. Echo $file _header. $info _header;
  147. echo $rgb _quad;
  148. echo $bmp _data;
  149. return true;
  150. }
Copy Code
  • Related Article

    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.