[PHP] Imf-bmp Plus Magic Table - sausage

Miscellaneous tools for your Soma Server

[PHP] Imf-bmp Plus Magic Table - sausage

Postby Tiffany » Tue Feb 26, 2008 11:27 am

Original post by sausage on PirateForum

Check attachments for ShopTool (PHP)
IMF to BMP
Code: Select all
<STYLE>
body {
font-size: 10;
font-family: Courier;
}
</STYLE>
<?php
set_time_limit(600);
//
// IMF name
// edit $file to IMF of choice.
// --- sausage
#########
######### NOTE
######### ALL OUTPUT IMAGES WILL BE UPSIDE DOWN. OPEN IN PAINT AND FLIP THEM.
$file = "imf/Mset203.imf";
//
// Functions
// do not edit
// --- sausage
function str2bin($str, $mode=0) {
   $out = false;
   for($a=0; $a < strlen($str); $a++) {
       $dec = ord(substr($str,$a,1));
       $bin = '';
       for($i=7; $i>=0; $i--) {
           if ( $dec >= pow(2, $i) ) {
               $bin .= "1";
               $dec -= pow(2, $i);
           } else {
               $bin .= "0";
           }
       }
       /* Default-mode */
       if ( $mode == 0 ) $out .= $bin;
       /* Human-mode (easy to read) */
       if ( $mode == 1 ) $out .= $bin . " ";
       /* Array-mode (easy to use) */
       if ( $mode == 2 ) $out[$a] = $bin;
   }
   return $out;
}

function hexbin($hex){
   $bin='';
   for($i=0;$i<strlen($hex);$i++)
       $bin.=str_pad(decbin(hexdec($hex{$i})),4,'0',STR_PAD_LEFT);
       return $bin;
}

function binhex($bin){
   $hex='';
   for($i=strlen($bin)-4;$i>=0;$i-=4)
       $hex.=dechex(bindec(substr($bin,$i,4)));
   return strrev($hex);
}
//
// IMF data
// segment below obtains header information from IMF file
// --- sausage
$filehandle = fopen($file, "rb");
$fsize = filesize($file);
$filecontent = fread($filehandle, $fsize);
$filecontent = str2bin($filecontent);
$headercontent = substr($filecontent, 0, 96);
$filecontent = substr($filecontent, 96, $fsize);
$mapWidth2 = binhex(substr($headercontent, 32, 8));
$mapWidth1 = binhex(substr($headercontent, 40, 8));
$mapY2 = binhex(substr($headercontent, 64, 8));
$mapY1 = binhex(substr($headercontent, 72, 8));
$mapWidth =  hexdec($mapWidth1.$mapWidth2);
$mapWidth=($mapWidth *2);
$mapY = hexdec($mapY1.$mapY2);
//
// Vars
//
// --- sausage
$offset = 0;
$bitoffset = 0;
$counter = 0;
$count = 0;
$pxPad = 0;
//
// BMP data
// segment below sets data for BMP header information
// --- sausage
$Header['bfType1'] = 66;
$Header['bfType2'] = 77;
$Header['bfSize'] = ((($mapWidth * $mapY) * 3) + (($mapWidth % 4) * $mapY)) + 54;
$Header['bfReserved1'] = 0;
$Header['bfReserved2'] = 0;
$Header['bfOffbits'] = 54;
$Header['biSize'] = 40;
$Header['biWidth'] = $mapWidth -2;
$Header['biHeight'] = $mapY;
$Header['biPlanes'] = 1;
$Header['biBitCount'] = 24;
$Header['biCompression'] = 0;
$Header['biSizeXL'] = $mapWidth;
$Header['BiSizeYL'] = $mapY;
$Header['biSizeImage'] = ((($mapWidth * $mapY) * 3) + (($mapWidth % 4) * $mapY)) + 54;
$Header['biXPelsPerMeter'] = 0;
$Header['biYPelsPerMeter'] = 0;
$Header['biClrUsed'] = 0;
$Header['biClrImportant'] = 0;
$EXVAR1 = $Header['biWidth'] % 4;
$vert = 1;
$insert = 0;
//
// Array data
// segment below put's 1s and 0s in array from IMF file
// --- sausage
while($offset < $fsize){
    $ThisByte = substr($filecontent, $offset, 8);
    $ThisByte = strrev($ThisByte);
        while($bitoffset < 8){
            $ThisBit = substr($ThisByte, $bitoffset, 1);
            if($ThisBit == 1){
                    $Buffer['px' . $count] = 1;
            }
            if($ThisBit == 0){
                    $Buffer['px' . $count] = 0;
            }
            $count++;
            $bitoffset++;
        }
    $bitoffset = 0;
    $offset = $offset +8;
}
echo "<BR><BR>";
//
// BMP Data
// segment below determins bmp's name and if it is readable
// --- sausage

