首頁 > 後端開發 > php教程 > 仿GD讀取bmp函數

仿GD讀取bmp函數

WBOY
發布: 2016-07-25 08:49:47
原創
1256 人瀏覽過
最近在工作上需要把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. function imagecreatefrombmp( $filename ) {
  8. if ( !$f1 = fopen( $filename, "rb" ) )
  9. return FALSE;
  10. $FILE = unpack( "vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread( $f1, 14 ) );
  11. if ( $FILE['file_type' 78 )
  12. return FALSE;
  13. $BMP = unpack( 'Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel' . '/Vcompression/Vsize_bitmap/Vhoriz_resol_pixel' . '/Vcompression/Vsize_bitmap/Vhoriz_resolution' .color/Voported $f1, 40 ) );
  14. $BMP['colors'] = pow( 2, $BMP['bits_per_pixel'] );
  15. if ( $BMP['size_bitmap'] == 0 )
  16. $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  17. $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel'] / 8;
  18. $BMP['bits_per_pixel'] / 8;
  19. $BMP ['bytes_per_pixel2'] = ceil( $BMP['bytes_per_pixel'] );
  20. $BMP['decal'] = ($BMP['width'] * $BMP['bytes_per_pixel'] / 4); $BMP['decal'] -= floor( $BMP['width'] * $BMP['bytes_per_pixel'] / 4 );
  21. $BMP['decal'] = 4 - (4 * $BMP[ 'decal']);
  22. if ( $BMP['decal'] == 4 )
  23. $BMP['decal'] = 0;
  24. $PALETTE = array();
  25. if ($BMP['colors'] {
  26. $PALETTE = unpack('V'.$BMP['colors'], fread( $f1,$BMP['colors']*4));
  27. }
  28. $IMG = fread( $f1, $BMP['size_bitmap'] );
  29. $VIDE = chr( 0 );
  30. $res = imagecreatetruecolor( $BMP['width'], $BMP['height'] );
  31. $P = 0;
  32. $Y = $BMP['height '] - 1;
  33. while( $Y >= 0 ){
  34. $X = 0;
  35. while( $X if ( $BMP[ 'bits_per_pixel'] == 32 ){
  36. $COLOR = unpack( "V", substr( $IMG, $P, 3 ) );
  37. $B = ord(substr($IMG, $P,1 ));
  38. $G = ord(substr($IMG, $P 1,1));
  39. $R = ord(substr($IMG, $P 2,1));
  40. $color = imagecolorexact( $res, $R, $G, $B );
  41. if ( $color == -1 )
  42. $color = imagecolorallocate( $res, $R, $G, $B );
  43. $COLOR[0] = $R*256*256 $G*256 $B;
  44. $COLOR[1] = $color;
  45. } elseif ( $BMP['bits_per_pixel'] == 24 ) {
  46. $COLOR = unpack( "V", substr( $IMG, $P, 3 ) . $VIDE );
  47. } elseif ( $BMP['bits_per_pixel'] == 16 ){
  48. $ COLOR = unpack("v",substr($IMG,$P,2));
  49. $blue = (($COLOR[1] & 0x001f) $green = (( $COLOR[1] & 0x03e0) >> 2) 7;
  50. $red = (($COLOR[1] & 0xfc00) >> 7) 7;
  51. $COLOR[1] = $red * 65536 $ green * 256 $blue;
  52. } elseif ( $BMP['bits_per_pixel'] == 8 ){
  53. $COLOR = unpack( "n", $VIDE . substr( $IMG, $P, 1 ) ) ;
  54. $COLOR[1] = $PALETTE[$COLOR[1] 1];
  55. } elseif ( $BMP['bits_per_pixel'] == 4 ){
  56. $COLOR = unpack( "n" , $VIDE . substr( $IMG, floor( $P ), 1 ) );
  57. if ( ($P * 2) % 2 == 0 )
  58. $COLOR[1] = ($COLOR[1 ] >> 4);
  59. else
  60. $COLOR[1] = ($COLOR[1] & 0x0F);
  61. $COLOR[1] = $PALETTE[$COLOR[1] 1];
  62. } elseif ( $BMP['bits_per_pixel'] == 1 ){
  63. $COLOR = unpack( "n", $VIDE . substr( $IMG, floor( $P ), 1 ) );
  64. if ( ($P * 8) % 8 == 0 )
  65. $COLOR[1] = $COLOR[1] >> 7;
  66. elseif ( ($P * 8) % 8 == 1 )
  67. $COLOR[1] = ($COLOR[1] & 0x40) >> 6;
  68. elseif ( ($P * 8) % 8 == 2 )
  69. $COLOR[1] = ($COLOR [1] & 0x20) >> 5;
  70. elseif ( ($P * 8) % 8 == 3 )
  71. $COLOR[1] = ($COLOR[1] & 0x10) >> 4;
  72. elseif ( ($P * 8) % 8 == 4 )
  73. $COLOR[1] = ($COLOR[1] & 0x8) >> 3;
  74. elseif ( ($P * 8) % 8 == 5 )
  75. $COLOR[1] = ($COLOR[1] & 0x4) >> 2;
  76. elseif ( ($P * 8) % 8 == 6 )
  77. $COLOR[ 1] = ($COLOR[1] & 0x2) >> 1;
  78. elseif ( ($P * 8) % 8 == 7 )
  79. $COLOR[1] = ($COLOR[1] & 0x1 );
  80. $COLOR[1] = $PALETTE[$COLOR[1] 1];
  81. }else
  82. return FALSE;
  83. imagesetpixel( $res, $X, $Y, $COLOR[ 1] );
  84. $X ;
  85. $P = $BMP['bytes_per_pixel'];
  86. }
  87. $Y--;
  88. $P = $BMP['decal'];
  89. }
  90. fclose( $f1 );
  91. return $res;
  92. }
複製代碼

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板