/cnc/index.php:0000000000031757
//cnc/index.php:0000000000000001
function open_image ($file) {
$file = str_replace("%20"," ", $file);

# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }

# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }

# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }

# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }

# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }

# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }

# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }

# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }

# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }

return false;
}

function gray_it($imin, $xin, $yin){
$rgb = imagecolorat($imin, $xin, $yin);
$r = (($rgb >> 16) & 0xFF)*.3;
$g = (($rgb >> 8) & 0xFF)*.59;
$b = ($rgb & 0xFF)*.11;
return $r+$g+$b;
}

$image = open_image($_GET['image']);
if ($image === false) {
die ('Unable to open image');
}

$x = 0;
$y = 0;
$z = 2;

$gerber = "G0 X$x Y$y Z$z F60\n";
$gerber .= "G1 Z$z F40\n\n";

$step = .0078;
$max_depth = -0.005;

$width = imagesx($image);
$height = imagesy($image);

$d = 1;
$x_count = $y_count = $z_count = 0;

for ($w = 0; $w < $width; $w++){
if ($d == 0) {$d=1;}else{$d=0;}


for ($h = 0; $h < $height; $h++){
$adj_h = abs($d*($height-1)-$h);

$new_x = $w*$step;
$new_y = $adj_h*$step;
$new_z = number_format(gray_it($image, $w, $adj_h)/255*$max_depth-$max_depth, 5, '.', '');

$x_gerber = $y_gerber = $z_gerber = "";


if ($new_x != $x){
$x = $new_x;
$x_gerber = "X$x";
}

if ($new_y != $y){
$y = $new_y;
$y_gerber = "Y$y";
}

if ($new_z != $z){
$z = $new_z;
$z_gerber = "Z$z";
}



// if ($new_x != $x){
// $x = $new_x;
// $x_gerber = "X$x";
// if ($x_count > 1){
// $prev_x = $x_prev_gerb;
// }
// $x_count = 0;
// }else{
// $x_count++;
// $x_prev_gerb = "X$x";
// }
//
// if ($new_y != $y){
// $y = $new_y;
// $y_gerber = "Y$y";
// if ($y_count > 1){
// $prev_y = $y_prev_gerb;
// }
// $y_count = 0;
// }else{
// $y_count++;
// $y_prev_gerb = "Y$y";
// }
//
// if ($new_z != $z){
// $z = $new_z;
// $z_gerber = "Z$z";
// if ($z_count > 1){
// $prev_z = $z_prev_gerb;
// }
// $z_count = 0;
// }else{
// $z_count++;
// $z_prev_gerb = "Z$x";
// }
// $prev_gerb = $x_prev_gerb.$y_prev_gerb.$z_prev_gerb;
// $gerber .= $prev_gerb;
// if ($prev_gerb != ""){$gerber .= "\n";}
// $x_prev_gerb=$y_prev_gerb=$z_prev_gerb=0;
//
// $pregerb = $x_gerber.$y_gerber.$z_gerber;
// if ($pregerb != ""){$gerber .= "\n";}
}


/* if ($new_x != $x){
$x = $new_x;
$x_gerber = "X$x";
}

if ($new_y != $y){
$prev_y = $y;
$y = $new_y;
$y_gerber = "Y$y";
}

if ($new_z != $z){
if ($y_count > 0){
$gerber .= $prev_y."\n";
}

$z = $new_z;
$z_gerber = "Z$z";

$y_count = 0;
}else{
$y_gerber = "";
$y_count++;
}

$pregerb = $x_gerber.$y_gerber.$z_gerber;
$gerber .= $pregerb;
if ($pregerb != ""){$gerber .= "\n";}
}*/





$gerber .= "\n\n";
}

$gerber .= "G00 Z1\nG00 X0Y0\nM30";
//header('Content-Type: text');
//header('Content-Type: application/force-download');
//header('Content-Length: '.strlen($gerber));
header('Content-type: text/plain');
//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=image.cnc');
echo $gerber;
function open_image ($file) {
$file = str_replace("%20"," ", $file);

# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }

# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }

# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }

# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }

# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }

# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }

# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }

# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }

# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }

return false;
}

function gray_it($imin, $xin, $yin){
$rgb = imagecolorat($imin, $xin, $yin);
$r = (($rgb >> 16) & 0xFF)*.3;
$g = (($rgb >> 8) & 0xFF)*.59;
$b = ($rgb & 0xFF)*.11;
return $r+$g+$b;
}