$newbmp = substr($file, 4, 6) . ".bmp";
$dispnewbmp = substr($file, 4, 6) . ".bmp";
if (!is_writable($newbmp)){
    $Content = "";
    $handle = @fopen($newbmp, 'x+');
    fwrite($handle, $Content);
    @fclose($handle);
}

//$newbmp = substr($file, 0, 3) . ".bmp";
            if (is_writable($newbmp)) {
               if (!$newbmp = fopen($newbmp, 'w+b')) {
                     echo "Cannot open file ($newbmp)";
                     exit;
               }
//
// BMP Data
// segment below writes file headers as determined above
// --- sausage
fwrite($newbmp, pack('C', $Header['bfType1']));
fwrite($newbmp, pack('C', $Header['bfType2']));
fwrite($newbmp, pack('L', $Header['bfSize']));
fwrite($newbmp, pack('S', $Header['bfReserved1']));
fwrite($newbmp, pack('S', $Header['bfReserved2']));
fwrite($newbmp, pack('L', $Header['bfOffbits']));
fwrite($newbmp, pack('L', $Header['biSize']));
fwrite($newbmp, pack('L', $Header['biWidth']));
fwrite($newbmp, pack('L', $Header['biHeight']));
fwrite($newbmp, pack('S', $Header['biPlanes']));
fwrite($newbmp, pack('S', $Header['biBitCount']));
fwrite($newbmp, pack('L', $Header['biCompression']));
fwrite($newbmp, pack('L', $Header['biSizeImage']));
fwrite($newbmp, pack('L', $Header['biXPelsPerMeter']));
fwrite($newbmp, pack('L', $Header['biYPelsPerMeter']));
fwrite($newbmp, pack('L', $Header['biClrUsed']));
fwrite($newbmp, pack('L', $Header['biClrImportant']));
//
// BMP Data
// segment below writes the image from 1s and 0s in the array
// --- sausage

        //while($count >= 0){
        while($counter <= $count){
                if($Buffer['px' . $counter] == 0){
                    fwrite($newbmp, pack('C', 0)); //Red
                    fwrite($newbmp, pack('C', 0)); //Green
                    fwrite($newbmp, pack('C', 0)); //Blue
                    // R:0 G:0 B:0 - Black #000000)
                    fwrite($newbmp, pack('C', 0));
                    fwrite($newbmp, pack('C', 0));
                    fwrite($newbmp, pack('C', 0));

                    fwrite($newbmp, pack('C', 0));
                    fwrite($newbmp, pack('C', 0));
                    fwrite($newbmp, pack('C', 0));

                    fwrite($newbmp, pack('C', 0));
                    fwrite($newbmp, pack('C', 0));
                    fwrite($newbmp, pack('C', 0));
                }
                else if ($Buffer['px' . $counter] == 1){
                    fwrite($newbmp, pack('C', 204)); //Red
                    fwrite($newbmp, pack('C', 204)); //Green
                    fwrite($newbmp, pack('C', 204)); //Blue
                                                     // R:204 G:204 B:204 - Grey (#CCCCCC)
                    fwrite($newbmp, pack('C', 204));
                    fwrite($newbmp, pack('C', 204));
                    fwrite($newbmp, pack('C', 204));

                    fwrite($newbmp, pack('C', 204));
                    fwrite($newbmp, pack('C', 204));
                    fwrite($newbmp, pack('C', 204));

                    fwrite($newbmp, pack('C', 204));
                    fwrite($newbmp, pack('C', 204));
                    fwrite($newbmp, pack('C', 204));
                }
                else {
                    fwrite($newbmp, pack('C', 255)); //Red
                    fwrite($newbmp, pack('C', 255)); //Green
                    fwrite($newbmp, pack('C', 255)); //Blue
                                                     // R:255 G:255 B:255 - White (#FFFFFF)

                    fwrite($newbmp, pack('C', 255));
                    fwrite($newbmp, pack('C', 255));
                    fwrite($newbmp, pack('C', 255));

                    fwrite($newbmp, pack('C', 255));
                    fwrite($newbmp, pack('C', 255));
                    fwrite($newbmp, pack('C', 255));

                    fwrite($newbmp, pack('C', 255));
                    fwrite($newbmp, pack('C', 255));
                    fwrite($newbmp, pack('C', 255));
                }
            if($counter == $Header['biWidth'] * $vert) {
                $vert++;
                while($insert < $EXVAR1){
                    fwrite($newbmp, pack('L', 0));
                    $insert++;
                }
                $insert = 0;
            }   

            //$count--;
            $counter++;
        }
//
// Output data
// segment below displays atuff. used for debug
// --- sausage
            //fwrite($newbmp, pack('A', $var2));
            echo "Success, wrote file ($newbmp [<b>$dispnewbmp</b>])";
            //echo "<BR>";
            //echo $headerFull;
            //echo "<BR>";
            //echo $headfullpack;
            //echo "<BR>[ $EXVAR1 ] <pre>";
            //print_r($Header);
            //echo "<BR>";
            //print_r($Buffer);
            //echo "</pre><BR>";
      fclose($newbmp);
fclose($filehandle);
} else {
   echo "The file $newbmp is not writable";
}
?>


