<?php

$crates = $_POST['crates'];
$height = $_POST['height'];
$width  = $_POST['width'];
$depth  = $_POST['depth'];
$board  = $_POST['board'];
$thick  = $_POST['thick'];
$clear  = $_POST['door'];
$slop   = $_POST['toggle'];
$space  = $_POST['space'];
$zoom   = $_POST['zoom'];

$tried = $crates;
if ($tried) {

$crates = intval ($crates);
$height = round ($height * 16) / 16;
$width  = round ($width * 16) / 16;
$depth  = round ($depth * 16) / 16;
$clear  = round ($clear * 16) / 16;
$slop   = round ($slop  * 16) / 16;
$board  = round ($board * 16) / 16;
$space  = round ($space);
$zoom   = round ($zoom);
}

?>
<div class="float-l">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<table border="0" cellpadding="0">
<tr><td>How many crates
</td><td><input type="text" maxlength="2" size="3" name="crates" 
value="<?php if ($crates < 1){$crates = 1; echo $crates; }else {echo $crates;} ?>" />
</td></tr><tr><th class="red-s"> &nbsp; (dimensions round to 1/16)</th></tr>
<tr><td>Height of contents
</td><td><input type="text" maxlength="8" size="7" name="height" 
value="<?php if ($height < 6){$height = 96; echo $height; }else {echo $height;} ?>" />
</td></tr><tr><td>Width of contents</td><td>
<input type="text" maxlength="8" size="7" name="width" 
value="<?php if ($width < 6){$width = 48; echo $width; }else {echo $width;} ?>" />
</td></tr><tr><td>Depth of contents</td><td>
<input type="text" maxlength="8" size="7" name="depth" 
value="<?php if ($depth < 6){$depth = 36; echo $depth; }else {echo $depth;} ?>" />
</td></tr><tr><td>Lumber width</td><td>
<input type="text" maxlength="8" size="7" name="board" 
value="<?php if ($board < .5){$board = 3.5; echo $board; }else {echo $board;} ?>" />
</td></tr><tr><td>Lumber + ply thickness</td><td>
<input type="text" maxlength="8" size="7" name="thick" 
value="<?php if ($thick < .5){$thick = 1.125; echo $thick; }else {echo $thick;} ?>" />
</td></tr><tr><td>Clearance for door</td><td>
<input type="text" maxlength="5" size="7" name="door" 
value="<?php if ($clear < .001){$clear = .5; echo $clear; }else {echo $clear;} ?>" />
</td></tr><tr><td>Toggle slop</td><td>
<input type="text" maxlength="5" size="7" name="toggle" 
value="<?php if ($slop < .001){$slop = .125; echo $slop; }else {echo $slop;} ?>" />
</td></tr><tr><td>Minimum toggle spacing</td><td>
<input type="text" maxlength="5" size="7" name="space" 
value="<?php if ($space < 3 * $board){$space = 24; echo $space; }else {echo $space;} ?>" />
</td></tr><tr><td>Image size</td><td>
<input type="text" maxlength="3" size="4" name="zoom" 
value="<?php if ($zoom < 1){$zoom = 100; echo $zoom; }else {echo $zoom;} ?>" />
%</td></tr>
</table>
<p><input type="hidden" name="tried" value="yes" />
<input type="submit" value="<?php echo $tried ? 'recalculate' : 'calculate'; ?>" />
</p>
</form>
</div>

<?php

if (!$tried) 
{ 
echo '<div class="float-lt"><img src="/archives/images/crate.png" alt="default crate diagram" /></div>';
}

function rnd16 ($n) { return intval (round ($n * 16)); } // round to 1/16"

function make_frac ($num) { $mod=fmod ($num, 1) * 16; // build smallest fraction
if (1 & $mod) { return " - ".$mod."/16"; } 
else $mod = $mod/2;
if (1 & $mod) { return " - ".$mod."/8"; }
else $mod = $mod/2;
if (1 & $mod) { return " - ".$mod."/4"; }
else $mod = $mod/2;
if (1 & $mod) { return " - ".$mod."/2"; }
}