$image = open_image($_GET['image']);
if ($image === false) {
die ('Unable to open image');
}

$x = 0;
$y = 0;
$z = 2;

// 123456789012
$gerber = "G0 X$x Y$y Z$z F60\n";
$gerber .= "G1 Z$z F40\n\n";

$step = .0078;
$max_depth = -0.04;

$width = imagesx($image);
$height = imagesy($image);

$d = 1;
$y_count = 0;

for ($w = 0; $w < $width; $w++)
{
if ($d == 0)
{
$d=1;
}
else
{
$d=0;
}

for ($h = 0; $h < $height; $h++)
{
$adj_h = abs($d*($height-1)-$h);

$new_x = $w * $step;
$new_y = $adj_h * $step;
$new_z = number_format(gray_it($image, $w, $adj_h)/255*$max_depth-$max_depth, 5, '.', '');

$x_gerber = $y_gerber = $z_gerber = "";

/*
if ($new_x != $x)
{
$x = $new_x;
$x_gerber = "X$x";
}

if ($new_y != $y)
{
$y = $new_y;
$y_gerber = "Y$y";
}

if ($new_z != $z)
{
$z = $new_z;
$z_gerber = "Z$z";
}

$pregerb = $x_gerber.$y_gerber.$z_gerber;
*/
$pregerb = "";

// row changed, update ALL Z,Y,X in that order
if ($new_x != $x)
{
// complete line at current axis
$pregerb = "X$x\n";

$x = $new_x;
$y = $new_y;
$z = $new_z;

$pregerb .= "\nZ$z\nY$y\nX$x\n";
}
else
{
if ($new_z != $z)
{
// update y before z changes
$pregerb = "Y$y\n";

$z = $new_z;
$y = $new_y;

$pregerb = "Z$z\nY$y\n";
}
else
{
// keep record of last y position
$y = $new_y;
}
}

/////

$gerber .= $pregerb;
if ($pregerb != "")
{
$gerber .= "\n";
}
}
$gerber .= "\n\n";
}

$gerber .= "G00 Z1\nG00 X0Y0\nM30";
//header('Content-Type: text');
//header('Content-Type: application/force-download');
//header('Content-Length: '.strlen($gerber));
header('Content-type: text/plain');
//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=image.cnc');
echo $gerber;
function open_image ($file) {
$file = str_replace("%20"," ", $file);

# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }

# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }

# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }

# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }

# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }

# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }

# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }

# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }

# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }

return false;
}

function gray_it($imin, $xin, $yin){
$rgb = imagecolorat($imin, $xin, $yin);
$r = (($rgb >> 16) & 0xFF)*.3;
$g = (($rgb >> 8) & 0xFF)*.59;
$b = ($rgb & 0xFF)*.11;
return $r+$g+$b;
}

$image = open_image($_GET['image']);
if ($image === false) {
die ('Unable to open image');
}

$x = 0;
$y = 0;
$z = 2;

// 123456789012
$gerber = "G0 X$x Y$y Z$z F60\n";
$gerber .= "G1 Z$z F40\n\n";

$step = .0078;
$max_depth = -0.005;

$width = imagesx($image);
$height = imagesy($image);

$d = 1;
$y_count = 0;

// safe travel height
$s = 5.0;

for ($w = 0; $w < $width; $w++)
{
if ($d == 0)
{
$d=1;
}
else
{
$d=0;
}

for ($h = 0; $h < $height; $h++)
{
$adj_h = abs($d*($height-1)-$h);

$new_x = $w * $step;
$new_y = $adj_h * $step;
$new_z = number_format(gray_it($image, $w, $adj_h)/255*$max_depth-$max_depth, 5, '.', '');

$x_gerber = $y_gerber = $z_gerber = "";

/*
if ($new_x != $x)
{
$x = $new_x;
$x_gerber = "X$x";
}

if ($new_y != $y)
{
$y = $new_y;
$y_gerber = "Y$y";
}

if ($new_z != $z)
{
$z = $new_z;
$z_gerber = "Z$z";
}

$pregerb = $x_gerber.$y_gerber.$z_gerber;
*/
$pregerb = "";

// row changed, update ALL Z,Y,X in that order
if ($new_x != $x)
{
// complete line at current axis
$pregerb = "X$x\n";

// move to 'safe travel zone'
$pregreb .= "Z$s\n";

$x = $new_x;
$y = $new_y;
$z = $new_z;

$pregerb .= "\nZ$z\nY$y\nX$x\n";
}
else
{
if ($new_z != $z)
{
// update y before z changes
$pregerb = "Y$y\n";

$z = $new_z;
$y = $new_y;

$pregerb = "Z$z\nY$y\n";
}
else
{
// keep record of last y position
$y = $new_y;
}
}

/////

$gerber .= $pregerb;
if ($pregerb != "")
{
$gerber .= "\n";
}
}
}

