<?php
require_once('Image/3D.php');
require_once('Image/3D/Color/Metal.php');
require_once('Image/3D/Paintable/Light/Ambient.php');
// Create the blank three-dimensional space
// RJM Programming
// September, 2019
// Thanks to https://pear.php.net/package/Image_3D and https://www6.software.ibm.com/developerworks/education/os-php-3d/os-php-3d-a4.pdf

$shapechoices=["cone","cube","sphere","torus","star","helicoid","spherocube","bundt"];
$isget=false;
$r=0;
$g=0;
$b=0;
$roll=-45;
$pitch=-120;
$yaw=-10;
$detail=64;
$detail2=10;
$width=400;
$height=400;
$light="Light";
$alight1="Light";
$alight2="Light";
$blight1="#6464ff";
$blight2="#64ff64";
$pspect="Perspectively";
$driver="gd";
$ext=".png";
$topp="top";
$onl="";
$hi="<img id=topimg title='" . datauriit('object' . $shape . $ext) . "' src='object" . $shape . $ext . "?huh=" . rand(0,19876543) . "'></img>";
$bsg="border:1px solid lightblue;";
$onl=" onload='tdtitle();'";
if (trim(('' . $_SERVER['HTTP_REFERER'])) != '') { $onl="";  $bsg="";  }
if (strpos(strtolower(trim(('' . $_SERVER['HTTP_REFERER']))), "/itblog/") !== false) { $topp="parent"; $bsg="border:1px solid lightblue;";  }
if (strpos(strtolower(trim(('' . $_SERVER['HTTP_REFERER']))), "/wordpress/") !== false) { $topp="parent";   $bsg="border:1px solid lightblue;";  }

$xmove=-50;
$ymove=-30;
$zmove=10;

$xscale=70;
$yscale=220;
$zscale=70;

$xlight1=-300;
$ylight1=0;
$zlight1=-300;

$xlight2=300;
$ylight2=-300;
$zlight2=-300;

$radius=100;
$radiusouter=100;

$blight1r=100;
$blight1g=100;
$blight1b=255;
$blight2r=100;
$blight2g=255;
$blight2b=100;

$bgrd="#ffffff";
$bgrdr=255;
$bgrdg=255;
$bgrdb=255;

$scol="#ffffff";
$sgrdr=255;
$sgrdg=255;
$sgrdb=255;


$mcol="#ffffff";
$mgrdr=255;
$mgrdg=255;
$mgrdb=255;

$mval="1.25";
$mvalh="";
$mchecked="";
$mc="";

$parentdot="";
$parentdotx="";

