kép.jpg を画像に置き換えます。1回目。近づくように調整できると思います。
2 回目の試行: 床は円形よりもはるかに優れているようです。
3 回目の試行: コードに 2 つのフィルターを追加しました。
<?php
header('content-type: image/png');
//header('content-type: text/plain');
$im = imagecreatefromjpeg ('images/a.jpg');
list($width, $height, $type, $attr) = getimagesize('images/a.jpg');
$im2 = imagecreate($width, $height);
$x = 540;
$y = 353;
$colors = array();
$num = 100;
$num2 = 15 * 11 * $num;
$num3 = 100;
$num4 = 65;
$num5 = 15 * 11 * $num4;
$num6 = 65;
imagecolorallocate($im2, 255, 255, 255);
for ($k = 0; $k < 7; $k ++) {
$ar = 0;
$ag = 0;
$ab = 0;
for ($i = 0; $i < 15; $i ++) {
for ($j = 0; $j < 11; $j ++) {
$tc = imagecolorat($im, $x + $i, $y + $j + $k * 18);
$ar += ($tc >> 16) & 0xFF;
$ag += ($tc >> 8) & 0xFF;
$ab += $tc & 0xFF;
}
}
$c = new STDClass();
if ($k == 0) {
$c->r = floor ($ar / $num5) * $num4;
$c->g = floor ($ag / $num5) * $num4;
$c->b = floor ($ab / $num5) * $num4;
} else {
$c->r = floor ($ar / $num2) * $num;
$c->g = floor ($ag / $num2) * $num;
$c->b = floor ($ab / $num2) * $num;
}
$c->c = imagecolorallocate($im2, $c->r, $c->g, $c->b);
$colors[] = $c;
imagefilledrectangle($im2, 0, $k * 11, 15, $k * 11 + 11, $c->c);
}
$pixelmap = array();
$pm2 = array();
for ($i = 0; $i < $width; $i ++) {
$pixelmap[$i] = array();
$pm2[$i] = array();
for ($j = 0; $j < $height; $j ++) {
$pixelmap[$i][$j] = false;
$pm2[$i][$j] = false;
$c = imagecolorat($im, $i, $j);
$r = ($c >> 16) & 0xFF;
$g = ($c >> 8) & 0xFF;
$b = $c & 0xFF;
$r = floor ($r / $num3) * $num3;
$g = floor ($g / $num3) * $num3;
$b = floor ($b / $num3) * $num3;
$r2 = floor ($r / $num6) * $num6;
$g2 = floor ($g / $num6) * $num6;
$b2 = floor ($b / $num6) * $num6;
for ($k = 0; $k < 7; $k ++) {
$colorOK = true;
if ($k == 0) {
if ($r2 != $colors[$k]->r) { $colorOK = false; }
if ($g2 != $colors[$k]->g) { $colorOK = false; }
if ($b2 != $colors[$k]->b) { $colorOK = false; }
} else {
if ($r != $colors[$k]->r) { $colorOK = false; }
if ($g != $colors[$k]->g) { $colorOK = false; }
if ($b != $colors[$k]->b) { $colorOK = false; }
}
if ($colorOK) { break; }
}
if ($k < 7) { // else no mathing color was found
$pixelmap[$i][$j] = $colors[$k]->c;
//imagesetpixel($im2, $i, $j, $pixelmap[$i][$j]);
}
}
}
for ($i = 1; $i < $width - 1; $i ++) {
for ($j = 1; $j < $height - 1; $j ++) {
if ($pixelmap[$i][$j] !== false) {
if ($pixelmap[$i ][$j - 1] !== false ||
$pixelmap[$i + 1][$j - 1] !== false ||
$pixelmap[$i + 1][$j ] !== false ||
$pixelmap[$i + 1][$j + 1] !== false ||
/*$pixelmap[$i ][$j + 1] !== false ||*/
$pixelmap[$i - 1][$j + 1] !== false ||
$pixelmap[$i - 1][$j ] !== false) {
//imagesetpixel($im2, $i, $j, $pixelmap[$i][$j]);
} else {
$pixelmap[$i][$j] = false;
}
$pm2[$i][$j] = $pixelmap[$i][$j];
} else {
$surroundColors = array();
if (isset($surroundColors[$pixelmap[$i - 1][$j - 1]])) { $surroundColors[$pixelmap[$i - 1][$j - 1]]++; } else { $surroundColors[$pixelmap[$i - 1][$j - 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i ][$j - 1]])) { $surroundColors[$pixelmap[$i ][$j - 1]]++; } else { $surroundColors[$pixelmap[$i ][$j - 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i + 1][$j - 1]])) { $surroundColors[$pixelmap[$i + 1][$j - 1]]++; } else { $surroundColors[$pixelmap[$i + 1][$j - 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i + 1][$j ]])) { $surroundColors[$pixelmap[$i + 1][$j ]]++; } else { $surroundColors[$pixelmap[$i + 1][$j ]] = 1; };
if (isset($surroundColors[$pixelmap[$i + 1][$j + 1]])) { $surroundColors[$pixelmap[$i + 1][$j + 1]]++; } else { $surroundColors[$pixelmap[$i + 1][$j + 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i ][$j + 1]])) { $surroundColors[$pixelmap[$i ][$j + 1]]++; } else { $surroundColors[$pixelmap[$i ][$j + 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i - 1][$j + 1]])) { $surroundColors[$pixelmap[$i - 1][$j + 1]]++; } else { $surroundColors[$pixelmap[$i - 1][$j + 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i - 1][$j ]])) { $surroundColors[$pixelmap[$i - 1][$j ]]++; } else { $surroundColors[$pixelmap[$i - 1][$j ]] = 1; };
$c = array_keys ($surroundColors);
$pm2[$i][$j] = ($c[0] == false ? false : $c[0]);
//imagesetpixel($im2, $i, $j, $c[0]);
}
}
}
$pixelmap = $pm2;
for ($i = 1; $i < $width - 1; $i ++) {
for ($j = 1; $j < $height - 1; $j ++) {
if ($pixelmap[$i][$j] !== false) {
if ($pixelmap[$i ][$j - 1] !== false ||
$pixelmap[$i + 1][$j - 1] !== false ||
$pixelmap[$i + 1][$j ] !== false ||
$pixelmap[$i + 1][$j + 1] !== false ||
/*$pixelmap[$i ][$j + 1] !== false ||*/
$pixelmap[$i - 1][$j + 1] !== false ||
$pixelmap[$i - 1][$j ] !== false) {
imagesetpixel($im2, $i, $j, $pixelmap[$i][$j]);
} else {
$pixelmap[$i][$j] = false;
}
} else {
$surroundColors = array();
if (isset($surroundColors[$pixelmap[$i - 1][$j - 1]])) { $surroundColors[$pixelmap[$i - 1][$j - 1]]++; } else { $surroundColors[$pixelmap[$i - 1][$j - 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i ][$j - 1]])) { $surroundColors[$pixelmap[$i ][$j - 1]]++; } else { $surroundColors[$pixelmap[$i ][$j - 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i + 1][$j - 1]])) { $surroundColors[$pixelmap[$i + 1][$j - 1]]++; } else { $surroundColors[$pixelmap[$i + 1][$j - 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i + 1][$j ]])) { $surroundColors[$pixelmap[$i + 1][$j ]]++; } else { $surroundColors[$pixelmap[$i + 1][$j ]] = 1; };
if (isset($surroundColors[$pixelmap[$i + 1][$j + 1]])) { $surroundColors[$pixelmap[$i + 1][$j + 1]]++; } else { $surroundColors[$pixelmap[$i + 1][$j + 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i ][$j + 1]])) { $surroundColors[$pixelmap[$i ][$j + 1]]++; } else { $surroundColors[$pixelmap[$i ][$j + 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i - 1][$j + 1]])) { $surroundColors[$pixelmap[$i - 1][$j + 1]]++; } else { $surroundColors[$pixelmap[$i - 1][$j + 1]] = 1; };
if (isset($surroundColors[$pixelmap[$i - 1][$j ]])) { $surroundColors[$pixelmap[$i - 1][$j ]]++; } else { $surroundColors[$pixelmap[$i - 1][$j ]] = 1; };
$c = array_keys ($surroundColors);
//$pixelmap[$i][$j] = $c[0];
imagesetpixel($im2, $i, $j, $c[0]);
}
}
}
imagepng ($im2);
?>