php使用GD库生成bmp格式的图片(imagebmp)

WBOY
Release: 2016-07-25 09:07:18
Original
2287 people have browsed it
  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) // todo:32 bit
  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']) . “\0″;
  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. $extra = ”;
  49. $padding = 4 – ceil($width / (8 / $bit)) % 4;
  50. if ($padding % 4 != 0)
  51. {
  52. $extra = str_repeat(“\0″, $padding);
  53. }
  54. for ($j = $height – 1; $j >= 0; $j –)

  55. {
  56. $i = 0;
  57. while ($i {
  58. $bin = 0;
  59. $limit = $width – $i
  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. $bmp_data .= $extra;

  68. }
  69. }
  70. // RLE8 压缩
  71. else if ($compression == 1 && $bit == 8)
  72. {
  73. for ($j = $height – 1; $j >= 0; $j –)
  74. {
  75. $last_index = “\0″;
  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. else
  89. {
  90. $same_num ++;
  91. }
  92. }
  93. $bmp_data .= “\0\0″;

  94. }
  95. $bmp_data .= “\0\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(“\0″, $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 .= $extra;

  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. echo $file_header . $info_header;
  152. echo $rgb_quad;
  153. echo $bmp_data;
  154. return 1;

  155. }
  156. ?>
复制代码


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!