Guest User

Untitled

a guest
Dec 9th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. <?php
  2. function ImageCreateFromBMP($filename)
  3. {
  4.    if (! $f1 = fopen($filename,"rb")) return FALSE;
  5.  
  6.    $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
  7.    if ($FILE['file_type'] != 19778) return FALSE;
  8.  
  9.    $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  10.                  '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  11.                  '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
  12.    $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
  13.    if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  14.    $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
  15.    $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
  16.    $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
  17.    $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  18.    $BMP['decal'] = 4-(4*$BMP['decal']);
  19.    if ($BMP['decal'] == 4) $BMP['decal'] = 0;
  20.  
  21.    $PALETTE = array();
  22.    if ($BMP['colors'] < 16777216)
  23.    {
  24.     $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
  25.    }
  26.  
  27.    $IMG = fread($f1,$BMP['size_bitmap']);
  28.    $VIDE = chr(0);
  29.  
  30.    $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
  31.    $P = 0;
  32.    $Y = $BMP['height']-1;
  33.    while ($Y >= 0)
  34.    {
  35.     $X=0;
  36.     while ($X < $BMP['width'])
  37.     {
  38.      if ($BMP['bits_per_pixel'] == 24)
  39.         $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
  40.      elseif ($BMP['bits_per_pixel'] == 16)
  41.      {
  42.         $COLOR = unpack("n",substr($IMG,$P,2));
  43.         $COLOR[1] = $PALETTE[$COLOR[1]+1];
  44.      }
  45.      elseif ($BMP['bits_per_pixel'] == 8)
  46.      {
  47.         $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
  48.         $COLOR[1] = $PALETTE[$COLOR[1]+1];
  49.      }
  50.      elseif ($BMP['bits_per_pixel'] == 4)
  51.      {
  52.         $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  53.         if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
  54.         $COLOR[1] = $PALETTE[$COLOR[1]+1];
  55.      }
  56.      elseif ($BMP['bits_per_pixel'] == 1)
  57.      {
  58.         $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  59.         if     (($P*8)%8 == 0) $COLOR[1] =  $COLOR[1]        >>7;
  60.         elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
  61.         elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
  62.         elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
  63.         elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
  64.         elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
  65.         elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
  66.         elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
  67.         $COLOR[1] = $PALETTE[$COLOR[1]+1];
  68.      }
  69.      else
  70.         return FALSE;
  71.      imagesetpixel($res,$X,$Y,$COLOR[1]);
  72.      $X++;
  73.      $P += $BMP['bytes_per_pixel'];
  74.     }
  75.     $Y--;
  76.     $P+=$BMP['decal'];
  77.    }
  78.  
  79.    fclose($f1);
  80.  
  81.  return $res;
  82. }
  83. ?>
Add Comment
Please, Sign In to add comment