Read bmp functions like GD

Source: Internet
Author: User
Read bmp functions like GD
Recently, we need to convert bmp images into jpg images, but what we can find on the internet is that bmp-to-gd classes or functions are more or less problematic. Most of the 16-bit bmp images are either completely black and completely powdered, or cannot be converted, while others do not support 32-bit bmp images at all.
Finally, I found a solution on the php official website. The most widely circulated imagecreatefrombmp function on the Internet, similar to GD, is a problematic version and cannot correctly process 16-bit bmp images. However, some people have provided correction methods on the official website, the corrected code is as follows:

  1. /**
  2. * BMP creation function
  3. * @ Author simon
  4. * @ Modified
  5. * @ Param string $ filename path of bmp file
  6. * @ Example who use, who knows
  7. * @ Return resource of GD
  8. */
  9. Function imagecreatefrombmp ($ filename ){
  10. If (! $ F1 = fopen ($ filename, "rb "))
  11. Return FALSE;
  12. $ FILE = unpack ("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread ($ f1, 14 ));
  13. If ($ FILE ['File _ type']! = 19778)
  14. Return FALSE;
  15. $ BMP = unpack ('vheader _ size/Vwidth/Vheight/vplanes/vbits_per_pixel '. '/Vcompression/Vsize_bitmap/Vhoriz_resolution '. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread ($ f1, 40 ));
  16. $ BMP ['Colors '] = pow (2, $ BMP ['bits _ per_pixel']);
  17. If ($ BMP ['size _ bitmap'] = 0)
  18. $ BMP ['size _ bitmap'] = $ FILE ['File _ size']-$ file ['bitmap _ offset'];
  19. $ BMP ['bytes _ per_pixel '] = $ BMP ['bits _ per_pixel']/8;
  20. $ BMP ['bytes _ per_pixel2 '] = ceil ($ BMP ['bytes _ per_pixel']);
  21. $ BMP ['desc'] = ($ BMP ['width'] * $ BMP ['bytes _ per_pixel ']/4 );
  22. $ BMP ['desc']-= floor ($ BMP ['width'] * $ BMP ['bytes _ per_pixel ']/4 );
  23. $ BMP ['demo'] = 4-(4 * $ BMP ['demo']);
  24. If ($ BMP ['desc'] = 4)
  25. $ BMP ['desc'] = 0;
  26. $ PALETTE = array ();
  27. If ($ BMP ['color'] <16777216 & $ BMP ['color']! = 65536)
  28. {
  29. $ PALETTE = unpack ('v'. $ BMP ['Colors '], fread ($ f1, $ BMP ['Colors'] * 4 ));
  30. }
  31. $ IMG = fread ($ f1, $ BMP ['size _ bitmap']);
  32. $ VIDE = chr (0 );
  33. $ Res = imagecreatetruecolor ($ BMP ['width'], $ BMP ['height']);
  34. $ P = 0;
  35. $ Y = $ BMP ['height']-1;
  36. While ($ Y> = 0 ){
  37. $ X = 0;
  38. While ($ X <$ BMP ['width']) {
  39. If ($ BMP ['bits _ per_pixel '] = 32 ){
  40. $ COLOR = unpack ("V", substr ($ IMG, $ P, 3 ));
  41. $ B = ord (substr ($ IMG, $ P, 1 ));
  42. $ G = ord (substr ($ IMG, $ P + 1, 1 ));
  43. $ R = ord (substr ($ IMG, $ P + 2, 1 ));
  44. $ Color = imagecolorexact ($ res, $ R, $ G, $ B );
  45. If ($ color =-1)
  46. $ Color = imagecolorallocate ($ res, $ R, $ G, $ B );
  47. $ COLOR [0] = $ R * 256*256 + $ G * 256 + $ B;
  48. $ COLOR [1] = $ color;
  49. } Elseif ($ BMP ['bits _ per_pixel '] = 24 ){
  50. $ COLOR = unpack ("V", substr ($ IMG, $ P, 3). $ VIDE );
  51. } Elseif ($ BMP ['bits _ per_pixel '] = 16 ){
  52. $ COLOR = unpack ("v", substr ($ IMG, $ P, 2 ));
  53. $ Blue = ($ COLOR [1] & 0x001f) <3) + 7;
  54. $ Green = ($ COLOR [1] & 0x03e0)> 2) + 7;
  55. $ Red = ($ COLOR [1] & 0xfc00)> 7) + 7;
  56. $ COLOR [1] = $ red * 65536 + $ green * 256 + $ blue;
  57. } Elseif ($ BMP ['bits _ per_pixel '] = 8 ){
  58. $ COLOR = unpack ("n", $ VIDE. substr ($ IMG, $ P, 1 ));
  59. $ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
  60. } Elseif ($ BMP ['bits _ per_pixel '] = 4 ){
  61. $ COLOR = unpack ("n", $ VIDE. substr ($ IMG, floor ($ P), 1 ));
  62. If ($ P * 2) % 2 = 0)
  63. $ COLOR [1] = ($ COLOR [1]> 4 );
  64. Else
  65. $ COLOR [1] = ($ COLOR [1] & 0x0F );
  66. $ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
  67. } Elseif ($ BMP ['bits _ per_pixel '] = 1 ){
  68. $ COLOR = unpack ("n", $ VIDE. substr ($ IMG, floor ($ P), 1 ));
  69. If ($ P * 8) % 8 = 0)
  70. $ COLOR [1] = $ COLOR [1]> 7;
  71. Elseif ($ P * 8) % 8 = 1)
  72. $ COLOR [1] = ($ COLOR [1] & 0x40)> 6;
  73. Elseif ($ P * 8) % 8 = 2)
  74. $ COLOR [1] = ($ COLOR [1] & 0x20)> 5;
  75. Elseif ($ P * 8) % 8 = 3)
  76. $ COLOR [1] = ($ COLOR [1] & 0x10)> 4;
  77. Elseif ($ P * 8) % 8 = 4)
  78. $ COLOR [1] = ($ COLOR [1] & 0x8)> 3;
  79. Elseif ($ P * 8) % 8 = 5)
  80. $ COLOR [1] = ($ COLOR [1] & 0x4)> 2;
  81. Elseif ($ P * 8) % 8 = 6)
  82. $ COLOR [1] = ($ COLOR [1] & 0x2)> 1;
  83. Elseif ($ P * 8) % 8 = 7)
  84. $ COLOR [1] = ($ COLOR [1] & 0x1 );
  85. $ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
  86. } Else
  87. Return FALSE;
  88. Imagesetpixel ($ res, $ X, $ Y, $ COLOR [1]);
  89. $ X ++;
  90. $ P + = $ BMP ['bytes _ per_pixel '];
  91. }
  92. $ Y --;
  93. $ P + = $ BMP ['demo'];
  94. }
  95. Fclose ($ f1 );
  96. Return $ res;
  97. }

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.