php使用GD庫產生bmp格式的圖片(imagebmp)

WBOY
發布: 2016-07-25 09:07:18
原創
2371 人瀏覽過
複製程式碼
  1. /**

  2. * 創建bmp格式圖片
  3. *
  4. * @author: legend
  5. * @description: create Bitmap-File with GD library
  6. * @version: 0.1
  7. *
  8. * @param resource $im 映像資源
  9. * @param string $filename 若要另存為文件,請指定文件名,為空則直接在瀏覽器輸出
  10. * @param integer $bit 影像品質(1、4 、8、16、24、32位元)
  11. * @param integer $compression 壓縮方式,0為不壓縮,1使用RLE8壓縮演算法進行壓縮
  12. *
  13. * @return integer
  14. */
  15. function imagebmp(&$im, $filename = ”, $bit = 8, $compression = 0)
  16. {
  17. if (!in_array($bit, array(1, 4, 8, 16, 24, 32)))
  18. {
  19. $bit = 8;
  20. }
  21. else if ($bit == 32) // 待辦事項:32 位元
  22. {
  23. $bit = 24;
  24. }
  25. $bits = pow(2, $bit);

  26. //調整調色盤

  27. imagetruecolortopalette($im, true, $bits);
  28. $width = imagesx($im);
  29. $height = imagesy($im);
  30. $colors_num = imagecolorstotal($im);
  31. if ($bit {

  32. //顏色索引
  33. $rgb_quad = ”;
  34. for ($i = 0; $i {
  35. $colors = imagecolorsforindex( $im, $i);
  36. $rgb_quad .= chr($colors['blue']) 。 chr($colors['green']) 。 chr($colors['red']) 。 「 」;
  37. }
  38. //點陣圖資料

  39. $bmp_data = ”;
  40. //壓縮非

  41. if ($ compression == 0 || $bit {
  42. if (!in_array($bit, array(1, 4, 8)))
  43. {
  44. $bit = 8;
  45. }
  46. $compression = 0;

  47. //每行位元組數必須為4的倍數,補齊。 ;

  48. $padding = 4 – ceil($width / (8 / $bit)) % 4;
  49. if ($padding % 4 != 0)
  50. {
  51. $extra = str_repeat(“ ”, $padding);
  52. }
  53. for ($j = $height – 1; $j >= 0; $j –)

  54. {
  55. $i = 0;
  56. while ($i {
  57. $bin = 0;
  58. $limit = $width – $i
  59. ; 8 / $位? (8 / $bit - $width + $i) * $bit : 0;
  60. for ($k = 8 – $bit; $k >= $limit; $k -= $ bit)

  61. {
  62. $index = imagecolorat($im, $i, $j);
  63. $bin |= $index $i ++;
  64. }
  65. $bmp_data .= chr($bin);

  66. }
  67. < ;p>$bmp_data .= $extra;
  68. }
  69. }
  70. // RLE8 壓縮
  71. else if ($compression == 1 && $bit == 8)
  72. {
  73. for ($j = $height – 1; $j >= 0; $jj –)
  74. {
  75. $last_index = 「 」;
  76. $same_num = 0;
  77. for ($i = 0; $i {
  78. $ index = imagecolorat($im, $i, $j);
  79. if ($index !== $last_index | | $same_num > 255)
  80. {
  81. if ($same_num != 0)
  82. {
  83. $bmp_data .= chr($same_num) . chr($last_index);
  84. }
  85. $last_index = $index;

  86. $same_num = 1;
  87. }
  88. 其他
  89. {
  90. $same_num ++;
  91. }
  92. }
  93. $bmp_data .= “ ”;

  94. } p>
  95. $ bmp_data .= “ 1″;

  96. }
  97. $size_quad = strlen($rgb_quad);

  98. $size_data = strlen($bmp_data) ;
  99. }
  100. else
  101. {
  102. // 每行位元組數必須為4的倍數,補齊。
  103. $extra = ”;
  104. $padding = 4 – ($width * ($bit / 8)) % 4;
  105. if ($padding % 4 != 0)
  106. {
  107. $extra = str_repeat(“ ”, $padding);
  108. }
  109. //點陣圖資料

  110. $bmp_data = ”;
  111. for ($j = $height – 1; $j >= 0; $j –)

  112. {
  113. for ($i = 0; $i {
  114. $index = imagecolorat($im, $i, $j);
  115. $colors = imagecolorsforindex($im, $index);
  116. if ($bit == 16)

  117. {
  118. $bin = 0
  119. $bin |= ($colors['red']>>3) $ bin |= ($colors['green'] >> 3) $bin |= $colors['blue'] >>> 3;

  120. $bmp_data . = pack(“v”, $bin);

  121. }
  122. else
  123. {
  124. $bmp_data .= pack(“ c*”, $colors['blue'], $colors[' green '], $colors['red']);
  125. }
  126. // todo: 32bit ;

  127. }
  128. $bmp_data . = $額外;

  129. }
  130. $size_quad = 0;

  131. $size_data = strlen($bmp_data);
  132. $colors_num = 0;
  133. }
  134. //點陣圖檔案頭

  135. $file_header = “BM” . pack(“V3″, 54 + $size_quad + $size_data, 0, 54 + $size_quad);
  136. //點陣圖資訊頭

  137. $info_header = pack(“V3v2V *” , 0×28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);
  138. // 寫入檔案

  139. if ($filename != ”)
  140. {
  141. $fp = fopen(“test.bmp”, “wb”);
  142. fwrite($ fp, $file_header);

  143. fwrite($fp, $info_header);
  144. fwrite($fp, $rgb_quad);
  145. fwrite($fp, $bmp_data);
  146. fclose($fp) ;
  147. return 1;

  148. }
  149. //瀏覽器輸出

  150. header(“Content-Type: image/bmp ”);
  151. 回顯$file_header $info_header;
  152. 回顯$rgb_quad;
  153. 回顯$bmp_data;
  154. return 1;

  155. }
  156. ?>
}
?>


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!