39292-vm/parse_img.php
2026-03-26 23:22:13 +00:00

40 lines
1.3 KiB
PHP

<?php
$img = imagecreatefrompng('./assets/pasted-20260326-224107-4bd652c0.png');
$w = imagesx($img);
$h = imagesy($img);
echo "Image: ${w}x${h}\n";
$y = $h / 2;
$colors = [];
for ($x = 0; $x < $w; $x++) {
$c = imagecolorat($img, $x, $y);
$rgb = imagecolorsforindex($img, $c);
$color_str = sprintf("%02x%02x%02x", $rgb['red'], $rgb['green'], $rgb['blue']);
if (empty($colors) || $colors[count($colors)-1]['color'] !== $color_str) {
$colors[] = ['x' => $x, 'color' => $color_str];
}
}
// Compact similar colors to find major blocks
$blocks = [];
$threshold = 5;
foreach ($colors as $c) {
if (empty($blocks)) {
$blocks[] = $c;
} else {
$last = $blocks[count($blocks)-1];
if (abs(hexdec(substr($last['color'],0,2)) - hexdec(substr($c['color'],0,2))) > $threshold ||
abs(hexdec(substr($last['color'],2,2)) - hexdec(substr($c['color'],2,2))) > $threshold ||
abs(hexdec(substr($last['color'],4,2)) - hexdec(substr($c['color'],4,2))) > $threshold) {
$blocks[] = $c;
}
}
}
foreach ($blocks as $i => $b) {
$width = ($i < count($blocks)-1 ? $blocks[$i+1]['x'] : $w) - $b['x'];
if ($width > 10) {
echo "x: {$b['x']}, width: {$width}, color: #{$b['color']}\n";
}
}