1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <?php
$image = imagecreatefrompng("0.png");
$width = imagesx($image);
$height = imagesy($image);
$white = imagecolorallocate($image, 255, 255, 255);
$lightGrey = imagecolorallocate($image, 160, 160, 160);
$red = imagecolorallocate($image, 255, 0, 0);
$green = imagecolorallocate($image, 0, 255, 0);
$command = 'convert 0.png \
-define connected-components:verbose=true \
-connected-components 4 null: | grep "srgba(255,255,255,1)"';
$result = shell_exec($command);
$array = explode(" srgba(255,255,255,1)", $result);
for ($i=0; $i<count($array); $i++) {
preg_match('/([0-9]*).[0-9],([0-9]*).[0-9] ([0-9]*)/', $array[$i], $match);
if ($match[3] < 700 && $match[3] > 50) {
$stack = array(
array($match[1], $match[2])
);
while (count($stack) > 0) {
$current = array_pop($stack);
imagesetpixel($image, $current[0], $current[1], $lightGrey);
$north = array($current[0], $current[1]-1);
$south = array($current[0], $current[1]+1);
$west = array($current[0]-1, $current[1]);
$east = array($current[0]+1, $current[1]);
if ($north[1] >= 0 && imagecolorat($image, $north[0], $north[1]) == $white)
array_push($stack, $north);
if ($south[1] < $height && imagecolorat($image, $south[0], $south[1]) == $white)
array_push($stack, $south);
if ($west[0] >= 0 && imagecolorat($image, $west[0], $west[1]) == $white)
array_push($stack, $west);
if ($east[0] < $width && imagecolorat($image, $east[0], $east[1]) == $white)
array_push($stack, $east);
}
}
}
imagepng($image, 'result.png');
imagedestroy($image);
//echo implode("</br>", $array);;
?> |
Partager