<?php
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);
require('commonFunctions.php');

$fileSystem = $_GET['fileSystem'];
$id = $_GET['id'];
$ind = $_GET['ind'];
$action = $_GET['action'];
$replace = $_GET['replace'];
$icons = $_GET['icons'];
$batchList = $_POST['batchList'];

$folderPath = "Image Attributes";

$fileSystem = str_replace('items', 'item', $fileSystem);

$flagListIn = array('None', 'CanUse', 'CanTradeNPC', 'NoAuction', 'CanEquip', 'NoSale', 'NoDelivery', 'Ex');
$flagListOut = array('', 'Can Use', 'NPC tradable', 'Not auctionable', 'Equippable', 'Not vendorable', 'Not sendable', 'Not sendable, Exclusive');

$characterList = array();

$characterList['Space'] = array('', 7);
$characterList['A'] = array('Capital A.png', 11);
$characterList['B'] = array('Capital B.png', 11);
$characterList['C'] = array('Capital C.png', 10);
$characterList['D'] = array('Capital D.png', 10);
$characterList['E'] = array('Capital E.png', 10);
$characterList['F'] = array('Capital F.png', 9);
$characterList['G'] = array('Capital G.png', 11);
$characterList['H'] = array('Capital H.png', 10);
$characterList['I'] = array('Capital I.png', 6);
$characterList['J'] = array('Capital J.png', 9);
$characterList['K'] = array('Capital K.png', 11);
$characterList['L'] = array('Capital L.png', 10);
$characterList['M'] = array('Capital M.png', 12);
$characterList['N'] = array('Capital N.png', 10);
$characterList['O'] = array('Capital O.png', 11);
$characterList['P'] = array('Capital P.png', 10);
$characterList['Q'] = array('Capital Q.png', 12);
$characterList['R'] = array('Capital R.png', 11);
$characterList['S'] = array('Capital S.png', 10);
$characterList['T'] = array('Capital T.png', 10);
$characterList['U'] = array('Capital U.png', 10);
$characterList['V'] = array('Capital V.png', 11);
$characterList['W'] = array('Capital W.png', 14);
$characterList['X'] = array('Capital X.png', 11);
$characterList['Y'] = array('Capital Y.png', 11);
$characterList['Z'] = array('Capital Z.png', 11);
$characterList['Grave accent'] = array('Grave accent.png', 9);
$characterList['1'] = array('Number 1.png', 9);
$characterList['2'] = array('Number 2.png', 9);
$characterList['3'] = array('Number 3.png', 9);
$characterList['4'] = array('Number 4.png', 9);
$characterList['5'] = array('Number 5.png', 9);
$characterList['6'] = array('Number 6.png', 9);
$characterList['7'] = array('Number 7.png', 9);
$characterList['8'] = array('Number 8.png', 9);
$characterList['9'] = array('Number 9.png', 9);
$characterList['0'] = array('Number 0.png', 9);
$characterList['Hyphen'] = array('Hyphen.png', 8);
$characterList['Equal sign'] = array('Equal sign.png', 9);
$characterList['Right square bracket'] = array('Right square bracket.png', 8);
$characterList['Left square bracket'] = array('Left square bracket.png', 8);
$characterList['Backslash'] = array('Backslash.png', 7);
$characterList['Semicolon'] = array('Semicolon.png', 5);
$characterList['Apostrophe'] = array('Apostrophe.png', 5);
$characterList['Comma'] = array('Comma.png', 5);
$characterList['Period'] = array('Period.png', 5);
$characterList['Slash'] = array('Slash.png', 7);
$characterList['a'] = array('Lowercase a.png', 9);
$characterList['b'] = array('Lowercase b.png', 9);
$characterList['c'] = array('Lowercase c.png', 9);
$characterList['d'] = array('Lowercase d.png', 9);
$characterList['e'] = array('Lowercase e.png', 9);
$characterList['f'] = array('Lowercase f.png', 7);
$characterList['g'] = array('Lowercase g.png', 9);
$characterList['h'] = array('Lowercase h.png', 9);
$characterList['i'] = array('Lowercase i.png', 5);
$characterList['j'] = array('Lowercase j.png', 6);
$characterList['k'] = array('Lowercase k.png', 9);
$characterList['l'] = array('Lowercase l.png', 5);
$characterList['m'] = array('Lowercase m.png', 12);
$characterList['n'] = array('Lowercase n.png', 9);
$characterList['o'] = array('Lowercase o.png', 10);
$characterList['p'] = array('Lowercase p.png', 9);
$characterList['q'] = array('Lowercase q.png', 9);
$characterList['r'] = array('Lowercase r.png', 6);
$characterList['s'] = array('Lowercase s.png', 8);
$characterList['t'] = array('Lowercase t.png', 7);
$characterList['u'] = array('Lowercase u.png', 9);
$characterList['v'] = array('Lowercase v.png', 9);
$characterList['w'] = array('Lowercase w.png', 11);
$characterList['x'] = array('Lowercase x.png', 9);
$characterList['y'] = array('Lowercase y.png', 9);
$characterList['z'] = array('Lowercase z.png', 8);
$characterList['Tilde'] = array('Tilde.png', 12);
$characterList['Exclamation point'] = array('Exclamation point.png', 7);
$characterList['At sign'] = array('At sign.png', 11);
$characterList['Number sign'] = array('Number sign.png', 10);
$characterList['Dollar sign'] = array('Dollar sign.png', 8);
$characterList['Percent sign'] = array('Percent sign.png', 13);
$characterList['Caret'] = array('Caret.png', 9);
$characterList['Ampersand'] = array('Ampersand.png', 11);
$characterList['Asterisk'] = array('Asterisk.png', 9);
$characterList['Left parentheses'] = array('Left parentheses.png', 8);
$characterList['Right parentheses'] = array('Right parentheses.png', 8);
$characterList['Underscore'] = array('Underscore.png', 9);
$characterList['Plus sign'] = array('Plus sign.png', 9);
$characterList['Right curly bracket'] = array('Right curly bracket.png', 7);
$characterList['Left curly bracket'] = array('Left curly bracket.png', 7);
$characterList['Vertical pipe'] = array('Vertical pipe.png', 6);
$characterList['Colon'] = array('Colon.png', 5);
$characterList['Quotation mark'] = array('Quotation mark.png', 7);
$characterList['Less than'] = array('Less than.png', 9);
$characterList['Greater than'] = array('Greater than.png', 9);
$characterList['Question mark'] = array('Question mark.png', 9);
$characterList['Fire'] = array('Fire-Icon.png', 16);
$characterList['Ice'] = array('Ice-Icon.png', 16);
$characterList['Wind'] = array('Wind-Icon.png', 16);
$characterList['Earth'] = array('Earth-Icon.png', 16);
$characterList['Thunder'] = array('Thunder-Icon.png', 16);
$characterList['Water'] = array('Water-Icon.png', 16);
$characterList['Light'] = array('Light-Icon.png', 16);
$characterList['Dark'] = array('Dark-Icon.png', 16);
$characterList['Male Symbol'] = array('Male Symbol.png', 15);
$characterList['Female Symbol'] = array('Female Symbol.png', 15);
$characterList['Dot'] = array('Dot.png', 7);
$characterList['Large H'] = array('Large H.png', 15);
$characterList['Large G'] = array('Large G.png', 15);
$characterList['Large E'] = array('Large E.png', 15);
$characterList['Large T'] = array('Large T.png', 15);
$characterList['Large M'] = array('Large M.png', 15);

