j0h

keyring.php

j0h
Aug 19th, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.13 KB | None | 0 0
  1. <?php
  2. $submitted = ($_SERVER['REQUEST_METHOD'] === 'POST');
  3. $label     = isset($_POST['label']) ? $_POST['label'] : '';
  4. $svg       = isset($_POST['svg']) ? $_POST['svg'] : ''; // single value now
  5.  
  6. // up to 15 char
  7. $label_ok = (bool)preg_match('/^[\x20-\x7E]{0,15}$/', $label);
  8.  
  9. echo "<!DOCTYPE html>\n<head>\n
  10. <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">
  11. <link rel=\"shortcut icon\" href=\"http://www.hackmaine.org/favicon.ico\">
  12. <style>
  13. div.gallery {
  14.  margin: 0px;
  15.  float: left;
  16.  width: 200px;
  17. }
  18. div.gallery img {
  19.  width: 180px;
  20.  height: 200px;
  21.  display: block;
  22.  border: 1px solid #ccc;
  23. }
  24. div.caption {
  25.  font-size: 12px;
  26.  color: #167c11;
  27.  word-break: break-all;
  28. }
  29. label.thumb {
  30.  display: block;
  31.  cursor: pointer;
  32.  padding: 6px;
  33.  border-radius: 6px;
  34.  transition: box-shadow .15s ease;
  35. }
  36. label.thumb:hover {
  37.  box-shadow: 0 0 0 2px rgba(22,124,17,0.25) inset;
  38. }
  39. input[type=radio] {
  40.  margin-right: 6px;
  41.  transform: scale(1.2);
  42.  vertical-align: middle;
  43. }
  44. .controls {
  45.  clear: both;
  46.  padding-top: 16px;
  47. }
  48. body {
  49.  font-family: Courier, monospace;
  50.  font-size: 20px;
  51.  color:#167c11;
  52.  background-color:#FFFFFF;
  53.  background-position: top center;
  54.  background-repeat: no-repeat;
  55.  background-size: 30%;
  56. }
  57. button, input[type=submit] {
  58.  font-family: inherit;
  59.  font-size: 16px;
  60.  padding: 6px 12px;
  61.  border: 1px solid #167c11;
  62.  border-radius: 6px;
  63.  background: #fff;
  64.  color: #167c11;
  65.  cursor: pointer;
  66. }
  67. input[type=text] {
  68.  font-family: inherit;
  69.  font-size: 18px;
  70.  padding: 6px 8px;
  71.  border: 1px solid #167c11;
  72.  border-radius: 6px;
  73.  width: 260px;
  74. }
  75. .smallnote { font-size: 12px; color: #0f540c; }
  76. </style>
  77. <title>SVG 2 keyring</title>
  78. </head>
  79. <body>";
  80.  
  81. echo "<h2>Select an image</h2>";
  82.  
  83. // Gather SVGs
  84. $arrFiles = glob('./*.svg');
  85.  
  86. // Start form
  87. echo "<form method=\"post\" action=\"\" id=\"svgForm\">\n";
  88.  
  89. // Gallery with radio buttons
  90. foreach ($arrFiles as $x) {
  91.     $href = $x;
  92.     $basename = basename($x);
  93.     $h_href = htmlspecialchars($href, ENT_QUOTES, 'UTF-8');
  94.     $h_name = htmlspecialchars($basename, ENT_QUOTES, 'UTF-8');
  95.  
  96.     echo "<div class=\"gallery\">
  97.            <label class=\"thumb\">
  98.              <input type=\"radio\" name=\"svg\" value=\"{$h_href}\">
  99.              <a href=\"{$h_href}\" target=\"_blank\" title=\"{$h_name}\">
  100.                <img src=\"{$h_href}\" alt=\"{$h_name}\">
  101.              </a>
  102.              <div class=\"caption\">{$h_name}</div>
  103.            </label>
  104.          </div>\n";
  105. }
  106.  
  107. // Directory links
  108. $dirs = array_filter(glob('*'), 'is_dir');
  109. if (!empty($dirs)) {
  110.     echo "<div style=\"clear:both; padding-top:10px;\">";
  111.     foreach ($dirs as $dir) {
  112.         $h_dir = htmlspecialchars($dir, ENT_QUOTES, 'UTF-8');
  113.         echo " <a href=\"{$h_dir}\">{$h_dir}</a> ";
  114.     }
  115.     echo "</div>";
  116. }
  117.  
  118. echo "<div class=\"controls\">
  119.  <label for=\"label\">Add your Text</label><br>
  120.  <input
  121.    type=\"text\"
  122.    id=\"label\"
  123.    name=\"label\"
  124.    maxlength=\"15\"
  125.    pattern=\"[\\x20-\\x7E]{0,15}\"
  126.    value=\"", htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ,"\"
  127.    autocomplete=\"off\"
  128.    >
  129.  <br><h2> Build your keyRing </h2>
  130.  
  131.  <div style=\"margin-top:12px;\">
  132.    <input type=\"submit\" value=\"Submit\">
  133.  </div>
  134. </div>\n";
  135.  
  136. echo "</form>\n";
  137.  
  138. // If submitted, show a simple result block
  139. if ($submitted) {
  140.     echo "<hr>";
  141.     if (!$label_ok) {
  142.         echo "<div>Too macny characters.</div>";
  143.     } else {
  144. if ($submitted && $label_ok) {
  145.     $svg = $_POST['svg'] ?? '';
  146.  
  147.     $myfile = fopen("output.scad", "w") or die("Unable to open output.scad");
  148.  
  149.     // write svg filename first
  150.     if (!empty($svg)) {
  151.         $svg_file = "svg_file=\"" . $svg . "\";\n";
  152.         fwrite($myfile, $svg_file);
  153.     }
  154.  
  155.     // now append main.src content
  156.     if (file_exists("main.src")) {
  157.         $srcHandle = fopen("main.src", "r") or die("Unable to open main.src");
  158.         $src = fread($srcHandle, filesize("main.src"));
  159.         fclose($srcHandle);
  160.         fwrite($myfile, $src);
  161.     }
  162.  
  163.     fclose($myfile);
  164. $openscad = '/usr/bin/openscad';        // path to openscad
  165. $out = "output.stl";
  166. $cmd = $openscad . ' -o ' . escapeshellarg($out) . ' ' . escapeshellarg($myfile) . ' 2>&1';
  167.     $output = [];
  168.     $status = 0;
  169.     exec($cmd, $output, $status);
  170. }
  171.  
  172.             echo "<div>Label: <code>" . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . "</code></div>";
  173.     }
  174.     if (!empty($svg)) {
  175.         $h_svg = htmlspecialchars($svg, ENT_QUOTES, 'UTF-8');
  176.         echo "<div>Selected SVG: <a href=\"{$h_svg}\" target=\"_blank\">{$h_svg}</a></div>";
  177.     } else {
  178.         echo "<div>No SVG selected.</div>";
  179.     }
  180. }
  181.  
  182. /*
  183.  build scad by writing form vars plus source vars to a file
  184.  Then exec openscad output.scad -o ouptut.stl
  185. */
  186.  
  187. /* main.src:: we build the stl based on importing the image as a string, and adding the keyring, source below..
  188.  
  189. W            = 30;      // target width in mm for the SVG
  190. h            = 3;       // base thickness (and ring height)
  191. ring_r       = 10;      // ring outer radius
  192. ring_rs      = 5;       // ring inner radius (hole radius)
  193. line_width   = 0.8;     // outline thickness
  194. edge_height  = 1.0;     // outline extrude height
  195. $fn          = 100;     // how fine the faces are
  196.  
  197. // --- Modules ---
  198. module ring(r=20, rs=15, h=5){
  199.     difference(){
  200.          cylinder(h=h, r=r);
  201.          cylinder(h=h+0.1, r=rs); // +0.1 avoids z-fighting
  202.     }
  203. }
  204.  
  205. module art2d(width){
  206.     // Keep everything centered and normalized to a known width
  207.     resize([width, 0, 0])
  208.         import(svg_file, center=true);
  209. }
  210.  
  211. module edge(width, line_w, top_h){
  212.     // Outline that matches the resized image shape
  213.     translate([0,0,h])
  214.         linear_extrude(height=top_h, convexity=10)
  215.             offset(delta=line_w)
  216.                 art2d(width);
  217. }
  218.  
  219. // --- Build ---
  220. union(){
  221.     // Base body from SVG
  222.     linear_extrude(height=h, convexity=10)
  223.         art2d(W);
  224.     // move ring to edge of image    
  225.     translate([ W/2 + ring_rs, 0, edge_height ])
  226.         ring(r=ring_r, rs=ring_rs, h=h);
  227.     // Matching outline on top
  228.     edge(W, line_width, edge_height);
  229. }
  230.  
  231. */
Advertisement
Add Comment
Please, Sign In to add comment