Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $submitted = ($_SERVER['REQUEST_METHOD'] === 'POST');
- $label = isset($_POST['label']) ? $_POST['label'] : '';
- $svg = isset($_POST['svg']) ? $_POST['svg'] : ''; // single value now
- // up to 15 char
- $label_ok = (bool)preg_match('/^[\x20-\x7E]{0,15}$/', $label);
- echo "<!DOCTYPE html>\n<head>\n
- <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">
- <link rel=\"shortcut icon\" href=\"http://www.hackmaine.org/favicon.ico\">
- <style>
- div.gallery {
- margin: 0px;
- float: left;
- width: 200px;
- }
- div.gallery img {
- width: 180px;
- height: 200px;
- display: block;
- border: 1px solid #ccc;
- }
- div.caption {
- font-size: 12px;
- color: #167c11;
- word-break: break-all;
- }
- label.thumb {
- display: block;
- cursor: pointer;
- padding: 6px;
- border-radius: 6px;
- transition: box-shadow .15s ease;
- }
- label.thumb:hover {
- box-shadow: 0 0 0 2px rgba(22,124,17,0.25) inset;
- }
- input[type=radio] {
- margin-right: 6px;
- transform: scale(1.2);
- vertical-align: middle;
- }
- .controls {
- clear: both;
- padding-top: 16px;
- }
- body {
- font-family: Courier, monospace;
- font-size: 20px;
- color:#167c11;
- background-color:#FFFFFF;
- background-position: top center;
- background-repeat: no-repeat;
- background-size: 30%;
- }
- button, input[type=submit] {
- font-family: inherit;
- font-size: 16px;
- padding: 6px 12px;
- border: 1px solid #167c11;
- border-radius: 6px;
- background: #fff;
- color: #167c11;
- cursor: pointer;
- }
- input[type=text] {
- font-family: inherit;
- font-size: 18px;
- padding: 6px 8px;
- border: 1px solid #167c11;
- border-radius: 6px;
- width: 260px;
- }
- .smallnote { font-size: 12px; color: #0f540c; }
- </style>
- <title>SVG 2 keyring</title>
- </head>
- <body>";
- echo "<h2>Select an image</h2>";
- // Gather SVGs
- $arrFiles = glob('./*.svg');
- // Start form
- echo "<form method=\"post\" action=\"\" id=\"svgForm\">\n";
- // Gallery with radio buttons
- foreach ($arrFiles as $x) {
- $href = $x;
- $basename = basename($x);
- $h_href = htmlspecialchars($href, ENT_QUOTES, 'UTF-8');
- $h_name = htmlspecialchars($basename, ENT_QUOTES, 'UTF-8');
- echo "<div class=\"gallery\">
- <label class=\"thumb\">
- <input type=\"radio\" name=\"svg\" value=\"{$h_href}\">
- <a href=\"{$h_href}\" target=\"_blank\" title=\"{$h_name}\">
- <img src=\"{$h_href}\" alt=\"{$h_name}\">
- </a>
- <div class=\"caption\">{$h_name}</div>
- </label>
- </div>\n";
- }
- // Directory links
- $dirs = array_filter(glob('*'), 'is_dir');
- if (!empty($dirs)) {
- echo "<div style=\"clear:both; padding-top:10px;\">";
- foreach ($dirs as $dir) {
- $h_dir = htmlspecialchars($dir, ENT_QUOTES, 'UTF-8');
- echo " <a href=\"{$h_dir}\">{$h_dir}</a> ";
- }
- echo "</div>";
- }
- echo "<div class=\"controls\">
- <label for=\"label\">Add your Text</label><br>
- <input
- type=\"text\"
- id=\"label\"
- name=\"label\"
- maxlength=\"15\"
- pattern=\"[\\x20-\\x7E]{0,15}\"
- value=\"", htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ,"\"
- autocomplete=\"off\"
- >
- <br><h2> Build your keyRing </h2>
- <div style=\"margin-top:12px;\">
- <input type=\"submit\" value=\"Submit\">
- </div>
- </div>\n";
- echo "</form>\n";
- // If submitted, show a simple result block
- if ($submitted) {
- echo "<hr>";
- if (!$label_ok) {
- echo "<div>Too macny characters.</div>";
- } else {
- if ($submitted && $label_ok) {
- $svg = $_POST['svg'] ?? '';
- $myfile = fopen("output.scad", "w") or die("Unable to open output.scad");
- // write svg filename first
- if (!empty($svg)) {
- $svg_file = "svg_file=\"" . $svg . "\";\n";
- fwrite($myfile, $svg_file);
- }
- // now append main.src content
- if (file_exists("main.src")) {
- $srcHandle = fopen("main.src", "r") or die("Unable to open main.src");
- $src = fread($srcHandle, filesize("main.src"));
- fclose($srcHandle);
- fwrite($myfile, $src);
- }
- fclose($myfile);
- $openscad = '/usr/bin/openscad'; // path to openscad
- $out = "output.stl";
- $cmd = $openscad . ' -o ' . escapeshellarg($out) . ' ' . escapeshellarg($myfile) . ' 2>&1';
- $output = [];
- $status = 0;
- exec($cmd, $output, $status);
- }
- echo "<div>Label: <code>" . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . "</code></div>";
- }
- if (!empty($svg)) {
- $h_svg = htmlspecialchars($svg, ENT_QUOTES, 'UTF-8');
- echo "<div>Selected SVG: <a href=\"{$h_svg}\" target=\"_blank\">{$h_svg}</a></div>";
- } else {
- echo "<div>No SVG selected.</div>";
- }
- }
- /*
- build scad by writing form vars plus source vars to a file
- Then exec openscad output.scad -o ouptut.stl
- */
- /* main.src:: we build the stl based on importing the image as a string, and adding the keyring, source below..
- W = 30; // target width in mm for the SVG
- h = 3; // base thickness (and ring height)
- ring_r = 10; // ring outer radius
- ring_rs = 5; // ring inner radius (hole radius)
- line_width = 0.8; // outline thickness
- edge_height = 1.0; // outline extrude height
- $fn = 100; // how fine the faces are
- // --- Modules ---
- module ring(r=20, rs=15, h=5){
- difference(){
- cylinder(h=h, r=r);
- cylinder(h=h+0.1, r=rs); // +0.1 avoids z-fighting
- }
- }
- module art2d(width){
- // Keep everything centered and normalized to a known width
- resize([width, 0, 0])
- import(svg_file, center=true);
- }
- module edge(width, line_w, top_h){
- // Outline that matches the resized image shape
- translate([0,0,h])
- linear_extrude(height=top_h, convexity=10)
- offset(delta=line_w)
- art2d(width);
- }
- // --- Build ---
- union(){
- // Base body from SVG
- linear_extrude(height=h, convexity=10)
- art2d(W);
- // move ring to edge of image
- translate([ W/2 + ring_rs, 0, edge_height ])
- ring(r=ring_r, rs=ring_rs, h=h);
- // Matching outline on top
- edge(W, line_width, edge_height);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment