国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

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

2020-03-22 18:41:36
字體:
供稿:網(wǎng)友
  1. /**

  2. * 創(chuàng)建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 如果要另存為文件,請(qǐng)指定文件名,為空則直接在瀏覽器輸出
  10. * @param integer $bit 圖像質(zhì)量(1、4、8、16、24、32位)
  11. * @param integer $compression 壓縮方式,0為不壓縮,1使用RLE8壓縮算法進(jìn)行壓縮
  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. // 調(diào)整調(diào)色板

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

  31. if ($bit <= 8)

  32. {
  33. // 顏色索引
  34. $rgb_quad = ”;
  35. for ($i = 0; $i < $colors_num; $i ++)
  36. {
  37. $colors = imagecolorsforindex($im, $i);
  38. $rgb_quad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . “/0″;
  39. }

  40. // 位圖數(shù)據(jù)

  41. $bmp_data = ”;

  42. // 非壓縮

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

  49. $compression = 0;

  50. // 每行字節(jié)數(shù)必須為4的倍數(shù),補(bǔ)齊。

  51. $extra = ”;
  52. $padding = 4 – ceil($width / (8 / $bit)) % 4;
  53. if ($padding % 4 != 0)
  54. {
  55. $extra = str_repeat(“/0″, $padding);
  56. }

  57. for ($j = $height – 1; $j >= 0; $j –)

  58. {
  59. $i = 0;
  60. while ($i < $width)
  61. {
  62. $bin = 0;
  63. $limit = $width – $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0;

  64. for ($k = 8 – $bit; $k >= $limit; $k -= $bit)

  65. {
  66. $index = imagecolorat($im, $i, $j);
  67. $bin |= $index << $k;
  68. $i ++;
  69. }

  70. $bmp_data .= chr($bin);

  71. }

  72. $bmp_data .= $extra;

  73. }
  74. }
  75. // RLE8 壓縮
  76. else if ($compression == 1 && $bit == 8)
  77. {
  78. for ($j = $height – 1; $j >= 0; $j –)
  79. {
  80. $last_index = “/0″;
  81. $same_num = 0;
  82. for ($i = 0; $i <= $width; $i ++)
  83. {
  84. $index = imagecolorat($im, $i, $j);
  85. if ($index !== $last_index || $same_num > 255)
  86. {
  87. if ($same_num != 0)
  88. {
  89. $bmp_data .= chr($same_num) . chr($last_index);
  90. }

  91. $last_index = $index;

  92. $same_num = 1;
  93. }
  94. else
  95. {
  96. $same_num ++;
  97. }
  98. }

  99. $bmp_data .= “/0/0″;

  100. }

  101. $bmp_data .= “/0/1″;

  102. }

  103. $size_quad = strlen($rgb_quad);

  104. $size_data = strlen($bmp_data);
  105. }
  106. else
  107. {
  108. // 每行字節(jié)數(shù)必須為4的倍數(shù),補(bǔ)齊。
  109. $extra = ”;
  110. $padding = 4 – ($width * ($bit / 8)) % 4;
  111. if ($padding % 4 != 0)
  112. {
  113. $extra = str_repeat(“/0″, $padding);
  114. }

  115. // 位圖數(shù)據(jù)

  116. $bmp_data = ”;

  117. for ($j = $height – 1; $j >= 0; $j –)

  118. {
  119. for ($i = 0; $i < $width; $i ++)
  120. {
  121. $index = imagecolorat($im, $i, $j);
  122. $colors = imagecolorsforindex($im, $index);

  123. if ($bit == 16)

  124. {
  125. $bin = 0 << $bit;

  126. $bin |= ($colors['red'] >> 3) << 10;

  127. $bin |= ($colors['green'] >> 3) << 5;
  128. $bin |= $colors['blue'] >> 3;

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

  130. }
  131. else
  132. {
  133. $bmp_data .= pack(“c*”, $colors['blue'], $colors['green'], $colors['red']);
  134. }

  135. // todo: 32bit;

  136. }

  137. $bmp_data .= $extra;

  138. }

  139. $size_quad = 0;

  140. $size_data = strlen($bmp_data);
  141. $colors_num = 0;
  142. }

  143. // 位圖文件頭

  144. $file_header = “BM” . pack(“V3″, 54 + $size_quad + $size_data, 0, 54 + $size_quad);

  145. // 位圖信息頭

  146. $info_header = pack(“V3v2V*”, 0×28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);

  147. // 寫入文件

  148. if ($filename != ”)
  149. {
  150. $fp = fopen(“test.bmp”, “wb”);

  151. fwrite($fp, $file_header);

  152. fwrite($fp, $info_header);
  153. fwrite($fp, $rgb_quad);
  154. fwrite($fp, $bmp_data);
  155. fclose($fp);

  156. return 1;

  157. }

  158. // 瀏覽器輸出

  159. header(“Content-Type: image/bmp”);
  160. echo $file_header . $info_header;
  161. echo $rgb_quad;
  162. echo $bmp_data;

  163. return 1;

  164. }
  165. ?>

復(fù)制代碼

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 太白县| 红安县| 桓仁| 岐山县| 中山市| 九龙坡区| 绿春县| 广水市| 虹口区| 互助| 山丹县| 秦皇岛市| 永川市| 大埔区| 务川| 开原市| 德惠市| 宜兰市| 肇庆市| 扎鲁特旗| 唐山市| 长岛县| 万宁市| 涞水县| 舟山市| 娄底市| 新源县| 南通市| 靖安县| 滨海县| 孟村| 韩城市| 绥德县| 宁南县| 于都县| 佛教| 阳新县| 亚东县| 乐东| 鲁甸县| 太白县|