Magic.TBL reader -- Requires MSSQL.PHP (Can be found in ShopTool ZIP)
-- So long since I wrote it, Not sure if EDIT works.
Code: Select all
<STYLE>
body {
 font-size: 10;
 font-family: Courier;
}
</STYLE>
<?php
$file = "magic.tbl";
include("mssql.php");
$ACTION = $_GET['ACTION'];
$magic = $_GET['magic'];
global $MagicData;
global $MagicName;

if($ACTION=="VIEW-MAGIC-DATA"){
$filehandle = fopen($file, "r");
$fsize = filesize($file);
$filecontent = fread($filehandle, $fsize);
$filecontent = substr($filecontent, 4, $fsize);
$offset = 0;
$offse2 = 0;
while($offset < $fsize){
    $ThisBit = substr($filecontent, $offset, 54);
    $ThissMid = hexdec(bin2hex(substr($ThisBit, 0, 1)));
    $ThisLen = strlen($ThisBit);
    $ThisMagicName = mssql_fetch_array(mssql_query("SELECT * FROM magic WHERE sMid='$ThissMid'"));
    if($ThisLen <= 50){
        break;
    }
    $ThisCast1 = hexdec(bin2hex(substr($ThisBit, 14,1)));
    $ThisCast2 = hexdec(bin2hex(substr($ThisBit, 18,1)));
    $ThisAtt1 = hexdec(bin2hex(substr($ThisBit, 38,1)));
    $ThisAtt2 = hexdec(bin2hex(substr($ThisBit, 42,1)));
    $ThisFlyOver = hexdec(bin2hex(substr($ThisBit, 26,1)));
    echo "<HR>
<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  <tr>
    <td width=\"30\" bgcolor=\"#CCCCCC\">$ThissMid</td>   
    <td width=\"250\">( <b>$ThisMagicName[strName]</b> )</td>
    <td width=\"120\" align=\"center\"><b>CAST-1=$ThisCast1</b></td>
    <td width=\"120\" align=\"center\"><b>CAST-2=$ThisCast2</b></td>
    <td width=\"120\" align=\"center\"><b>FLYOVER=$ThisFlyOver</b>&nbsp;</td>
    <td width=\"120\" align=\"center\"><b>ATTACK-1=$ThisAtt1</b></td>
    <td width=\"120\" align=\"center\"><b>ATTACK-2=$ThisAtt2</b></td>
  </tr>
</table>";
        while($offse2 < $ThisLen){
            $ThisByte = substr($ThisBit, $offse2, 1);
            echo strtoupper(bin2hex($ThisByte)) . "&nbsp;";
            $offse2 = $offse2+1;
        }
    $offset = $offset + 54;
    $offse2 = 0;
    echo "<BR>";
}
fclose($filehandle);
}