$topoverlay=" 

 function diframe(iois) {
  if (iois != null) {
    bconto = (iois.contentWindow || iois.contentDocument);
    if (bconto != null) {
   try {
     if (bconto.document) { bconto = bconto.document; }
     if (bconto.getElementById('topimg')) { bc=bconto.getElementById('topimg').title; parent.bc=bconto.getElementById('topimg').title; }
   } catch(eee) { }
   }
  }
 }

 function ciframe(iois) {
  if (iois != null) {
    aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
   try {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.getElementById('topimg')) {
      if ((aconto.getElementById('topimg').title) != '') {
       if (document.getElementById('td_' + tdfs)) {
        document.getElementById('td_' + tdfs).title=aconto.getElementById('topimg').title;
       }
      }
     }
   } catch(eee) { }
   }
  }
 }
 
 function tdtitle() {
     if (document.getElementById('topimg')) {
      if ((document.getElementById('topimg').title) != '') {
       if (document.getElementById('td_' + tdfs)) {
        document.getElementById('td_' + tdfs).title=document.getElementById('topimg').title;
       }
      }
     }
 }

   function topoverlay() {
     if (!document.getElementById('form_' + tdfs) && aconto) { 
     if (aconto.getElementById('form_' + tdfs).method.toUpperCase() != 'GET') {
     aconto.getElementById('form_' + tdfs).target='ifinv';
     aconto.getElementById('form_' + tdfs).method='GET';
     //aconto.getElementById('ifinv').src=document.URL.split('#')[0].split('?')[0];
     aconto.getElementById('submit_' + tdfs).click();
     setTimeout(topoverlay, 5000);
     } else if (aconto.getElementById('driver_' + tdfs).value.toLowerCase() == 'gd') {
     
     //if (!" . $topp . ".document.getElementById('topimg') && parent.document.getElementById('topimg')) {
     if (parent.document.getElementById('topimg')) {


     if (parent.document.body.innerHTML.indexOf('imgoverlay') == -1) {
     parent.document.getElementById('topimg').style.opacity='0.5';
     parent.document.getElementById('topimg').src=parent.document.getElementById('topimg').title;
     parent.document.getElementById('topimg').title='';
     }
     if (" . $parentdot ."bconto) {
     parent.document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=' + bc + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     } else {
     parent.document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=object' + tdfs + '.png?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     }


     } else if (!" . $topp . ".document.getElementById('topimg')) {

     if (document.body.innerHTML.indexOf('imgoverlay') == -1) {
     document.getElementById('topimg').style.opacity='0.5';
     document.getElementById('topimg').src=document.getElementById('topimg').title;
     document.getElementById('topimg').title='';
     }
     if (" . $parentdot ."bconto) {
     document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=' + bc + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     } else {
     document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=object' + tdfs + '.png?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     }


     } else {     
     if (" . $topp . ".document.body.innerHTML.indexOf('imgoverlay') == -1) {
     " . $topp . ".document.getElementById('topimg').style.opacity='0.5';
     " . $topp . ".document.getElementById('topimg').src=" . $topp . ".document.getElementById('topimg').title;
     " . $topp . ".document.getElementById('topimg').title='';
     }
     if (" . $parentdot ."bconto) {
     " . $topp . ".document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=' + bc + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     } else {
     " . $topp . ".document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=object' + tdfs + '.png?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     }
     }
     
     aconto.getElementById('form_' + tdfs).target='';
     aconto.getElementById('form_' + tdfs).method='POST';
     } else { 
     //if (!" . $topp . ".document.getElementById('topimg') && parent.document.getElementById('topimg')) {
     if (parent.document.getElementById('topimg')) {
     if (parent.document.body.innerHTML.indexOf('imgoverlay') == -1) {
     parent.document.getElementById('topimg').style.opacity='0.5';
     //parent.document.getElementById('topimg').src=parent.document.getElementById('topimg').title;
     }
     parent.document.getElementById('otherimg').innerHTML+='<img class=imgoverlay src=object' + tdfs + '.svg?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     } else if (!" . $topp . ".document.getElementById('topimg')) {
     if (document.body.innerHTML.indexOf('imgoverlay') == -1) {
     document.getElementById('topimg').style.opacity='0.5';
     //document.getElementById('topimg').src=document.getElementById('topimg').title;
     }
     document.getElementById('otherimg').innerHTML+='<img class=imgoverlay src=object' + tdfs + '.svg?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     } else {
     if (" . $topp . ".document.body.innerHTML.indexOf('imgoverlay') == -1) {
     " . $topp . ".document.getElementById('topimg').style.opacity='0.5';
     //" . $topp . ".document.getElementById('topimg').src=" . $topp . ".document.getElementById('topimg').title;
     }
     " . $topp . ".document.getElementById('otherimg').innerHTML+='<img class=imgoverlay src=object' + tdfs + '.svg?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     }
     aconto.getElementById('form_' + tdfs).target='';
     aconto.getElementById('form_' + tdfs).method='POST';
     }
     
     } else {
     if (document.getElementById('form_' + tdfs).method.toUpperCase() != 'GET') {
     document.getElementById('form_' + tdfs).target='ifinv';
     document.getElementById('form_' + tdfs).method='GET';
     //document.getElementById('ifinv').src=document.URL.split('#')[0].split('?')[0];
     document.getElementById('submit_' + tdfs).click();
     setTimeout(topoverlay, 5000);
     } else if (document.getElementById('driver_' + tdfs).value.toLowerCase() == 'gd') {
     //if (!" . $topp . ".document.getElementById('topimg') && parent.document.getElementById('topimg')) {
     if (parent.document.getElementById('topimg')) {
     if (parent.document.body.innerHTML.indexOf('imgoverlay') == -1) {
     parent.document.getElementById('topimg').style.opacity='0.5';
     parent.document.getElementById('topimg').src=parent.document.getElementById('topimg').title;
     parent.document.getElementById('topimg').title='';
     }
     if (" . $parentdot ."bconto) {
     parent.document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=' + bc + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     } else {
     parent.document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=object' + tdfs + '.png?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     }
     } else if (!" . $topp . ".document.getElementById('topimg')) {

     if (document.body.innerHTML.indexOf('imgoverlay') == -1) {
     document.getElementById('topimg').style.opacity='0.5';
     document.getElementById('topimg').src=document.getElementById('topimg').title;
     document.getElementById('topimg').title='';
     }
     if (" . $parentdot ."bconto) {
     document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=' + bc + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     } else {
     document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=object' + tdfs + '.png?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     }


     } else {
     if (" . $topp . ".document.body.innerHTML.indexOf('imgoverlay') == -1) {
     " . $topp . ".document.getElementById('topimg').style.opacity='0.5';
     " . $topp . ".document.getElementById('topimg').src=" . $topp . ".document.getElementById('topimg').title;
     " . $topp . ".document.getElementById('topimg').title='';
     }
     if (" . $parentdot ."bconto) {
     " . $topp . ".document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=' + " . $parentdot ."bc + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     } else {
     " . $topp . ".document.getElementById('otherimg').innerHTML+='<img class=imgoverlay title=\"\" src=object' + tdfs + '.png?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     }
     }
     document.getElementById('form_' + tdfs).target='';
     document.getElementById('form_' + tdfs).method='POST';
     } else { 
     //if (!" . $topp . ".document.getElementById('topimg') && parent.document.getElementById('topimg')) {
     if (parent.document.getElementById('topimg')) {
     if (parent.document.body.innerHTML.indexOf('imgoverlay') == -1) {
     parent.document.getElementById('topimg').style.opacity='0.5';
     //parent.document.getElementById('topimg').src=parent.document.getElementById('topimg').title;
     }
     parent.document.getElementById('otherimg').innerHTML+='<img class=imgoverlay src=object' + tdfs + '.svg?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     } else if (!" . $topp . ".document.getElementById('topimg')) {

     if (document.body.innerHTML.indexOf('imgoverlay') == -1) {
     document.getElementById('topimg').style.opacity='0.5';
     //document.getElementById('topimg').src=document.getElementById('topimg').title;
     }
     document.getElementById('otherimg').innerHTML+='<img class=imgoverlay src=object' + tdfs + '.svg?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';


     } else {
     if (" . $topp . ".document.body.innerHTML.indexOf('imgoverlay') == -1) {
     " . $topp . ".document.getElementById('topimg').style.opacity='0.5';
     //" . $topp . ".document.getElementById('topimg').src=" . $topp . ".document.getElementById('topimg').title;
     }
     " . $topp . ".document.getElementById('otherimg').innerHTML+='<img class=imgoverlay src=object' + tdfs + '.svg?huh=' + Math.floor(Math.random() * 1987675) + ' style=\"opacity:0.5;position:absolute;top:0px;left:0px;\"></img>';
     }
     document.getElementById('form_' + tdfs).target='';
     document.getElementById('form_' + tdfs).method='POST';
     }
     }
   }
";

function datauriit($inim) {
global $blight1, $blight2;
if (!file_exists($inim)) {
        return '';
} else if (strpos(strtolower($inim), ".png") !== false) {
        return 'data:image/png;base64,' . base64_encode(file_get_contents($inim));
} else {
        $preh1=file_get_contents($inim);
        if (strpos($preh1, "</polygon>") !== false) {
        $posth1=explode("</polygon>", $preh1);
        } else {
        $posth1=explode("/>", $preh1);
        }
        $xposth1=str_replace('#ffffff', strtoupper($bgrd), $posth1[0]);
        $xposth2=str_replace('stroke: none', 'stroke: NONE', $xposth1);
        $posth=str_replace($posth1[0],$xposth2,$preh1);
        $xhi=str_replace("<svg ","<svg id=topimg ",str_replace('stroke: none', 'stroke: ' . $blight1, str_replace('#ffffff', $blight2, $posth)));
        return 'data:image/svg;base64,' . base64_encode("<svg " . explode("<svg ",explode("</svg>",str_replace('stroke: none', 'stroke: ' . $blight1, str_replace('#ffffff', $blight2, $posth)))[0] . "</svg>")[1]);
}
}

function rgb_via_hex($hexin) {
global $r, $g, $b;
list($r, $g, $b) = sscanf($hexin, "#%02x%02x%02x");
}

$thing="";
$textx="";
$shape="cone";
$metal=null;
if (isset($_GET['shape'])) {
  $shape=strtolower(urldecode($_GET['shape']));
} else if (isset($_POST['shape'])) { 
  $shape=strtolower(urldecode($_POST['shape']));
} else if (isset($_GET['text'])) {
  $textx=str_replace("+"," ",urldecode($_GET['text']));
  $shape="";
} else if (isset($_POST['text'])) { 
  $textx=str_replace("+"," ",urldecode($_POST['text']));
  $shape="";
}
if ($textx == "") {
  $hi="<img id=topimg title='" . datauriit('object' . $shape . $ext) . "' src='object" . $shape . $ext . "?huh=" . rand(0,19876543) . "'></img>";

  if ($shape == "spherocube") {
    $roll=45;
    $pitch=30;
    $yaw=15;
    $xmove=-225;
    $ymove=-100;
    $zmove=0;
  } else if ($shape == "star") {
    $roll=0;
    $pitch=-25;
    $yaw=-15;
  } else if ($shape == "helicoid") {
    $roll=-50;
    $pitch=0;
    $yaw=15;
    $xmove=50;
    $ymove=-220;
    $zmove=0;
    $detail=10;
  } else if ($shape == "torus") {
    $roll=-45;
    $pitch=0;
    $yaw=-30;
    $detail=10;
    $detail2=10;
    $xmove=-0;
    $ymove=-20;
    $zmove=0;
    $radius=90;
    $radiusouter=120;
    $detail=10;
    $detail2=10;
  } else if ($shape == "bundt") {
    $roll=-45;
    $pitch=0;
    $yaw=-15;
    $detail=15;
  } else if ($shape == "cube") {
    $roll=-60;
    $pitch=60;
    $yaw=40;
    $radius=100;
  } else if ($shape == "cone") {
    $roll=-45;
    $pitch=-120;
    $yaw=-10;
    $detail=64;
    $xscale=70;
    $yscale=220;
    $zscale=70;
    $xmove=-50;
    $ymove=-30;
    $zmove=10;
  } else if ($shape == "sphere") {
    $radius=85;
    $detail=5;
  } 

  foreach( $_GET as $name=>$val ) {
    $thing=explode("_", $name)[0];
    if (strpos($name, "_") !== false || strpos($name, "light") !== false) {
      if ($thing == "roll") { 
        $roll=$val;
      } else if ($thing == "pitch") { 
        $pitch=$val;
      } else if ($thing == "yaw") { 
        $yaw=$val;
      } else if ($thing == "detail") { 
        $detail=$val;
      } else if ($thing == "detail2") { 
        $detail2=$val;
      } else if ($thing == "width") { 
        $width=$val;
      } else if ($thing == "height") { 
        $height=$val;
      } else if ($thing == "light") { 
        $light=$val;
      } else if ($thing == "light1x") { 
        $xlight1=$val;
      } else if ($thing == "light1y") { 
        $ylight1=$val;
      } else if ($thing == "light1z") { 
        $zlight1=$val;
      } else if ($thing == "light2x") { 
        $xlight2=$val;
      } else if ($thing == "light2y") { 
        $ylight2=$val;
      } else if ($thing == "light2z") { 
        $zlight2=$val;
      } else if ($thing == "scalex") { 
        $xscale=$val;
      } else if ($thing == "scaley") { 
        $yscale=$val;
      } else if ($thing == "scalez") { 
        $zscale=$val;
      } else if ($thing == "movex") { 
        $xmove=$val;
      } else if ($thing == "movey") { 
        $ymove=$val;
      } else if ($thing == "movez") { 
        $zmove=$val;
      } else if ($thing == "radius") { 
        $radius=$val;
      } else if ($thing == "radiusouter") { 
        $radiusouter=$val;
      } else if ($thing == "light1" && strpos($name, "_") === false) { 
        $alight1=$val;
      } else if ($thing == "light2" && strpos($name, "_") === false) { 
        $alight2=$val;
      } else if ($thing == "bcol") { 
        $bgrd=$val;
        rgb_via_hex($bgrd);
        $bgrdr=$r;
        $bgrdg=$g;
        $bgrdb=$b;
      } else if ($thing == "mcolvalue") { 
        $mval=$val;
      } else if ($thing == "scol") { 
        $scol=$val;
        rgb_via_hex($scol);
        $sgrdr=$r;
        $sgrdg=$g;
        $sgrdb=$b;
      } else if ($thing == "mcol") { 
        $mcol=$val;
        rgb_via_hex($mcol);
        $mgrdr=$r;
        $mgrdg=$g;
        $mgrdb=$b;
      } else if ($thing == "light1" && strpos($name, "_") !== false) { 
        $blight1=$val;
        rgb_via_hex($blight1);
        $blight1r=$r;
        $blight1g=$g;
        $blight1b=$b;
      } else if ($thing == "light2" && strpos($name, "_") !== false) { 
        $blight2=$val;
        rgb_via_hex($blight2);
        $blight2r=$r;
        $blight2g=$g;
        $blight2b=$b;
      }
    } else if ($thing == "perspective") {
      $pspect=$val;
      $isget=true;
      $bsg="";
    } else if ($thing == "driver") {
      $driver=$val;
      $ext=str_replace("gd", ".png", str_replace("svg", ".svg", $driver));
      if ($driver == "gd") {
        $hi="<img id=topimg title='" . datauriit('object' . $shape . $ext) . "' src='object" . $shape . $ext . "?huh=" . rand(0,19876543) . "'></img>";
      } else {
        $preh1=file_get_contents('object' . $shape . $ext);
        if (strpos($preh1, "</polygon>") !== false) {
        $posth1=explode("</polygon>", $preh1);
        } else {
        $posth1=explode("/>", $preh1);
        }
        $xposth1=str_replace('#ffffff', strtoupper($bgrd), $posth1[0]);
        $xposth2=str_replace('stroke: none', 'stroke: NONE', $xposth1);
        $posth=str_replace($posth1[0],$xposth2,$preh1);
        $hi=str_replace("<svg ","<svg id=topimg title='" . datauriit('object' . $shape . $ext) . "' ",str_replace('stroke: none', 'stroke: ' . $blight1, str_replace('#ffffff', $blight2, $posth)));
      }
    } 
  }
  foreach( $_POST as $name=>$val ) {
    $thing=explode("_", $name)[0];
    if (strpos($name, "_") !== false || strpos($name, "light") !== false) {
      if ($thing == "roll") { 
        $roll=$val;
      } else if ($thing == "pitch") { 
        $pitch=$val;
      } else if ($thing == "yaw") { 
        $yaw=$val;
      } else if ($thing == "detail") { 
        $detail=$val;
      } else if ($thing == "detail2") { 
        $detail2=$val;
      } else if ($thing == "width") { 
        $width=$val;
      } else if ($thing == "height") { 
        $height=$val;
      } else if ($thing == "light") { 
        $light=$val;
      } else if ($thing == "radius") { 
        $radius=$val;
      } else if ($thing == "scalex") { 
        $xscale=$val;
      } else if ($thing == "scaley") { 
        $yscale=$val;
      } else if ($thing == "scalez") { 
        $zscale=$val;
      } else if ($thing == "light1x") { 
        $xlight1=$val;
      } else if ($thing == "light1y") { 
        $ylight1=$val;
      } else if ($thing == "light1z") { 
        $zlight1=$val;
      } else if ($thing == "light2x") { 
        $xlight2=$val;
      } else if ($thing == "light2y") { 
        $ylight2=$val;
      } else if ($thing == "light2z") { 
        $zlight2=$val;
      } else if ($thing == "movex") { 
        $xmove=$val;
      } else if ($thing == "movey") { 
        $ymove=$val;
      } else if ($thing == "movez") { 
        $zmove=$val;
      } else if ($thing == "scol") { 
        $scol=$val;
        rgb_via_hex($scol);
        $sgrdr=$r;
        $sgrdg=$g;
        $sgrdb=$b;
      } else if ($thing == "mcolvalue") { 
        $mval=$val;
      } else if ($thing == "mcol") { 
        $mcol=$val;
        rgb_via_hex($mcol);
        $mgrdr=$r;
        $mgrdg=$g;
        $mgrdb=$b;
      } else if ($thing == "bcol") { 
        $bgrd=$val;
        rgb_via_hex($bgrd);
        $bgrdr=$r;
        $bgrdg=$g;
        $bgrdb=$b;
      } else if ($thing == "radiusouter") { 
        $radiusouter=$val;
      } else if ($thing == "light1" && strpos($name, "_") === false) { 
        $alight1=$val;
      } else if ($thing == "light2" && strpos($name, "_") === false) { 
        $alight2=$val;
      } else if ($thing == "light1" && strpos($name, "_") !== false) { 
        $blight1=$val;
        rgb_via_hex($blight1);
        $blight1r=$r;
        $blight1g=$g;
        $blight1b=$b;
      } else if ($thing == "light2" && strpos($name, "_") !== false) { 
        $blight2=$val;
        rgb_via_hex($blight2);
        $blight2r=$r;
        $blight2g=$g;
        $blight2b=$b;
      }
    } else if ($thing == "perspective") {
      $pspect=$val;
      $isget=false;
      $bsg="";
    } else if ($thing == "driver") {
      $driver=$val;
      $ext=str_replace("gd", ".png", str_replace("svg", ".svg", $driver));
      if ($driver == "gd") {
        $hi="<img id=topimg title='" . datauriit('object' . $shape . $ext) . "' src='object" . $shape . $ext . "?huh=" . rand(0,19876543) . "'></img>";
      } else {
        $preh1=file_get_contents('object' . $shape . $ext);
        if (strpos($preh1, "</polygon>") !== false) {
        $posth1=explode("</polygon>", $preh1);
        } else {
        $posth1=explode("/>", $preh1);
        }
        $xposth1=str_replace('#ffffff', strtoupper($bgrd), $posth1[0]);
        $xposth2=str_replace('stroke: none', 'stroke: NONE', $xposth1);
        $posth=str_replace($posth1[0],$xposth2,$preh1);
        $hi=str_replace("<svg ","<svg id=topimg title='" . datauriit('object' . $shape . $ext) . "' ",str_replace('stroke: none', 'stroke: ' . $blight1, str_replace('#ffffff', $blight2, $posth)));
      }
    }
  }
} else {
    $roll=-35;
    $pitch=30;
    $yaw=-15;
    $xmove=-150;
    $ymove=10;
    $zmove=20;
    $xscale=6;
    $yscale=6;
    $zscale=6;
}
$shape=trim($shape);
if (isset($_GET['metal'])) {
  $metal=new Image_3D_Color_Metal($mgrdr,$mgrdg,$mgrdb);
  if (strlen($_GET['metal']) == 0) {
  $metal->setMetal($mval);
  } else {
  $metal->setMetal(strtolower(urldecode($_GET['metal'])));
  }
  $mvalh=$mval;
  $mchecked=" checked";
  $mc=" name=metal";
} else if (isset($_POST['metal'])) { 
  $metal=new Image_3D_Color_Metal($mgrdr,$mgrdg,$mgrdb);
  if (strlen($_POST['metal']) == 0) {
  $metal->setMetal($mval);
  } else {
  $metal->setMetal(strtolower(urldecode($_POST['metal'])));
  }
  $mvalh=$mval;
  $mchecked=" checked";
  $mc=" name=metal";
} else {
  $metal=null;
}

$htmlforms="<!doctype html><html><head><title>Image_3D PHP Package 3D Graphics - RJM Programming - September, 2019 ... thanks to https://pear.php.net/package/Image_3D and https://www6.software.ibm.com/developerworks/education/os-php-3d/os-php-3d-a4.pdf</title><style> td { vertical-align: top;  } </style><scr" . "ipt type='text/javascript'> var bc='', bconto=null, aconto=null, tdfs='" . $shape . "', jext='" . $ext . "'; if (tdfs.length == 0) { tdfs='cone'; } function tdfill(infv,inselid) { tdfs=infv; var rect=document.getElementById(inselid.replace('select_','td_')).getBoundingClientRect(); document.getElementById('td_' + infv).style.width='' +  eval(50 + rect.width) + 'px';  document.getElementById('td_' + infv).style.height='' + eval(50 + rect.height) + 'px'; document.getElementById('td_' + infv).innerHTML='<iframe onload=ciframe(this); frameborder=0 style=margin-top:0px;width:' + eval(40 + rect.width) + 'px;height:' + eval(40 + rect.height) + 'px; src=' + document.URL.split('#')[0].split('?')[0] + '?shape=' + infv + '></iframe>';  document.getElementById('td_' + infv).style.display='table-cell'; } " . $topoverlay . " </s" . "cript></head><body" . $onl . "><table><tr>";
$driversel=str_replace(">" . strtoupper($driver) . "<", " selected>" . strtoupper($driver) . "<", "<select name='driver'><option value='gd'>GD</option><option value='svg'>SVG</option></select>");
$perspectivesel=str_replace(">" . $pspect . "<", " selected>" . $pspect . "<", "<select name='perspective'><option value='Perspectively'>Perspectively</option><option value='Isometric'>Isometric</option></select>");
$lightsel1=str_replace(">" . $alight1 . "<", " selected>" . $alight1 . "<", "<select name='light'><option value='Light'>Light</option><option value='Point'>Point</option><option value='Spotlight'>Spotlight</option><option value='Ambient'>Ambient</option></select>");
$lightsel2=str_replace(">" . $alight2 . "<", " selected>" . $alight2 . "<", "<select name='light'><option value='Light'>Light</option><option value='Point'>Point</option><option value='Spotlight'>Spotlight</option><option value='Ambient'>Ambient</option></select>");
$selstuff="<select name='shape'><option value=''>Please select a 3D shape below ...</option></select>";
for ($ii=0; $ii<sizeof($shapechoices); $ii++) {
$selstuff=str_replace("</select>", "<option value='" . $shapechoices[$ii] . "'>" . strtoupper(substr($shapechoices[$ii],0,1)) . substr($shapechoices[$ii],1) . "</option></select>", $selstuff);
}
for ($ii=0; $ii<sizeof($shapechoices); $ii++) {
$htmlforms.=str_replace($shape . "' style='display:none;'><form ",$shape . "' style='display:BLOCK;" . $bsg . "'>" . $hi . "<br><br><form style='border:3px solid red;padding: 15px 15px 15px 15px;' ","<td id='td_" . $shapechoices[$ii] . "' style='display:none;'><form id='form_" . $shapechoices[$ii] . "' method='POST' action='./cone.php'><br><input id='submit_" . $shapechoices[$ii] . "' style='inline-block;background-color:yellow;' type='submit' value='Display'></input>&nbsp;&nbsp;<input onclick=topoverlay(); style='inline-block;background-color:lightblue;' type='button' value='Overlay'></input>&nbsp;&nbsp;" . str_replace("<select name", "<select onchange=tdfill(this.value,this.id); id='select_" . $shapechoices[$ii] . "' name", str_replace("<option value='" . $shapechoices[$ii] . "'", "<option value='" . $shapechoices[$ii] . "' selected", $selstuff)) . "<br><br>
Width: <input title='px' type='number' step='1' name='width_" . $shapechoices[$ii] . "' id='width_" . $shapechoices[$ii] . "' value='" . $width ."'></input><!--br-->
Height: <input title='px' type='number' step='1' name='height_" . $shapechoices[$ii] . "' id='height_" . $shapechoices[$ii] . "' value='" . $height ."'></input><br><br>
Shape Base Colour: <input type='color' name='scol_" . $shapechoices[$ii] . "' id='scol_" . $shapechoices[$ii] . "' value='" . $scol . "'></input><br>
Background Colour: <input type='color' name='bcol_" . $shapechoices[$ii] . "' id='bcol_" . $shapechoices[$ii] . "' value='" . $bgrd . "'></input><br>
<input onchange=\" document.getElementById(this.id.replace('mcolhuh','mcoldivalue')).innerHTML=document.getElementById(this.id.replace('mcolhuh','mcoldivalue')).innerHTML.replace('<input name=','<INPUT data-name=metal ').replace('<input ','<input name=metal '); document.getElementById(this.id.replace('mcolhuh','mcolvalue')).value=('' + document.getElementById(this.id.replace('mcolhuh','mcolval')).value); this.style.display='none'; \" type='checkbox' name='mcolhuh_" . $shapechoices[$ii] . "' id='mcolhuh_" . $shapechoices[$ii] . "'" . $mchecked . "></input> Metal<div style='display:inline-block;' id='mcoldivalue_" . $shapechoices[$ii] . "'><input" . $mc . " type='hidden' value='" . $mvalh . "' id='mcolvalue_" . $shapechoices[$ii] . "'></input></div> <input onchange=\" document.getElementById(this.id.replace('mcolval','mcolvalue')).value=('' + this.value);  \" onblur=\" document.getElementById(this.id.replace('mcolval','mcolvalue')).value=('' + this.value);  \" type='number' step='0.01' value='" . $mval . "' name='mcolval_" . $shapechoices[$ii] . "' id='mcolval_" . $shapechoices[$ii] . "'></input> Base Colour: <input type='color' name='mcol_" . $shapechoices[$ii] . "' id='mcol_" . $shapechoices[$ii] . "' value='" . $mcol . "'></input><br><br>
" . str_replace("<select name='light'", "<select id='alight1_" . $shapechoices[$ii] . "' name='light1'", $lightsel1) . "1: <input type='color' name='light1_" . $shapechoices[$ii] . "' id='light1_" . $shapechoices[$ii] . "' value='" . $blight1 . "'></input><!--br-->
" . str_replace("<select name='light'", "<select id='alight2_" . $shapechoices[$ii] . "' name='light2'", $lightsel2) . "2: <input type='color' name='light2_" . $shapechoices[$ii] . "' id='light2_" . $shapechoices[$ii] . "' value='" . $blight2 . "'></input><br><br>
Light1 x: <input style='width:80px;' title='px' type='number' step='1' name='light1x_" . $shapechoices[$ii] . "' id='light1x_" . $shapechoices[$ii] . "' value='" . $xlight1 . "'></input><!--br--> 
Light1 y: <input style='width:80px;' title='px' type='number' step='1' name='light1y_" . $shapechoices[$ii] . "' id='light1y_" . $shapechoices[$ii] . "' value='" . $ylight1 . "'></input><!--br-->	
Light1 z: <input style='width:80px;' title='px' type='number' step='1' name='light1z_" . $shapechoices[$ii] . "' id='light1z_" . $shapechoices[$ii] . "' value='" . $zlight1 . "'></input><br>	
Light2 x: <input style='width:80px;' title='px' type='number' step='1' name='light2x_" . $shapechoices[$ii] . "' id='light2x_" . $shapechoices[$ii] . "' value='" . $xlight2 . "'></input><!--br--> 
Light2 y: <input style='width:80px;' title='px' type='number' step='1' name='light2y_" . $shapechoices[$ii] . "' id='light2y_" . $shapechoices[$ii] . "' value='" . $ylight2 . "'></input><!--br-->	
Light2 z: <input style='width:80px;' title='px' type='number' step='1' name='light2z_" . $shapechoices[$ii] . "' id='light2z_" . $shapechoices[$ii] . "' value='" . $zlight2 . "'></input><br><br>	
" . str_replace("<select name='driver'", "<select id='driver_" . $shapechoices[$ii] . "' name='driver'", $driversel) . "<!--br-->
" . str_replace("<select name='perspective'", "<select id='perspective_" . $shapechoices[$ii] . "' name='perspective'", $perspectivesel) . "<br><br>
Tilt Left/Right [gamma or roll]: <input title='degrees' type='number' step='1' min='-360' max='360.0' name='roll_" . $shapechoices[$ii] . "' id='roll_" . $shapechoices[$ii] . "' value='" . $roll . "'></input><br> 
Tilt Front/Back [beta or pitch]: <input title='degrees' type='number' step='1' min='-360' max='360.0' name='pitch_" . $shapechoices[$ii] . "' id='pitch_" . $shapechoices[$ii] . "' value='" . $pitch . "'></input><br>	
Bearing [alpha or yaw]: <input title='degrees' type='number' step='1' min='-360' max='360' name='yaw_" . $shapechoices[$ii] . "' id='yaw_" . $shapechoices[$ii] . "' value='" . $yaw . "'></input><br><br>
Scale x: <input style='width:80px;' type='number' step='1' name='scalex_" . $shapechoices[$ii] . "' id='scalex_" . $shapechoices[$ii] . "' value='" . $xscale . "'></input><!--br--> 
Scale y: <input style='width:80px;' type='number' step='1' name='scaley_" . $shapechoices[$ii] . "' id='scaley_" . $shapechoices[$ii] . "' value='" . $yscale . "'></input><!--br-->	
Scale z: <input style='width:80px;' type='number' step='1' name='scalez_" . $shapechoices[$ii] . "' id='scalez_" . $shapechoices[$ii] . "' value='" . $zscale . "'></input><br><br>	
Move x: <input style='width:80px;' title='px' type='number' step='1' name='movex_" . $shapechoices[$ii] . "' id='movex_" . $shapechoices[$ii] . "' value='" . $xmove . "'></input><!--br--> 
Move y: <input style='width:80px;' title='px' type='number' step='1' name='movey_" . $shapechoices[$ii] . "' id='movey_" . $shapechoices[$ii] . "' value='" . $ymove . "'></input><!--br-->	
Move z: <input style='width:80px;' title='px' type='number' step='1' name='movez_" . $shapechoices[$ii] . "' id='movez_" . $shapechoices[$ii] . "' value='" . $zmove . "'></input><br><br>	
Detail: <input type='number' step='1' min='1' name='detail_" . $shapechoices[$ii] . "' id='detail_" . $shapechoices[$ii] . "' value='" . $detail . "'></input><!--br-->
Detail 2: <input type='number' step='1' min='1' name='detail2_" . $shapechoices[$ii] . "' id='detail2_" . $shapechoices[$ii] . "' value='" . $detail2 . "'></input><br><br>
Radius/Length: <input title='px' type='number' min='1' step='1' name='radius_" . $shapechoices[$ii] . "' id='radius_" . $shapechoices[$ii] . "' value='" . $radius . "'></input><!--br-->
Outer Radius: <input title='px' type='number' min='1' step='1' name='radiusouter_" . $shapechoices[$ii] . "' id='radiusouter_" . $shapechoices[$ii] . "' value='" . $radiusouter . "'></input>
</form></td>");
}
$htmlforms.="</tr></table><iframe onload=diframe(this); id=ifinv name=ifinv style='display:none;' src=''></iframe><div id=otherimg></div></body></html>";

if (!$metal && ($shape == "cone" || $shape == "") && $textx == "") {
$delim="";
if ($arg) {
foreach ($argv as $arg) {
    $e=explode("=",$arg);
    if (count($e) == 2) {
        $textx.=$delim . $e[1];
        
switch (strtolower(trim($textx))) {
    case "cone":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "torus":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "sphere":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "cube":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "bundt":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "helicoid":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "star":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "spherocube":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
}

    } else {   
        $textx.=$delim . $e[0];
    }
    if ($delim == "") {
      $textx="";
    }
    $delim=" ";
}
if ($textx != "") {
    $textx=trim($textx);
    if ($textx != "") { $shape=""; }
}
}
}

$world = new Image_3D();
$world->setColor(new Image_3D_Color($bgrdr,$bgrdg,$bgrdb));
// A blue light from the left
//$light1 = $world->createLight($alight1,array(-300,0,-300));
$light1 = $world->createLight($alight1,array($xlight1,$ylight1,$zlight1));
$light1->setColor(new Image_3D_Color($blight1r,$blight1g,$blight1b));
// A green light from the upper-right
//$light2 = $world->createLight($alight2,array(300,-300,-300));
$light2 = $world->createLight($alight2,array($xlight2,$ylight2,$zlight2));
$light2->setColor(new Image_3D_Color($blight2r,$blight2g,$blight2b));
switch ($shape) {
    case "cone":
//$cone = $world->createObject('cone',array('detail' => 64));
$cone = $world->createObject('cone',array('detail' => $detail));
if ($metal) {
$cone->setColor($metal);
} else { 
$cone->setColor(new Image_3D_Color($sgrdr,$sgrdg,$sgrdb));
}
//$cone->transform($world->createMatrix('scale', array(70, 220, 70)));
$cone->transform($world->createMatrix('scale', array($xscale, $yscale, $zscale)));
//$cone->transform($world->createMatrix('rotation',array(-45, -120, -10)));
$cone->transform($world->createMatrix('rotation',array($roll, $pitch, $yaw)));
//$cone->transform($world->createMatrix('move', array(-50, -30, 10)));
$cone->transform($world->createMatrix('move', array($xmove, $ymove, $zmove)));
        break;
    case "torus":
//$torus = $world->createObject('torus', array('inner_radius' => 90,'outer_radius' => 120,'detail_1' => 10,'detail_2' => 10));
$torus = $world->createObject('torus', array('inner_radius' => $radius,'outer_radius' => $radiusouter,'detail_1' => $detail,'detail_2' => $detail2));
if ($metal) {
$torus->setColor($metal);
} else {
$torus->setColor(new Image_3D_Color($sgrdr,$sgrdg,$sgrdb));
}
//$torus->transform($world->createMatrix('Rotation', array(-45,0,-30)));
$torus->transform($world->createMatrix('Rotation', array($roll,$pitch,$yaw)));
//$torus->transform($world->createMatrix('Move', array(0,-20,0)));
$torus->transform($world->createMatrix('move', array($xmove, $ymove, $zmove)));
        break;
    case "cube":
//$cube = $world->createObject('cube',array(100, 100, 100));
$cube = $world->createObject('cube',array($radius, $radius, $radius));
if ($metal) {
$cube->setColor($metal);
} else {
$cube->setColor(new Image_3D_Color($sgrdr,$sgrdg,$sgrdb));
}
//$cube->transform($world->createMatrix('rotation',array(-60,60,40)));
$cube->transform($world->createMatrix('rotation',array($roll,$pitch,$yaw)));
        break;
        
    case "sphere":
//$sphere = $world->createObject('sphere',array('r' => 85, 'detail' => 5));
$sphere = $world->createObject('sphere',array('r' => $radius, 'detail' => $detail));
if ($metal) {
$sphere->setColor($metal);
} else {
$sphere->setColor(new Image_3D_Color($sgrdr,$sgrdg,$sgrdb));
}
        break;
    
    case "bundt":
$map = $world->createObject('map');
$scale = 130;
//$detail = 15; //35;
$increment = 1 / $detail;
for ($r = 0; $r <= 1; $r += $increment) {
$row = array();
for ($t = 0; $t <= (2 * pi()); $t += $increment) {
$row[] = new Image_3D_Point( $scale * ($r * cos($t)),
$scale * ($r * sin($t)),
$scale * (sin(4 * pi() * $r))
);
}
$map->addRow($row);
}
if ($metal) {
$map->setColor($metal);
} else {
$map->setColor(new Image_3D_Color($sgrdr,$sgrdg,$sgrdb));
}
//$map->transform($world->createMatrix('Rotation', array(-45, 0, -15)));
$map->transform($world->createMatrix('Rotation', array($roll, $pitch, $yaw)));
        break;
        
    case "helicoid":
$map = $world->createObject('map');
$scale = 120;
//$detail = 10; //30;
$levels = 6;
$increment = 1 / $detail;
for ($s = 0; $s <= 1; $s += $increment) {
$row = array();
for ($t = 0; $t <= $levels; $t += $increment) {
$row[] = new Image_3D_Point(
$scale * ($s * cos(2 * pi() * $t)), // x
$scale * ($s * sin(2 * pi() * $t)), // y
$scale * ($t) // z
);
}
$map->addRow($row);
}
if ($metal) {
$map->setColor($metal);
} else {
$map->setColor(new Image_3D_Color($sgrdr,$sgrdg,$sgrdb));
}
//$map->transform($world->createMatrix('Rotation', array(-50, 0, 15)));
$map->transform($world->createMatrix('Rotation', array($roll, $pitch, $yaw)));
//$map->transform($world->createMatrix('Move', array(50, -220, 0)));    
$map->transform($world->createMatrix('move', array($xmove, $ymove, $zmove)));
        break;
        
    case "star":
$polygons = array();
// Front face
$polygons[] = array(
array(0, -120, 0), array(-18, -12, 0),
array(-86, 48, 0), array(0, 18, 0),
array(86, 48, 0), array(18, -12, 0)
);
// Back face
$polygons[] = array(
array(0, -120, 60), array(-18, -12, 60),
array(-86, 48, 60), array(0, 18, 60),
array(86, 48, 60), array(18, -12, 60)
);
// 3 Sides
$polygons[] = array(
array(0, -120, 0), array(-18, -12, 0),
array(-86, 48, 0), array(-86, 48, 60),
array(-18, -12, 60),array(0, -120, 60)
);
$polygons[] = array(
array(-86, 48, 0), array(0, 18, 0),
array(86, 48, 0), array(86, 48, 60),
array(0, 18, 60), array(-86, 48, 60)
);
$polygons[] = array(
array(86, 48, 0), array(18, -12, 0),
array(0, -120, 0), array(0, -120, 60),
array(18, -12, 60),array(86, 48, 60)
);
foreach ($polygons as $poly) {
$points = array();
foreach ($poly as $set) {
$points[] = new Image_3D_Point($set[0], $set[1], $set[2]);
}
$p = $world->createObject('polygon', $points);
if ($metal) {
$p->setColor($metal);
} else {
$p->setColor(new Image_3D_Color($sgrdr,$sgrdg,$sgrdb));
}
}
//$world->transform($world->createMatrix('Rotation',array(0, -25, -15)));
$world->transform($world->createMatrix('Rotation',array($roll, $pitch, $yaw)));
        break;
        
    case "spherocube":
for ($x=0; $x < 5; $x++) {
for ($y=0; $y < 5; $y++) {
for ($z=0; $z < 5; $z++) {
$sphere = $world->createObject('sphere',array('r' => 25, 'detail' => 2));
if ($metal) {
$sphere->setColor($metal);
} else {
$sphere->setColor(new Image_3D_Color($sgrdr,$sgrdg,$sgrdb));
}
$sphere->transform($world->createMatrix('Move',array($x * 75, $y * 75, $z * 75)));
//$sphere->transform($world->createMatrix('Rotation',array(45, 30, 15)));
$sphere->transform($world->createMatrix('Rotation',array($roll, $pitch, $yaw)));
}
}
}
//$world->transform($world->createMatrix('Move', array(-225, -100, 0)));
$world->transform($world->createMatrix('move', array($xmove, $ymove, $zmove)));
        break;
        
    default:
        if ($textx != "") {
        if (file_exists('Image/3D/Paintable/Object/TextData.dat')) {
        file_put_contents('Image/3D/Paintable/Object/TextData.dat', $textx);
        }
$text = $world->createObject('text', $textx);
if ($metal) {
$text->setColor($metal);
} else {
$text->setColor(new Image_3D_Color($sgrdr,$sgrdg,$sgrdb));
}
//$text->transform($world->createMatrix('Scale', array(6, 6, 6)));
$text->transform($world->createMatrix('Scale', array($xscale, $yscale, $zscale)));
//$text->transform($world->createMatrix('Rotation',array(-35, 30, -15)));
$text->transform($world->createMatrix('Rotation',array($roll, $pitch, $yaw)));
//$text->transform($world->createMatrix('Move', array(-150, 10, 20)));        
$text->transform($world->createMatrix('move', array($xmove, $ymove, $zmove)));
		}       
        break;

}
// Render and save the 2-D image
//$world->createRenderer('Perspectively');
$world->createRenderer($pspect);
//$world->createDriver('gd');
$world->createDriver($driver);
$world->render($width, $height, 'object' . $shape . $ext);
//header ('Content-type:image/png');
//echo file_get_contents('object' . $shape . '.png');
if ($isget) {
  echo str_replace(" id=", " style=display:none; id=",$hi); //str_replace("BLOCK","none",$htmlforms);
} else {
  echo $htmlforms;
}
?>
