PageRenderTime 57ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/functions/imagecreatefrombmpstring.php

https://github.com/chokoleytdesignoper/fluxcp_choko
PHP | 95 lines | 88 code | 1 blank | 6 comment | 35 complexity | b85e8d58eec0b004f1c226b5c8f7853d MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Code taken from https://cerescp.svn.sourceforge.net/svnroot/cerescp/emblema.php
  4. * under the GNU General Public license version 2.
  5. *
  6. * @license GPLv2 <http://www.gnu.org/licenses/gpl-2.0.txt>
  7. */
  8. function imagecreatefrombmpstring($im) {
  9. $header = unpack("vtype/Vsize/v2reserved/Voffset", substr($im, 0, 14));
  10. $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", substr($im, 14, 40));
  11. extract($info);
  12. extract($header);
  13. if($type != 0x4D42)
  14. return false;
  15. $palette_size = $offset - 54;
  16. $ncolor = $palette_size / 4;
  17. $imres=imagecreatetruecolor($width, $height);
  18. imagealphablending($imres, false);
  19. imagesavealpha($imres, true);
  20. $pal=array();
  21. if($palette_size) {
  22. $palette = substr($im, 54, $palette_size);
  23. $gd_palette = "";
  24. $j = 0; $n = 0;
  25. while($j < $palette_size) {
  26. $b = ord($palette{$j++});
  27. $g = ord($palette{$j++});
  28. $r = ord($palette{$j++});
  29. $a = ord($palette{$j++});
  30. if ( ($r & 0xf8 == 0xf8) && ($g == 0) && ($b & 0xf8 == 0xf8))
  31. $a = 127; // alpha = 255 on 0xFF00FF
  32. $pal[$n++] = imagecolorallocatealpha($imres, $r, $g, $b, $a);
  33. }
  34. }
  35. $scan_line_size = (($bits * $width) + 7) >> 3;
  36. $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03): 0;
  37. for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
  38. $scan_line = substr($im, $offset + (($scan_line_size + $scan_line_align) * $l), $scan_line_size);
  39. if($bits == 24) {
  40. $j = 0; $n = 0;
  41. while($j < $scan_line_size) {
  42. $b = ord($scan_line{$j++});
  43. $g = ord($scan_line{$j++});
  44. $r = ord($scan_line{$j++});
  45. $a = 0;
  46. if ( ($r & 0xf8 == 0xf8) && ($g == 0) && ($b & 0xf8 == 0xf8))
  47. $a = 127; // alpha = 255 on 0xFF00FF
  48. $col=imagecolorallocatealpha($imres, $r, $g, $b, $a);
  49. imagesetpixel($imres, $n++, $i, $col);
  50. }
  51. }
  52. else if($bits == 8) {
  53. $j = 0;
  54. while($j < $scan_line_size) {
  55. $col = $pal[ord($scan_line{$j++})];
  56. imagesetpixel($imres, $j-1, $i, $col);
  57. }
  58. }
  59. else if($bits == 4) {
  60. $j = 0; $n = 0;
  61. while($j < $scan_line_size) {
  62. $byte = ord($scan_line{$j++});
  63. $p1 = $byte >> 4;
  64. $p2 = $byte & 0x0F;
  65. imagesetpixel($imres, $n++, $i, $pal[$p1]);
  66. imagesetpixel($imres, $n++, $i, $pal[$p2]);
  67. }
  68. }
  69. else if($bits == 1) {
  70. $j = 0; $n = 0;
  71. while($j < $scan_line_size) {
  72. $byte = ord($scan_line{$j++});
  73. $p1 = (int) (($byte & 0x80) != 0);
  74. $p2 = (int) (($byte & 0x40) != 0);
  75. $p3 = (int) (($byte & 0x20) != 0);
  76. $p4 = (int) (($byte & 0x10) != 0);
  77. $p5 = (int) (($byte & 0x08) != 0);
  78. $p6 = (int) (($byte & 0x04) != 0);
  79. $p7 = (int) (($byte & 0x02) != 0);
  80. $p8 = (int) (($byte & 0x01) != 0);
  81. imagesetpixel($imres, $n++, $i, $pal[$p1]);
  82. imagesetpixel($imres, $n++, $i, $pal[$p2]);
  83. imagesetpixel($imres, $n++, $i, $pal[$p3]);
  84. imagesetpixel($imres, $n++, $i, $pal[$p4]);
  85. imagesetpixel($imres, $n++, $i, $pal[$p5]);
  86. imagesetpixel($imres, $n++, $i, $pal[$p6]);
  87. imagesetpixel($imres, $n++, $i, $pal[$p7]);
  88. imagesetpixel($imres, $n++, $i, $pal[$p8]);
  89. }
  90. }
  91. }
  92. return $imres;
  93. }
  94. ?>