Guest User

zerxer

a guest
Sep 21st, 2008
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.55 KB | None | 0 0
  1. <?
  2. /*
  3. *------------------------------------------------------------
  4. *                   BMP Image functions
  5. *------------------------------------------------------------
  6. *                      By JPEXS
  7. */
  8.  
  9.  
  10.  
  11.  
  12. /*
  13. *------------------------------------------------------------
  14. *                    ImageBMP
  15. *------------------------------------------------------------
  16. *            - Creates new BMP file
  17. *
  18. *         Parameters:  $img - Target image
  19. *                      $file - Target file to store
  20. *                            - if not specified, bmp is returned
  21. *
  22. *           Returns: if $file specified - true if OK
  23.                      if $file not specified - image data
  24. */
  25. function imagebmp($img,$file="",$RLE=0)
  26. {
  27.  
  28.  
  29. $ColorCount=imagecolorstotal($img);
  30.  
  31. $Transparent=imagecolortransparent($img);
  32. $IsTransparent=$Transparent!=-1;
  33.  
  34.  
  35. if($IsTransparent) $ColorCount--;
  36.  
  37. if($ColorCount==0) {$ColorCount=0; $BitCount=24;};
  38. if(($ColorCount>0)and($ColorCount<=2)) {$ColorCount=2; $BitCount=1;};
  39. if(($ColorCount>2)and($ColorCount<=16)) { $ColorCount=16; $BitCount=4;};
  40. if(($ColorCount>16)and($ColorCount<=256)) { $ColorCount=0; $BitCount=8;};
  41.  
  42.  
  43.                 $Width=imagesx($img);
  44.                 $Height=imagesy($img);
  45.  
  46.                 $Zbytek=(4-($Width/(8/$BitCount))%4)%4;
  47.  
  48.                 if($BitCount<24) $palsize=pow(2,$BitCount)*4;
  49.  
  50.                 $size=(floor($Width/(8/$BitCount))+$Zbytek)*$Height+54;
  51.                 $size+=$palsize;
  52.                 $offset=54+$palsize;
  53.  
  54.                 // Bitmap File Header
  55.                 $ret = 'BM';                        // header (2b)
  56.                 $ret .= int_to_dword($size);        // size of file (4b)
  57.                 $ret .= int_to_dword(0);        // reserved (4b)
  58.                 $ret .= int_to_dword($offset);        // byte location in the file which is first byte of IMAGE (4b)
  59.                 // Bitmap Info Header
  60.                 $ret .= int_to_dword(40);        // Size of BITMAPINFOHEADER (4b)
  61.                 $ret .= int_to_dword($Width);        // width of bitmap (4b)
  62.                 $ret .= int_to_dword($Height);        // height of bitmap (4b)
  63.                 $ret .= int_to_word(1);        // biPlanes = 1 (2b)
  64.                 $ret .= int_to_word($BitCount);        // biBitCount = {1 (mono) or 4 (16 clr ) or 8 (256 clr) or 24 (16 Mil)} (2b)
  65.                 $ret .= int_to_dword($RLE);        // RLE COMPRESSION (4b)
  66.                 $ret .= int_to_dword(0);        // width x height (4b)
  67.                 $ret .= int_to_dword(0);        // biXPelsPerMeter (4b)
  68.                 $ret .= int_to_dword(0);        // biYPelsPerMeter (4b)
  69.                 $ret .= int_to_dword(0);        // Number of palettes used (4b)
  70.                 $ret .= int_to_dword(0);        // Number of important colour (4b)
  71.                 // image data
  72.  
  73.                 $CC=$ColorCount;
  74.                 $sl1=strlen($ret);
  75.                 if($CC==0) $CC=256;
  76.                 if($BitCount<24)
  77.                    {
  78.                     $ColorTotal=imagecolorstotal($img);
  79.                      if($IsTransparent) $ColorTotal--;
  80.  
  81.                      for($p=0;$p<$ColorTotal;$p++)
  82.                      {
  83.                       $color=imagecolorsforindex($img,$p);
  84.                        $ret.=inttobyte($color["blue"]);
  85.                        $ret.=inttobyte($color["green"]);
  86.                        $ret.=inttobyte($color["red"]);
  87.                        $ret.=inttobyte(0); //RESERVED
  88.                      };
  89.  
  90.                     $CT=$ColorTotal;
  91.                   for($p=$ColorTotal;$p<$CC;$p++)
  92.                        {
  93.                       $ret.=inttobyte(0);
  94.                       $ret.=inttobyte(0);
  95.                       $ret.=inttobyte(0);
  96.                       $ret.=inttobyte(0); //RESERVED
  97.                      };
  98.                    };
  99.  
  100.  
  101. if($BitCount<=8)
  102. {
  103.  
  104.  for($y=$Height-1;$y>=0;$y--)
  105.  {
  106.   $bWrite="";
  107.   for($x=0;$x<$Width;$x++)
  108.    {
  109.    $color=imagecolorat($img,$x,$y);
  110.    $bWrite.=decbinx($color,$BitCount);
  111.    if(strlen($bWrite)==8)
  112.     {
  113.      $retd.=inttobyte(bindec($bWrite));
  114.      $bWrite="";
  115.     };
  116.    };
  117.  
  118.   if((strlen($bWrite)<8)and(strlen($bWrite)!=0))
  119.     {
  120.      $sl=strlen($bWrite);
  121.      for($t=0;$t<8-$sl;$t++)
  122.       $sl.="0";
  123.      $retd.=inttobyte(bindec($bWrite));
  124.     };
  125.  for($z=0;$z<$Zbytek;$z++)
  126.    $retd.=inttobyte(0);
  127.  };
  128. };
  129.  
  130. if(($RLE==1)and($BitCount==8))
  131. {
  132.  for($t=0;$t<strlen($retd);$t+=4)
  133.   {
  134.    if($t!=0)
  135.    if(($t)%$Width==0)
  136.     $ret.=chr(0).chr(0);
  137.  
  138.    if(($t+5)%$Width==0)
  139.    {
  140.      $ret.=chr(0).chr(5).substr($retd,$t,5).chr(0);
  141.      $t+=1;
  142.    }
  143.    if(($t+6)%$Width==0)
  144.     {
  145.      $ret.=chr(0).chr(6).substr($retd,$t,6);
  146.      $t+=2;
  147.     }
  148.     else
  149.     {
  150.      $ret.=chr(0).chr(4).substr($retd,$t,4);
  151.     };
  152.   };
  153.   $ret.=chr(0).chr(1);
  154. }
  155. else
  156. {
  157. $ret.=$retd;
  158. };
  159.  
  160.  
  161.                 if($BitCount==24)
  162.                 {
  163.                 for($z=0;$z<$Zbytek;$z++)
  164.                  $Dopl.=chr(0);
  165.  
  166.                 for($y=$Height-1;$y>=0;$y--)
  167.                  {
  168.                  for($x=0;$x<$Width;$x++)
  169.                         {
  170.                            $color=imagecolorsforindex($img,ImageColorAt($img,$x,$y));
  171.                            $ret.=chr($color["blue"]).chr($color["green"]).chr($color["red"]);
  172.                         }
  173.                  $ret.=$Dopl;
  174.                  };
  175.  
  176.                  };
  177.  
  178.   if($file!="")
  179.    {
  180.     $r=($f=fopen($file,"w"));
  181.     $r=$r and fwrite($f,$ret);
  182.     $r=$r and fclose($f);
  183.     return $r;
  184.    }
  185.   else
  186.   {
  187.    echo $ret;
  188.   };
  189. };
  190.  
  191.  
  192. /*
  193. *------------------------------------------------------------
  194. *                    ImageCreateFromBmp
  195. *------------------------------------------------------------
  196. *            - Reads image from a BMP file
  197. *
  198. *         Parameters:  $file - Target file to load
  199. *
  200. *            Returns: Image ID
  201. */
  202.  
  203. function imagecreatefrombmp($file)
  204. {
  205. global  $CurrentBit, $echoMode;
  206.  
  207. $f=fopen($file,"r");
  208. $Header=fread($f,2);
  209.  
  210. if($Header=="BM")
  211. {
  212.  $Size=freaddword($f);
  213.  $Reserved1=freadword($f);
  214.  $Reserved2=freadword($f);
  215.  $FirstByteOfImage=freaddword($f);
  216.  
  217.  $SizeBITMAPINFOHEADER=freaddword($f);
  218.  $Width=freaddword($f);
  219.  $Height=freaddword($f);
  220.  $biPlanes=freadword($f);
  221.  $biBitCount=freadword($f);
  222.  $RLECompression=freaddword($f);
  223.  $WidthxHeight=freaddword($f);
  224.  $biXPelsPerMeter=freaddword($f);
  225.  $biYPelsPerMeter=freaddword($f);
  226.  $NumberOfPalettesUsed=freaddword($f);
  227.  $NumberOfImportantColors=freaddword($f);
  228.  
  229. if($biBitCount<24)
  230.  {
  231.   $img=imagecreate($Width,$Height);
  232.   $Colors=pow(2,$biBitCount);
  233.   for($p=0;$p<$Colors;$p++)
  234.    {
  235.     $B=freadbyte($f);
  236.     $G=freadbyte($f);
  237.     $R=freadbyte($f);
  238.     $Reserved=freadbyte($f);
  239.     $Palette[]=imagecolorallocate($img,$R,$G,$B);
  240.    };
  241.  
  242.  
  243.  
  244.  
  245. if($RLECompression==0)
  246. {
  247.    $Zbytek=(4-ceil(($Width/(8/$biBitCount)))%4)%4;
  248.  
  249. for($y=$Height-1;$y>=0;$y--)
  250.     {
  251.      $CurrentBit=0;
  252.      for($x=0;$x<$Width;$x++)
  253.       {
  254.          $C=freadbits($f,$biBitCount);
  255.        imagesetpixel($img,$x,$y,$Palette[$C]);
  256.       };
  257.     if($CurrentBit!=0) {freadbyte($f);};
  258.     for($g=0;$g<$Zbytek;$g++)
  259.      freadbyte($f);
  260.      };
  261.  
  262.  };
  263. };
  264.  
  265.  
  266. if($RLECompression==1) //$BI_RLE8
  267. {
  268. $y=$Height;
  269.  
  270. $pocetb=0;
  271.  
  272. while(true)
  273. {
  274. $y--;
  275. $prefix=freadbyte($f);
  276. $suffix=freadbyte($f);
  277. $pocetb+=2;
  278.  
  279. $echoit=false;
  280.  
  281. if($echoit)echo "Prefix: $prefix Suffix: $suffix<BR>";
  282. if(($prefix==0)and($suffix==1)) break;
  283. if(feof($f)) break;
  284.  
  285. while(!(($prefix==0)and($suffix==0)))
  286. {
  287.  if($prefix==0)
  288.   {
  289.    $pocet=$suffix;
  290.    $Data.=fread($f,$pocet);
  291.    $pocetb+=$pocet;
  292.    if($pocetb%2==1) {freadbyte($f); $pocetb++;};
  293.   };
  294.  if($prefix>0)
  295.   {
  296.    $pocet=$prefix;
  297.    for($r=0;$r<$pocet;$r++)
  298.     $Data.=chr($suffix);
  299.   };
  300.  $prefix=freadbyte($f);
  301.  $suffix=freadbyte($f);
  302.  $pocetb+=2;
  303.  if($echoit) echo "Prefix: $prefix Suffix: $suffix<BR>";
  304. };
  305.  
  306. for($x=0;$x<strlen($Data);$x++)
  307.  {
  308.   imagesetpixel($img,$x,$y,$Palette[ord($Data[$x])]);
  309.  };
  310. $Data="";
  311.  
  312. };
  313.  
  314. };
  315.  
  316.  
  317. if($RLECompression==2) //$BI_RLE4
  318. {
  319. $y=$Height;
  320. $pocetb=0;
  321.  
  322. /*while(!feof($f))
  323.  echo freadbyte($f)."_".freadbyte($f)."<BR>";*/
  324. while(true)
  325. {
  326. //break;
  327. $y--;
  328. $prefix=freadbyte($f);
  329. $suffix=freadbyte($f);
  330. $pocetb+=2;
  331.  
  332. $echoit=false;
  333.  
  334. if($echoit)echo "Prefix: $prefix Suffix: $suffix<BR>";
  335. if(($prefix==0)and($suffix==1)) break;
  336. if(feof($f)) break;
  337.  
  338. while(!(($prefix==0)and($suffix==0)))
  339. {
  340.  if($prefix==0)
  341.   {
  342.    $pocet=$suffix;
  343.  
  344.    $CurrentBit=0;
  345.    for($h=0;$h<$pocet;$h++)
  346.     $Data.=chr(freadbits($f,4));
  347.    if($CurrentBit!=0) freadbits($f,4);
  348.    $pocetb+=ceil(($pocet/2));
  349.    if($pocetb%2==1) {freadbyte($f); $pocetb++;};
  350.   };
  351.  if($prefix>0)
  352.   {
  353.    $pocet=$prefix;
  354.    $i=0;
  355.    for($r=0;$r<$pocet;$r++)
  356.     {
  357.     if($i%2==0)
  358.      {
  359.       $Data.=chr($suffix%16);
  360.      }
  361.      else
  362.      {
  363.       $Data.=chr(floor($suffix/16));
  364.      };
  365.     $i++;
  366.     };
  367.   };
  368.  $prefix=freadbyte($f);
  369.  $suffix=freadbyte($f);
  370.  $pocetb+=2;
  371.  if($echoit) echo "Prefix: $prefix Suffix: $suffix<BR>";
  372. };
  373.  
  374. for($x=0;$x<strlen($Data);$x++)
  375.  {
  376.   imagesetpixel($img,$x,$y,$Palette[ord($Data[$x])]);
  377.  };
  378. $Data="";
  379.  
  380. };
  381.  
  382. };
  383.  
  384.  
  385.  if($biBitCount==24)
  386. {
  387.  $img=imagecreatetruecolor($Width,$Height);
  388.  $Zbytek=$Width%4;
  389.  
  390.    for($y=$Height-1;$y>=0;$y--)
  391.     {
  392.      for($x=0;$x<$Width;$x++)
  393.       {
  394.        $B=freadbyte($f);
  395.        $G=freadbyte($f);
  396.        $R=freadbyte($f);
  397.        $color=imagecolorexact($img,$R,$G,$B);
  398.        if($color==-1) $color=imagecolorallocate($img,$R,$G,$B);
  399.        imagesetpixel($img,$x,$y,$color);
  400.       }
  401.     for($z=0;$z<$Zbytek;$z++)
  402.      freadbyte($f);
  403.    };
  404. };
  405. return $img;
  406.  
  407. };
  408.  
  409.  
  410. fclose($f);
  411.  
  412.  
  413. };
  414.  
  415. ?>
  416.  
Advertisement
Add Comment
Please, Sign In to add comment