当前位置:Gxlcms > PHP教程 > 仿GD读取bmp函数

仿GD读取bmp函数

时间:2021-07-01 10:21:17 帮助过:24人阅读

最近在工作中需要把bmp图片转成jpg图片,但无奈的是网上能找到的bmp转gd的类或函数或多或少都有些问题。其中绝大多数在处理16位bmp图片时要么全黑全粉,要么干脆转换不出,其余有些根本不支持32位的bmp图片。
最后我终于在php的官网上找到了解决方法。原来网上流传最广的一个仿GD的imagecreatefrombmp函数是一个有问题的版本,无法正确处理16位bmp图片,但好在官网上有人给出了修正方法,修正好的代码如下:
  1. /**
  2. * BMP 创建函数
  3. * @author simon
  4. * @modified by 天心流水
  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['decal'] = ($BMP['width'] * $BMP['bytes_per_pixel'] / 4);
  22. $BMP['decal'] -= floor( $BMP['width'] * $BMP['bytes_per_pixel'] / 4 );
  23. $BMP['decal'] = 4 - (4 * $BMP['decal']);
  24. if ( $BMP['decal'] == 4 )
  25. $BMP['decal'] = 0;
  26. $PALETTE = array();
  27. if ($BMP['colors'] < 16777216 && $BMP['colors'] != 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['decal'];
  94. }
  95. fclose( $f1 );
  96. return $res;
  97. }

人气教程排行