$femaleSign = '&#9792;';
$maleSign = '&#9794;';

$elementIn = array("?Element: Fire?", "?Element: Ice?", "?Element: Wind?", "?Element: Earth?", "?Element: Thunder?", "?Element: Water?", "?Element: Light?", "?Element: Dark?", "?Element: Air?");
$elementShort = array("Fire", "Ice", "Air", "Earth", "Thunder", "Water", "Light", "Dark", "Wind");
$elementIn = array("{{Fire}}", "{{Ice}}", "{{Air}}", "{{Earth}}", "{{Thunder}}", "{{Water}}", "{{Light}}", "{{Dark}}", "{{Wind}}");
$elementOut = array("©", "¢", "£", "¤", "¥", "§", "®", "²", "£");

$specialIn = array('¼', '½', '·', '¯', '±', 'µ', '¹', '¾');
$specialOut = array('Male Symbol', 'Female Symbol', 'Dot', 'Large H', 'Large T', 'Large M', 'Large G', 'Large E');

$charList = array(' ', '`', '-', '=', '[', ']', ' \ ', ';', "'", ',', '.', '/', '_', '+', '{', '}', '|', ':', '"', '<', '>', '?', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', "©", "¢", "£", "¤", "¥", "§", "®", "²", '¼', '½', '·', '¯', '±', 'µ', '¹', '¾');
// "³" unused;
$nameList = array('Space', 'Grave accent', 'Hyphen', 'Equal sign', 'Left square bracket', 'Right square bracket', 'Backslash', 'Semicolon', 'Apostrophe', 'Comma', 'Period', 'Slash', 'Underscore', 'Plus sign', 'Left curly bracket', 'Right curly bracket', 'Vertical pipe', 'Colon', 'Quotation mark', 'Less than', 'Greater than', 'Question mark', 'Tilde', 'Exclamation point', 'At sign', 'Number sign', 'Dollar sign', 'Percent sign', 'Caret', 'Ampersand', 'Asterisk', 'Left parentheses', 'Right parentheses', 'Fire', 'Ice', 'Wind', 'Earth', 'Thunder', 'Water', 'Light', 'Dark', 'Male Symbol', 'Female Symbol', 'Dot', 'Large H', 'Large T', 'Large M', 'Large G', 'Large E');

$raceListIn = array('Hume Male', 'Hume Female', 'HumeMale', 'HumeFemale', 'ElvaanMale', 'ElvaanFemale', 'TarutaruMale', 'TarutaruFemale', 'Mithra', 'Galka', 'Hume', 'Elvaan', 'Tarutaru');
$raceListOut = array('¯¼', '¯½', '¯¼', '¯½', '¾¼', '¾½', '±¼', '±½', 'µ', '¹', '¯', '¾', '±');

$skillListIn = array('GreatAxe', 'GreatKatana', 'GreatSword', 'HandToHand', 'PoleArm', 'StringInstrument', 'Thrown', 'WindInstrument');
$skillListOut = array('Great Axe', 'Great Katana', 'Great Sword', 'Hand-to-Hand', 'Polearm', 'String Instr.', 'Throwing', 'Wind Instr.');

$imageIn = array('"', ':');
$imageOut = array('', '-');

function parseItem($itPrim, $fs, $act) {
	global $elementIn;
	global $elementOut;
    global $elementShort;
    global $skillListIn;
    global $skillListOut;
    global $raceListIn;
    global $raceListOut;
    $primativeIn = array('"', ':', '?');
    $primativeOut = array('_', '_', '_');
    $imageIn = array('"', ':');
    $imageOut = array('', '-');

    $tagList = array();
	$tagList['Aug'] = 0;
	$tagList['Rare'] = 0;
	$tagList['Ex'] = 0;
	$tagList['Temp'] = 0;
    $newRow = explode("|", $itPrim);
    $primative = $newRow[3];
    $wikiName = $newRow[2];
    $longName = ucfirst($newRow[4]);
    $id = $newRow[1];
    $name = ($primative != $longName) ? $longName : $primative;
    
    $iconPath = 'Image Attributes/Icons/';
    if ($newRow[0] == 'FFXI Armor') {
        $fs = 'armor';
        //$iconPath .= 'Armor';
    } elseif ($newRow[0] == 'FFXI Weapon') {
        $fs = 'weapons';
        //$iconPath .= 'Weapons';
    } elseif  ($id < 4096) {
        $fs =  'items1';
        //$iconPath .= 'General';
    } elseif ($id > 28672) {
        $fs =  'vouchers';
        //$iconPath .= 'Vouchers';
    } else {
        $fs =  'items2';
        //$iconPath .= 'Usable';
    }
    
    $primative = str_replace($primativeIn, $primativeOut, $primative);
    $imNum = "$iconPath/[".substr('00000000', 0, (8-strlen($id))). "$id] $primative.png";
    $imTag = "$iconPath/*".substr('00000000', 0, (8-strlen($id)))."$id*.png";
    $imageName = str_replace($imageIn, $imageOut, $wikiName)." description.png";
    //print "$imNum | $imTag | $imageName \r\n<br />";
    
    if (strstr($newRow[6], "Linkshell") !== false && stristr($newRow[4], "link")) {
        $newRow[6] = str_replace('Linkshell', '', $newRow[6]);
        $newRow[6] = str_replace(', ,', ',', $newRow[6]);
    }
    if (strrpos($newRow[6], "Rare") !== false) {
        $tagList['Rare'] = 1;
    }
    if (strrpos($newRow[6], "Ex") !== false) {
        $tagList['Ex'] = 1;
    }
    if ($fs == 'items1' || $fs == 'items2' || $fs == 'vouchers') {
        if ($newRow[0] == 'FFXI Furnishing' || $newRow[0] == 'FFXI Mannequin' || $newRow[0] == 'FFXI Flowerpot') {
            $furnEle = str_replace($elementShort, $elementOut, $newRow[10]);
            $furnNum = trim($newRow[11]);
            $ext = "<$furnEle STG:$furnNum>";
        } else {
            $ext = '';
        }

        $description = trim($longName) . '[br]' . trim($newRow[7]);

        if (stristr($description, "mannequin") !== false) {
            if (strstr($description, "female") !== false) {
                $description = str_replace("?", "½", $description);
            } elseif (strstr($description, "male") !== false) {
                $description = str_replace("?", "¼", $description);
            }
        } elseif (strstr($description, "[br]Functionality:[br]") !== false || $replace == 'on') {
            $description = str_replace(array("° ", "°·", " ?"), array(" ", "·", "·"), $description);
        }

        $description = str_replace($elementIn, $elementOut, $description);
        $description = checkWidth($description, ' ');
        if ($act == 'print') {
            printImage($imageName, $description, $imNum, $imTag, $tagList, '', $ext);
        } elseif ($act == 'batch') {
            makeImage($imageName, $description, $imNum, $imTag, $tagList, '', $ext, false, true, 1);
        } else {
            makeImage($imageName, $description, $imNum, $imTag, $tagList, '', $ext, false, false, $icon);
		}
    } elseif ($fs == 'weapons' || $fs == 'armor') {
        $races = str_replace('"', '', $newRow[12]);
        $races = str_replace(' ', '', $races);

        if ($races == "All") {
            $races = "All Races";
        } elseif ($races == "Male") {
            $races = 'All ¼';
        } elseif ($races == "Female") {
            $races = 'All ½';
        } else {
            $races = str_replace($raceListIn, $raceListOut, $races);
        }
        $enchant = '';
        if ($fs == 'weapons') {
            $skill = str_replace($skillListIn, $skillListOut, $newRow[17]);

            if ($skill == 'Ranged') {
                $skill = 'Archery';
            } elseif ($skill == 'Fishing Gear') {
                $skill = 'Fishing';
            }
            if ($skill == 'None' || $skill == '-' || $skill == 'Grips') {
                $skill = $races;
            } else {
                $skill = "($skill)$races";
            }
            if ($newRow[18] > 0) {
                $enchant1 = getEnchantment($newRow[20], 'l');
                $enchant2 = getEnchantment($newRow[21], 'l');
                $enchant .= "<".$newRow[18]."/".$newRow[18]." $enchant1/[$enchant2, $enchant1]>";
            }
        } else {
            $skill = $newRow[11];

            if  ($skill == "Rings") {
                $skill = 'Ring';
            } elseif ($skill == "Earrings") {
                $skill = 'Ear';
            }
            if  ($skill == "Shields" || $skill == '') {
                $skill = $races;
            } else {
                $skill = "[$skill]$races";
            }
            if ($newRow[16] > 0) {
                $enchant1 = getEnchantment($newRow[18], 'l');
                $enchant2 = getEnchantment($newRow[19], 'l');
                $enchant .= "<".$newRow[16]."/".$newRow[16]." $enchant1/[$enchant2, $enchant1]>";
            }
        }



        $jobs = str_replace('"', '', $newRow[13]);
        $jobs = str_replace($jobListIn, $jobListOut, $jobs);
        if ($jobs != "All") {
            $jobs = str_replace(', ', '/', $jobs);
        } else {
            $jobs = "All Jobs";
        }
        $level = "Lv.".$newRow[10]." $jobs";
        $level = checkWidth($level, '/');
        $description = trim($longName) . '[br]' . trim($skill). '[br]' . trim($newRow[7]);
        $description = str_replace($elementIn, $elementOut, $description);

        if ($replace == 'on') {
            $description = str_replace("?", "~", $description);
        }

        $description = checkWidth($description, ' ');
        if ($act == 'print') {
            printImage($imageName, $description, $imNum, $imTag, $tagList, $level, $enchant);
        } elseif ($act == 'batch') {
			makeImage($imageName, $description, $imNum, $imTag, $tagList, $level, $enchant, true, true, 1);
		} else {
            makeImage($imageName, $description, $imNum, $imTag, $tagList, $level, $enchant, true, false, $makeIcon);
        }
    }
}

function checkWidth($text, $connector) {
    $maxWidth = 317;
    global $characterList;
    global $elementIn;
    global $elementOut;
    global $charList;
    global $nameList;

    $textSplode = explode('[br]', $text);
    foreach ($textSplode as $key => $curDesc) {
        $descSplode = str_split($curDesc);
        $lineSpace = 0;
        $curWord = 0;
        $lines = array();
        $curLine = 0;
        $word = '';
        $wordWidth = 0;
        $lines[$curLine] = array();
        foreach($descSplode as $subKey => $curSplode) {
            $reg = $curSplode;
            if (ord($curSplode) == 92) {
                $curSplode = 'Backslash';
            } else {
                $curSplode = str_replace($charList, $nameList, $curSplode);
            }
            $lineSpace += $characterList[$curSplode][1];
            $wordWidth += $characterList[$curSplode][1];
            if ($reg == $connector) {
                if ($lineSpace > $maxWidth) {
                    $lineSpace = $wordWidth;
                    $curLine++;
                    $lines[$curLine] = array();
                    $curWord = 0;
                    array_push($lines[$curLine], $word);
                    $word = '' ;
                    $wordWidth = 0;
                } else {
                    array_push($lines[$curLine], $word);
                    $word = '';
                    $wordWidth = 0;
                    $curWord++;
               }
            } else {
                $word .= $reg;
            }
        }
        if ($lineSpace > $maxWidth) {
            $lineSpace = $wordWidth;
            $curLine++;
            $lines[$curLine] = array();
        }
        array_push($lines[$curLine], $word);
        foreach ($lines as $k => $curLine) {
            $lines[$k] = implode($connector, $curLine);
        }
        $con = ($connector == '/') ? '/[br]' : '[br]';
        $textSplode[$key] = implode($con, $lines);
    }
    $con = ($connector == '/') ? '/[br]' : '[br]';
    $text = implode($con, $textSplode);
    return $text;
}

function makeImage($imName, $imDesc, $iconPath, $imShort, $tags, $jobs, $extra, $col, $bat, $makeIcon) {
    global $characterList;
    global $charList;
    global $nameList;
    global $specialIn;
    global $specialOut;

    $imageNameIn = array('?', '"', ':');
    $imageNameOut = array('_', '_', '_');

    $iconOffset = array(8, 12);
    $firstLineOffset = array(51, 4);
    $tagOffset = array(314, 4);
    $enchantOffset = array(360, 0);
    $countIn = array(12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
    $countOut = array('Twelve Line Prototype.png', 'Eleven Line Prototype.png', 'Ten Line Prototype.png', 'Nine Line Prototype.png', 'Eight Line Prototype.png', 'Seven Line Prototype.png', 'Six Line Prototype.png', 'Five Line Prototype.png', 'Four Line Prototype.png', 'Three Line Prototype.png', 'Three Line Prototype.png', 'Three Line Prototype.png');
    //$countOut = array('Three Line Prototype.png', 'Three Line Prototype.png', 'Three Line Prototype.png', 'Four Line Prototype.png', 'Five Line Prototype.png', 'Six Line Prototype.png', 'Seven Line Prototype.png', 'Eight Line Prototype.png', 'Nine Line Prototype.png', 'Ten Line Prototype.png', 'Eleven Line Prototype.png', 'Twelve Line Prototype.png');

    $iconPath = str_replace($imageNameIn, $imageNameOut, $iconPath);

    $descList = explode('[br]', $imDesc);
    if (count($descList) > 3 && strlen($descList[count($descList)-1]) == 0) {
        array_pop($descList);
    }
    $len = count($descList);
    $jobList = explode('[br]', $jobs);
    if (strlen($jobs) > 0) {
        $len += count($jobList);
    }
    $numRows = (strlen($extra) > 0) ? $len+1 : $len;
    $boxProto = str_replace($countIn, $countOut, $numRows);
    
    $proto = imagecreatefrompng("Image Attributes/Box Attributes/".$boxProto);
    imagealphablending($proto, true);
    $icon = @imagecreatefrompng($iconPath);
    $blocker = @imagecreatefrompng("Image Attributes/Box Attributes/Blocker.png");
    if ($icon != false) {
        //$filter1 = imagefilter($icon, IMG_FILTER_COLORIZE, 0, 0, 0, 31);
        $filter1 = imagefilter($icon, IMG_FILTER_COLORIZE, 20, 20, 20);
        imagecopy($proto, $icon, $iconOffset[0], $iconOffset[1], 0, 0, imagesx($icon), imagesy($icon));
        imagecopy($proto, $blocker, $iconOffset[0], $iconOffset[1], 0, 0, imagesx($blocker), imagesy($blocker));
    } else {
        $second = glob($imShort);
        if ($second != false && count($second) > 0) {
            $icon = @imagecreatefrompng($second[0]);
            $iconPath = $second[0];
            if ($icon != false) {
                //$filter1 = imagefilter($icon, IMG_FILTER_COLORIZE, 0, 0, 0, 31);
                $filter1 = imagefilter($icon, IMG_FILTER_COLORIZE, 20, 20, 20);
                imagecopy($proto, $icon, $iconOffset[0], $iconOffset[1], 0, 0, imagesx($icon), imagesy($icon));
                imagecopy($proto, $blocker, $iconOffset[0], $iconOffset[1], 0, 0, imagesx($blocker), imagesy($blocker));
            }
        }
    }
    $kNum = 0;
    foreach ($tags as $key => $curTag) {
        if ($curTag == 1) {
            $thisTag = imagecreatefrompng("Image Attributes/Box Attributes/".$key.".png");
            @imagecopy($proto, $thisTag, ($tagOffset[0]+($kNum*16)), $tagOffset[1], 0, 0, 16, 16);
            $kNum++;
        }
    }
    $counter = 0;
    foreach ($descList as $key => $curDesc) {
        $descSplode = str_split($curDesc);
        $lineSpace = 0;
        foreach($descSplode as $subKey => $curSplode) {
            if (ord($curSplode) == 92) {
                $curSplode = 'Backslash';
            } else {
                $curSplode = str_replace($charList, $nameList, $curSplode);
            }
            $curPrint = $characterList[$curSplode];
            if ($curPrint[0] != '') {
                $thisLet = imagecreatefrompng("Image Attributes/Letters-White/".$curPrint[0]);
                @imagecopy($proto, $thisLet, ($firstLineOffset[0]+$lineSpace), ($firstLineOffset[1]+($counter*16)), 0, 0, $curPrint[1], 16);
            }
            $lineSpace += $curPrint[1];
        }
        $counter++;
    }
    if (strlen($jobs) > 0) {
        foreach ($jobList as $key => $curDesc) {
            $descSplode = str_split($curDesc);
            $lineSpace = 0;
            foreach($descSplode as $subKey => $curSplode) {
                if (ord($curSplode) == 92) {
                    $curSplode = 'Backslash';
                } else {
                    $curSplode = str_replace($charList, $nameList, $curSplode);
                }
                $curPrint = $characterList[$curSplode];
                if ($curPrint[0] != '') {
                    $thisLet = imagecreatefrompng("Image Attributes/Letters-White/".$curPrint[0]);
                    @imagecopy($proto, $thisLet, ($firstLineOffset[0]+$lineSpace), ($firstLineOffset[1]+($counter*16)), 0, 0, $curPrint[1], 16);
                }
                $lineSpace += $curPrint[1];
            }
        $counter++;
        }
    }
    if (strlen($extra) > 0) {
        $extSplode = str_split($extra);
        $lineSpace = 0;
        krsort($extSplode);
        foreach($extSplode as $curExt) {
            if (ord($curExt) == 92) {
                $curExt = 'Backslash';
            } else {
                $curExt = str_replace($charList, $nameList, $curExt);
            }
            $curPrint = $characterList[$curExt];
            $lineSpace += $curPrint[1];
            if ($curExt != 'Space') {
                $thisLet = imagecreatefrompng("Image Attributes/Letters-White/".$curPrint[0]);
                if ($col == true) {
                    $filter1 = imagefilter($thisLet, IMG_FILTER_BRIGHTNESS, -70);
                    $filter2 = imagefilter($thisLet, IMG_FILTER_COLORIZE, 0, 65, 85);
                }
                @imagecopy($proto, $thisLet, ($enchantOffset[0]-$lineSpace), ($firstLineOffset[1]+($counter*16)), 0, 0, $curPrint[1], 16);
            }
        }
    }
    if ($bat == false) {
        header('Content-type: image/png');
        imagepng($proto);
        imagedestroy($proto);
    } else {
        $imdest = str_replace(':', '-', $imName);
        $iconcopy = "ThumbDump/".str_replace(' description.png', ' icon.png', $imdest);
        $imdest = "ImageDump/".$imdest;
        imagepng($proto, $imdest);
        if ($makeIcon == 1) {
            if (copy($iconPath, $iconcopy) == true) {
                print "http://www.septimusprime.com/bgwiki/$iconcopy\r\n<br /><br /><img src=\"$iconcopy\">\r\n<br /><br />";
            }
        }
        print "http://www.septimusprime.com/bgwiki/$imdest\r\n<br /><br /><img src=\"$imdest\">\r\n<br /><br /><br /><br />";
    }
}

function printImage($imName, $imDesc, $iconPath, $imShort, $tags, $jobs, $extra) {
    global $characterList;
    global $charList;
    global $nameList;
    global $specialIn;
    global $specialOut;
    
    $imageNameIn = array('?', '"');
    $imageNameOut = array('_', '_');

    $iconOffset = array(8, 12);
    $firstLineOffset = array(51, 4);
    $tagOffset = array(314, 4);
    $enchantOffset = array(351, 0);
    $countIn = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
    $countOut = array('Three Line Prototype.png', 'Three Line Prototype.png', 'Three Line Prototype.png', 'Four Line Prototype.png', 'Five Line Prototype.png', 'Six Line Prototype.png', 'Seven Line Prototype.png', 'Eight Line Prototype.png', 'Nine Line Prototype.png', 'Ten Line Prototype.png', 'Eleven Line Prototype.png', 'Twelve Line Prototype.png');
    
    $imDesc = str_replace($elementIn, $elementOut, $imDesc);

    $descList = explode('[br]', $imDesc);
    $len = count($descList);
    $jobList = explode('[br]', $jobs);
    if (strlen($jobs) > 0) {
        $len += count($jobList);
    }
    $numRows = (strlen($extra) > 0) ? $len+1 : $len;
    $numRows = ($numRows < 3) ? 3 : $numRows;
    $boxProto = str_replace($countIn, $countOut, $numRows);

    $iName = $iconPath;
    $iconPath = str_replace($imageNameIn, $imageNameOut, $iconPath);
    if (is_file($iconPath) == false) {
        $second = glob($imShort, GLOB_NOCHECK);
        print_r ($second);
        print "   $imShort<br />";
        if ($second != false && count($second) > 0) {
            $iconPath = $second[0];
        }
    }

    print "<img src=\"Image Attributes/Box Attributes/".$boxProto."\"><br />";
    print $iconPath."<br />";
    print "<img src=\"$iconPath\">";

    foreach ($tags as $key => $curTag) {
        if ($curTag == 1) {
            print "<img src=\"Image Attributes/Box Attributes/".$key.".png\">";
        }
    }

    print "<br />";

    foreach ($descList as $key => $curDesc) {
        $descSplode = str_split($curDesc);
        $lineSpace = 0;
        print "<br />";
        foreach($descSplode as $subKey => $curSplode) {
            if (ord($curSplode) == 92) {
                $curSplode = 'Backslash';
            } else {
                $curSplode = str_replace($charList, $nameList, $curSplode);
            }
            $curPrint = $characterList[$curSplode];
            if ($curPrint[0] != '') {
                print "<img src=\"Image Attributes/Letters-White/".$curPrint[0]."\">";
            } else {
                print "&nbsp;";
            }
            $lineSpace += $curPrint[1];
        }
    }

    if (strlen($job) > 0) {
        foreach ($jobList as $key => $curDesc) {
            $descSplode = str_split($curDesc);
            $lineSpace = 0;
            print "<br />";
            foreach($descSplode as $subKey => $curSplode) {
                if (ord($curSplode) == 92) {
                    $curSplode = 'Backslash';
                } else {
                    $curSplode = str_replace($charList, $nameList, $curSplode);
                }
                $curPrint = $characterList[$curSplode];
                if ($curPrint[0] != '') {
                    print "<img src=\"Image Attributes/Letters-White/".$curPrint[0]."\">";
                } else {
                    print "&nbsp;";
                }
                $lineSpace += $curPrint[1];
            }
        }
    }
    if (strlen($extra) > 0) {
        print "\r\n<br />";
        $extSplode = str_split($extra);
        $lineSpace = 0;
        foreach($extSplode as $curExt) {
            if (ord($curExt) == 92) {
                $curExt = 'Backslash';
            } else {
                $curExt = str_replace($charList, $nameList, $curExt);
            }
            $curPrint = $characterList[$curExt];
            $lineSpace -= $curPrint[1];
            if ($curPrint[0] != '') {
                print "<img src=\"Image Attributes/Letters-White/".$curPrint[0]."\">";
            } else {
                print "&nbsp;";
            }

        }
    }
}

if ($action == 'batch' && strlen($ind) == 0) {
    if (strlen($batchList) > 0) {
        $array = @file('ultimateParse.txt');
        $ind = array();
        $batchArr = explode("\r", $batchList);
        $searchArr = array();
        foreach ($array as $curArr) {
            $curData = explode('|', $curArr);
            $curLet = substr($curData[3], 0, 1);
            if (is_null($searchArr[$curLet])) {
                $searchArr[$curLet] = array();
            }
            $searchArr[$curLet][] = $curData;
        }
        $thumbList = array();
        $descList = array();
        
        foreach ($batchArr as $row) {
            if (strpos($row, '(Scroll)') !== false) {
                $newRow = explode('(', $row);
                $row = $newRow[0];
            }
            $row = trim($row);
            $row = str_replace('Plus ', '+', $row);
            $test = false;
            if (strpos($row, "*") !== false) {
                $row = str_replace('* ', '', $row);
                $row = str_replace('# ', '', $row);
                $curLet = substr($row, 0, 1);
                $retArr = array();
                print "<b>$row</b><br /><br />\r\n";
                foreach ($searchArr[$curLet] as $data) {
                    if ($data[3] == $row) {
                        $datum = implode('|', $data);
                        parseItem($datum, 'batch', $action);
                        array_push($descList, str_replace($imageIn, $imageOut, $data[2])." description.png");
                        array_push($thumbList, str_replace($imageIn, $imageOut, $data[2])." icon.png");
                        $test = true;
                        break;
                    }
                }
                if ($test == false) {
                    print "<b>ERROR: </b>Unable to pattern match an item named: '$row'<br /><br />\r\n";
                }
            }
        }
        print "<br /><br /><br />iMacros loop: ".count($descList)." iterations.<br />\r\n";
        foreach ($descList as $curVal) {
            print "\"$curVal\"<br />\r\n";
        }
        print "<br /><br />iMacros loop: ".count($descList)." iterations.<br />\r\n";
        foreach ($thumbList as $curVal) {
            print "\"$curVal\"<br />\r\n";
        }
    }
} else if (strlen($ind) > 0) {
    $fileName = $fileSystem.'.txt';
    $array = @file($fileName);

    $indList = explode(',', $ind);
    $index = array();
    $thumbList = array();
    $descList = array();
    foreach ($array as $key => $row) {
        $curRow = explode('|', $row);
        $index[$curRow[1]] = $key;
    }
    
    foreach($indList as $k => $curInd) {
        if (strlen($curInd) > 0 && $index[$curInd] !== null) {
            parseItem($array[$index[$curInd]], $fileSystem, $action);
            $data = explode('|', $array[$index[$curInd]]);
            array_push($descList, str_replace($imageIn, $imageOut, $data[2])." description.png");
            array_push($thumbList, str_replace($imageIn, $imageOut, $data[2])." icon.png");
        }
    }
    print "<br /><br /><br />iMacros loop: ".count($descList)." iterations.<br />\r\n";
    foreach ($descList as $curVal) {
        print "\"$curVal\"<br />\r\n";
    }
    print "<br /><br />iMacros loop: ".count($descList)." iterations.<br />\r\n";
    foreach ($thumbList as $curVal) {
        print "\"$curVal\"<br />\r\n";
    }
} else if (strlen($id) > 0) {
	$fileName = $fileSystem.'Data.txt';
	$array = @file($fileName);
 
    parseItem($array[$id], $fileSystem, $action);
} else {
    print "This form is for batch processing images from the
    <a href=\"http://wiki.bluegartrls.com/bg/Category:Item_Image\" target=\"_blank\">
    BG Wiki Item Image category page.</a>  Copy
    and paste a page of links into the following text field to automagically
    generate item images for that page.  Please do not generate more than one page
    (200 images) at a time.<br />
    Addtional information has been included at the bottom of the file, designed
    for Septimus's use for handling batch uploads.<br /><br />
    <form id=\"form1\" name=\"form1\" method=\"post\" action=\"Image-O-Matic.php?action=batch\">
  <label>Item List<br />
  <textarea name=\"batchList\" id=\"batchList\" cols=\"45\" rows=\"5\"></textarea>
  </label>
  <p>
    <label>
    <input type=\"submit\" name=\"Submit\" id=\"Submit\" value=\"Submit\" />
    </label>
    <label>
    <input type=\"reset\" name=\"Reset\" id=\"Reset\" value=\"Reset\" />
    </label>
  </p>
</form>";
}
?>