$gerber .= "G00 Z1\nG00 X0Y0\nM30";
//header('Content-Type: text');
//header('Content-Type: application/force-download');
//header('Content-Length: '.strlen($gerber));
header('Content-type: text/plain');
//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=image.cnc');
echo $gerber;
function open_image ($file) {
$file = str_replace("%20"," ", $file);

# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }

# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }

# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }

# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }

# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }

# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }

# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }

# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }

# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }

return false;
}

function gray_it($imin, $xin, $yin){
$rgb = imagecolorat($imin, $xin, $yin);
$r = (($rgb >> 16) & 0xFF)*.3;
$g = (($rgb >> 8) & 0xFF)*.59;
$b = ($rgb & 0xFF)*.11;
return $r+$g+$b;
}

$image = open_image($_GET['image']);
if ($image === false) {
die ('Unable to open image');
}

$x = 0;
$y = 0;
$z = 2;

// 123456789012
$gerber = "G0 X$x Y$y Z$z F60\n";
$gerber .= "G1 Z$z F40\n\n";

$step = .0078;
$max_depth = -0.05;

$width = imagesx($image);
$height = imagesy($image);

$d = 1;
$y_count = 0;

// safe travel height
$s = 5.0;

for ($w = 0; $w < $width; $w++)
{
if ($d == 0)
{
$d=1;
}
else
{
$d=0;
}

for ($h = 0; $h < $height; $h++)
{
$adj_h = abs($d*($height-1)-$h);

$new_x = $w * $step;
$new_y = $adj_h * $step;
$new_z = number_format(gray_it($image, $w, $adj_h)/255*$max_depth-$max_depth, 5, '.', '');

$x_gerber = $y_gerber = $z_gerber = "";

/*
if ($new_x != $x)
{
$x = $new_x;
$x_gerber = "X$x";
}

if ($new_y != $y)
{
$y = $new_y;
$y_gerber = "Y$y";
}

if ($new_z != $z)
{
$z = $new_z;
$z_gerber = "Z$z";
}

$pregerb = $x_gerber.$y_gerber.$z_gerber;
*/
$pregerb = "";

// row changed, update ALL Z,Y,X in that order
if ($new_x != $x)
{
// complete line at current axis
$pregerb = "X$x\n";

// move to 'safe travel zone'
$pregreb .= "Z$s\n";

$x = $new_x;
$y = $new_y;
$z = $new_z;

$pregerb .= "\nZ$z\nY$y\nX$x\n";
}
else
{
if ($new_z != $z)
{
// update y before z changes
$pregerb = "Y$y\n";

$z = $new_z;
$y = $new_y;

$pregerb = "Z$z\nY$y\n";
}
else
{
// keep record of last y position
$y = $new_y;
}
}

/////

$gerber .= $pregerb;
if ($pregerb != "")
{
$gerber .= "\n";
}
}
$gerber .= "\n\n";
}

$gerber .= "G00 Z1\nG00 X0Y0\nM30";
//header('Content-Type: text');
//header('Content-Type: application/force-download');
//header('Content-Length: '.strlen($gerber));
header('Content-type: text/plain');
//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=image.cnc');
echo $gerber;
function open_image ($file) {
$file = str_replace("%20"," ", $file);

# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }

# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }

# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }

# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }

# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }

# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }

# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }

# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }

# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }

return false;
}

function gray_it($imin, $xin, $yin){
$rgb = imagecolorat($imin, $xin, $yin);
$r = (($rgb >> 16) & 0xFF)*.3;
$g = (($rgb >> 8) & 0xFF)*.59;
$b = ($rgb & 0xFF)*.11;
return $r+$g+$b;
}

$image = open_image($_GET['image']);
if ($image === false) {
die ('Unable to open image');
}


$tile = "";

$width = imagesx($image);
$height = imagesy($image);

$d = 1;
$x_count = $y_count = $z_count = 0;

for ($h = 0; $h < $height; $h++){
for ($w = 0; $w < $width; $w++){

$rgb = imagecolorat($image, $w, $h);
echo sprintf('%06x',$rgb).",";

}
echo "
\n";
}


echo $tile;
?>