if($ACTION=="EDIT-MAGIC-DATA"){
    $filehandle = fopen($file, "r");
    $fsize = filesize($file);
    $filecontent = fread($filehandle, $fsize);
    $filecontent = substr($filecontent, 4, $fsize);
    $offset = 0;
    $offse2 = 0;
    while($offset < $fsize){
        $ThisBit = substr($filecontent, $offset, 54);
        $ThissMid = hexdec(bin2hex(substr($ThisBit, 0, 1)));
        $ThisLen = strlen($ThisBit);
        $ThisMagicName = mssql_fetch_array(mssql_query("SELECT * FROM magic WHERE sMid='$ThissMid'"));
        $MagicData[$ThissMid] = bin2hex($ThisBit);
        $MagicName[$ThissMid] = "$ThisMagicName[strName]";
        //echo bin2hex($ThisBit);
        if($ThisLen <= 50){
           break;
        }
        if(!$magic){
        echo "<a href=\"?ACTION=EDIT-MAGIC-DATA&magic=$ThissMid\">$ThisMagicName[strName]</a><BR>";
        }
        $offset = $offset + 54;
        $offse2 = 0;
    }
    fclose($filehandle);
}
if($magic > 0){
?>

<table width="400" border="1">
  <tr>
    <td colspan="2"><div align="center"><?php echo $MagicName[$magic]; ?></div></td>
  </tr>
  <tr>
  <td>Cast Image 1</td>
  <td><select name="select">
     </select></td>
  </tr>
  <tr>
    <td>Cast Image 2</td>
    <td><select name="select2">
      </select></td>
  </tr>
  <tr>
    <td>FlyOver Image</td>
    <td><select name="select3">
      </select></td>
  </tr>
  <tr>
    <td>Attack Image 1</td>
    <td><select name="select4">
      </select></td>
  </tr>
  <tr>
    <td>Attack Image 2</td>
    <td><select name="select5">
      </select></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
        <input type="submit" name="Submit" value="Submit">
      </div></td>
  </tr>
</table>
<?
}
?>
<p>Select an option:</p>
<p><a href="?ACTION=EDIT-MAGIC-DATA">Edit A Magic</a><br>
  <a href="?ACTION=VIEW-MAGIC-DATA">View Magic List</a><br>
  Add A Magic</p><br>
You do not have the required permissions to view the files attached to this post.
Tiffany
SD Pro 2 Star
SD Pro 2 Star
 
Posts: 428
Joined: Sat Jan 12, 2008 1:17 pm

Re: [PHP] Imf-bmp Plus Magic Table - sausage

Postby nikijam88 » Mon Feb 25, 2013 6:17 am

thanks for such a very much technical & usefull knowledge ...it should be share around the web.. :geek:
nikijam88
 
Posts: 1
Joined: Mon Feb 25, 2013 6:15 am


Return to Useful Tools

Who is online

Users browsing this forum: No registered users and 5 guests

cron