PHP generates BMP image like GD-php Tutorial

Source: Internet
Author: User
Generate BMP image like GD in PHP
Generate BMP image like GD in PHP

  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 = 32) // todo: 32 bit
  8. {
  9. $ Bit = 24;
  10. }
  11. $ Bits = pow (2, $ bit );
  12. // Adjust the color 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']). "\ 0 ";
  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 in each row must be a multiple of 4.
  37. $ Extra = '';
  38. $ Padding = 4-ceil ($ width/(8/$ bit) % 4;
  39. If ($ padding % 4! = 0)
  40. {
  41. $ Extra = str_repeat ("\ 0", $ 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 = "\ 0 ";
  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 in each row must be a multiple of 4.
  95. $ Extra = '';
  96. $ Padding = 4-($ width * ($ bit/8) % 4;
  97. If ($ padding % 4! = 0)
  98. {
  99. $ Extra = str_repeat ("\ 0", $ 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", 54 + $ size_quad + $ size_data, 0, 54 + $ size_quad );
  131. // Bitmap Header
  132. $ Info_header = pack ("V3v2V *", 0x28, $ width, $ height, 1, $ bit, $ compression, $ size_data, 0, 0, $ colors_num, 0 );
  133. // Write a 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. }

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.