Home >Backend Development >PHP Tutorial >PHP uses the GD library to generate images in bmp format (imagebmp)

PHP uses the GD library to generate images in bmp format (imagebmp)

WBOY
WBOYOriginal
2016-07-25 09:07:182421browse
  1. /**

  2. * Create bmp format pictures
  3. *
  4. * @author: legend
  5. * @description: create Bitmap-File with GD library
  6. * @version: 0.1
  7. *
  8. * @param resource $im image resource
  9. * @param string $filename If you want to save as a file, please specify the file name. If it is empty, it will be output directly in the browser
  10. * @param integer $bit image quality (1, 4, 8, 16, 24, 32 bits)
  11. * @param integer $compression compression Mode, 0 means no compression, 1 uses RLE8 compression algorithm for compression
  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 <= 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']) . “
Statement:
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