canvas_printer/convert.php
2023-05-20 00:00:14 -04:00

69 lines
No EOL
2.2 KiB
PHP

<?php
$file = fopen("inst.txt","w");
$im = imagecreatefrompng($argv[1]);
$offx = -round(imagesx($im)/2);
$offy = -round(imagesy($im)/2);
$pixelcount = imagesx($im) * imagesy($im);
echo $pixelcount."\n";
$pixelprogress = 0;
#$im = imagescale($im,64,64);
#imagefilter($im,IMG_FILTER_GRAYSCALE);
$lastcol;
fwrite($file,"wait\n40\n");
fwrite($file,"say\nhello\n");
fwrite($file,"move\n0\n5\n0\n");
$colors = array();
#get colours
for ($y=0; $y < imagesy($im); $y++) {
for ($x=0; $x < imagesx($im); $x++) {
$col = imagecolorat($im,$x,$y);
$r = round((($col >> 16) & 0xFF) / 32);
$g = round((($col >> 8) & 0xFF) / 32);
$b = round(($col & 0xFF) / 32);
$id = $r.$g.$b;
//echo $id."\n";
if(!in_array($id,$colors)){
$colors[] = $id;
}
}
}
$pixels_per_wait = 200;
$ppw_count = 0;
foreach ($colors as $col) {
$r = $col[0];
$g = $col[1];
$b = $col[2];
//fwrite($file,"wait\n10\n");
fwrite($file,"inv_fields\n\nreturn {s_blue=\"CHG:$b\",s_green=\"CHG:$g\",s_red=\"CHG:$r\"}\n");
for ($y=0; $y < imagesy($im); $y++) {
for ($x=0; $x < imagesx($im); $x++) {
$val = imagecolorat($im,$x,$y);
$r = round((($val >> 16) & 0xFF) / 32);
$g = round((($val >> 8) & 0xFF) / 32);
$b = round(($val & 0xFF) / 32);
$id = $r.$g.$b;
$x2 = $x + $offx;
$y2 = $y + $offy;
if($id == $col){
fwrite($file,"draw\n0\n$x2\n$y2\n");
$pixelprogress++;
if($pixels_per_wait>0){
$ppw_count++;
if($ppw_count == $pixels_per_wait){
$ppw_count = 0;
echo "\rconversion progess ".round(($pixelprogress/$pixelcount)*100)."%";
fwrite($file,"print\nprinting progess ".round(($pixelprogress/$pixelcount)*100)."%\n");
fwrite($file,"wait\n1\n");
}
}
}
}
}
}
fwrite($file,"wait\n10\n");
fwrite($file,"say\nhttps://bowels-of-the.macrofox.org/git/mtpt/canvas_printer\n");
fwrite($file,"wait\n2\n");
fwrite($file,"exit");