if ($tried) {

$txt    = 2;                                  // text size
$txt2   = $txt-1;                             // smaller
$thick  = $thick * 2;                         // thickness of panels
$thick2 = $thick/2;                           // thickness of one panel

$width2 = $width + $thick;                    // width added for panels
$kerf   = 1 + $slop * 10;                     // visual separation of lumber
$width2 = $width + $thick;                    // width label
$depth2 = $depth + $thick;                    // depth label
$num3   = $height;                            // height label
$tog_ht = ceil ( ($height-$board) / $space)-1;// # interior toggles for height
$tog_dp = ceil ( ($depth-$board)/$space)-1;   // n. interior toggles for depth
$tog_h_cl = ($height-$board)/ ($tog_ht + 1);  // centerline spacing for toggles
$tog_d_cl = ($depth-$board)/ ($tog_dp + 1);   // centerline spacing for toggles
$kb = (24 + ( (8 * $tog_ht) + (4 * $tog_dp))) * $crates; // Corner Blocks
  
$zoom_f    = 300/ ($height + $width + $depth) * ($zoom/100); // zoom factor
$height_z  = $height * $zoom_f;
$widthz    = $width * $zoom_f;
$width2z   = $width2 * $zoom_f;
$depthz    = $depth * $zoom_f;
$depth2z   = $depth2 * $zoom_f;
$thickz    = $thick * $zoom_f;
$thick2z   = $thick2 * $zoom_f;
$tog_h_clz = $tog_h_cl * $zoom_f;
$tog_d_clz = $tog_d_cl * $zoom_f;
$boardz    =  ($board * $zoom_f)-.2 * $zoom_f;

$id = uniqid ("");
$id1 = "a".$id;      // used for image 1
$id2 = "b".$id;      // used for image 2
$id3 = "c".$id;      // used for image 3

//image size and colors
$pic   = ImageCreate ($width2z + 40, $height_z + 30);
$bg    = ImageColorAllocate ($pic, 255, 255, 255);
$trans = imagecolortransparent ($pic, $bg); 
$ply   = ImageColorAllocate ($pic, 240, 235, 180);
$wood  = ImageColorAllocate ($pic, 200, 170, 80);
$black = ImageColorAllocate ($pic, 0, 0, 0);

// build crate
ImageFilledRectangle ($pic, 0, 0, $widthz, $height_z, $ply);
ImageFilledRectangle ($pic, 0, 0, $boardz, $height_z, $wood);
ImageFilledRectangle ($pic, $widthz - $boardz, 0, $widthz, $height_z, $wood);
ImageFilledRectangle ($pic, $boardz + $kerf, 0, 
$widthz - $boardz - $kerf, $boardz, $wood);
ImageFilledRectangle ($pic, $boardz + $kerf, $height_z - $boardz, 
$widthz - $boardz - $kerf, $height_z, $wood);

// make toggles
$i = $tog_ht;
while ($i > 0)
{
ImageFilledRectangle ($pic, $boardz + $kerf, $tog_h_clz * $i,
$widthz - $boardz - $kerf, $tog_h_clz * $i + $boardz, $wood);
$i--;
}

// make labels
imagestring ($pic, $txt, 0, $height_z + 3, "door + back", $black);
if (52 < $widthz)
{
imagestring ($pic, $txt, ($widthz / 2) - 22, $height_z + 15, $width2, $black);
imagestring ($pic, $txt, 0, $height_z + 15, "<", $black);
imagestring ($pic, $txt, $widthz - 6, $height_z + 15, ">", $black);
}
else
{
imagestring ( $pic, $txt, 0, $height_z + 15, $width2, $black);
}
ImagePNG ($pic, "$_SERVER[DOCUMENT_ROOT]/cache/$id1.png");
ImageDestroy ($pic);

//image size and colors
$pic   = ImageCreate ($depth2z + 60, $height_z + 30);
$bg    = ImageColorAllocate ($pic, 255, 255, 255);
$trans = imagecolortransparent ($pic, $bg); 
$ply   = ImageColorAllocate ($pic, 240, 235, 180);
$wood  = ImageColorAllocate ($pic, 200, 170, 80);
$black = ImageColorAllocate ($pic, 0, 0, 0);

// build crate
ImageFilledRectangle ($pic, 0, 0, $depthz, $height_z, $ply);
ImageFilledRectangle ($pic, 0, 0, $boardz, $height_z, $wood);
ImageFilledRectangle ($pic, $depthz - $boardz, 0, $depthz, $height_z, $wood);
ImageFilledRectangle ($pic, $boardz + $kerf, 0, $depthz - $boardz - $kerf, $boardz, $wood);
ImageFilledRectangle ($pic, $boardz + $kerf, $height_z - $boardz, $depthz - $boardz - $kerf, 
$height_z, $wood);

// make toggles
$i = $tog_ht;
while ($i > 0) 
{
ImageFilledRectangle ($pic, $boardz + $kerf, $tog_h_clz * $i,
$depthz - $boardz - $kerf, $tog_h_clz * $i + $boardz, $wood);
$i--;
}

// make labels
imagestring ($pic, $txt, 0, $height_z + 3, "sides", $black);

if (54 < $depthz)
{
imagestring ($pic, $txt, ($depthz / 2) - 22, $height_z + 15, $depth, $black);
imagestring ($pic, $txt, 0, $height_z + 15, "<", $black);
imagestring ($pic, $txt, $depthz-6, $height_z + 15, ">", $black);
}
else
{
imagestring ($pic, $txt, 0, $height_z + 15, $depth, $black);
}
if (24 < $height_z)
{ 
imagestring ($pic, $txt2, $depthz + 12, 0, "/\\", $black);
imagestring ($pic, $txt, $depthz + 6, $height_z / 2 - 5, $height, $black);
imagestring ($pic, $txt2, $depthz + 12, $height_z - 6, "\\/", $black);
}
else
{
imagestring ($pic, $txt, $depthz + 6, $height_z / 2 - 5, $height, $black);
}
ImagePNG ($pic, "$_SERVER[DOCUMENT_ROOT]/cache/$id2.png");
ImageDestroy ($pic);

//image size and colors
$pic   = ImageCreate ($widthz + 80, $depthz + 30);
$bg    = ImageColorAllocate ($pic, 255, 255, 255);
$trans = imagecolortransparent ($pic, $bg); 
$ply   = ImageColorAllocate ($pic, 240, 235, 180);
$wood  = ImageColorAllocate ($pic, 200, 170, 80);
$black = ImageColorAllocate ($pic, 0, 0, 0);

// build crate
ImageFilledRectangle ($pic, 0, 0, $widthz, $depthz, $ply);
ImageFilledRectangle ($pic, 0, 0, $boardz, $depthz, $wood);
ImageFilledRectangle ($pic, $widthz - $boardz, 0, $widthz, $depthz, $wood);
ImageFilledRectangle ($pic, $boardz + $kerf, 0, $widthz - $boardz - $kerf, $boardz, $wood);
ImageFilledRectangle ($pic, $boardz + $kerf, $depthz - $boardz, $widthz - $boardz - $kerf, $depthz, $wood);

// make toggles
$i = $tog_dp;
while ($i > 0) 
{
ImageFilledRectangle ($pic, $boardz + $kerf, $tog_d_clz * $i, 
$widthz - $boardz - $kerf, $tog_d_clz * $i + $boardz, $wood);
$i--;
}

// make labels
imagestring ($pic, $txt, 0, $depthz + 3, "top + bottom", $black);

if (52 < $widthz)
{
imagestring ($pic, $txt, ($widthz / 2) - 22, $depthz + 15, $width2, $black);
imagestring ($pic, $txt, 0, $depthz + 15, "<", $black);
imagestring ($pic, $txt, $widthz - 6, $depthz + 15, ">", $black);
}
else
{
imagestring ($pic, $txt, 0, $depthz + 15, $width2, $black);
}

if (24 < $depthz)
{
imagestring ($pic, $txt2, $widthz + 12, 0, "/\\", $black);
imagestring ($pic, $txt, $widthz + 6, $depthz / 2 - 5, $depth2, $black);
imagestring ($pic, $txt2, $widthz + 12, $depthz - 6, "\\/", $black);
}
else
{
imagestring ($pic, $txt, $widthz + 6, $depthz/2 - 5, $depth2, $black);
}
ImagePNG ($pic, "$_SERVER[DOCUMENT_ROOT]/cache/$id3.png");
ImageDestroy ($pic);

// display images
echo'<div class="float-lt">
<img src="/cache/'.$id1.'.png" alt="crate_front" />
<img src="/cache/'.$id2.'.png" alt="crate_side" />
<br />
<img src="/cache/'.$id3.'.png" alt="crate_top" />
</div>';

// Cut list
$array = array_merge 
(
array_fill (0,  $crates * 6, $height), 
array_fill (0,  $crates * 2, $height - $clear), 
array_fill (0,  $crates * 4, $depth2), 
array_fill (0, ($crates * 2 * $tog_dp) + ($crates * 2 * $tog_ht) + 8, $width2 - ($board * 2 + $slop)), 
array_fill (0, ($crates * 2 * $tog_ht) + 4, $depth - ($board * 2 + $slop))
);

$array = array_map ("rnd16", $array);
$array= (array_count_values ($array));
krsort ($array);
echo '<div class="float-l"><table cellpadding="0">
<caption><ins><h3>Cut List</h3></ins><b>&nbsp;&nbsp;#&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Length</b></caption>';
while (list ($key, $val) = each ($array)) {
    $key=$key/16;
echo "<tr><td align=\"right\">$val</td><td>&nbsp; @ &nbsp;</td>
<td align=\"right\">".intval ($key)." </td><td> ".make_frac ($key)." </td></tr>";
}
echo "</table><br />$kb Corner Blocks</div>";
}

?>
<div><br class="clearall" /></div>