Imitation GD reading bmp function

WBOY
Release: 2016-07-25 08:49:47
Original
1239 people have browsed it
Recently, I need to convert bmp pictures to jpg pictures at work, but unfortunately, the classes or functions for converting bmp to gd that can be found on the Internet have more or less problems. Most of them are either all black and all pink when processing 16-bit bmp images, or simply cannot be converted, and some of the others do not support 32-bit bmp images at all.
Finally, I finally found a solution on the official website of php. It turns out that the most widely circulated GD-like imagecreatefrombmp function is a problematic version and cannot correctly handle 16-bit bmp images. Fortunately, someone on the official website has given a correction method. The corrected code is as follows:
  1. /**
  2. * BMP creation function
  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. }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template