Bonsoir,

Voici un code qui avec la bilbi GD, ouvre une image N&B (du 400 DPI en A4, soit à peu près 3300/4700 pixels si mes souvenirs sont bons), détecte les formes les plus grandes (+ de 3500 pixels), les colorie en blanc, et sauvegarde le résultat sous une nouvelle image :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
67
68
69
70
71
72
73
74
75
76
<?php
 
	function hashh($arg1, $arg2)
	{
		$sum = $arg1 + $arg2;
    		return $sum * ($sum + 1)/2 + $arg1;
	}
 
	$image = imagecreatefrompng("step3-1.png"); 
  	$white = imagecolorallocate($image, 255, 255, 255);
  	$black = imagecolorallocate($image, 0, 0, 0); 
  	list($width, $height) = getimagesize("step3-1.png"); 
 
	$allPixels = array();
 
        for ($i=1; $i<$width; $i++) {
 
		for ($j=1; $j<$height; $j++) {
 
			if (imagecolorat($image, $i, $j) == 0) // si pixel noir..
			{
				$stack = array(
					array($i, $j)
				);
 
				$localArray = array();
 
				while (count($stack) > 0) { // pour chaque pixel noir voisin...
 
					$current = array_pop($stack);
 
					array_push($localArray, $current);
 
					$allPixels[hashh($current[0],$current[1])] = true;
 
					$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[0] > 0 && $north[0] < $width && $north[1] > 0 && $north[1] < $height)
						if ((imagecolorat($image, $north[0], $north[1]) == 0) && !isset($allPixels[hashh($north[0],$north[1])]))
							array_push($stack, $north);
 
					if ($south[0] > 0 && $south[0] < $width && $south[1] > 0 && $south[1] < $height) 
						if ((imagecolorat($image, $south[0], $south[1]) == 0) && !isset($allPixels[hashh($south[0],$south[1])]))
							array_push($stack, $south);
 
					if ($west[0] > 0 && $west[0] < $width && $west[1] > 0 && $west[1] < $height) 
						if ((imagecolorat($image, $west[0], $west[1]) == 0) && !isset($allPixels[hashh($west[0],$west[1])]))
							array_push($stack, $west);
 
					if ($east[0] > 0 && $east[0] < $width && $east[1] > 0 && $east[1] < $height) 
						if ((imagecolorat($image, $east[0], $east[1]) == 0) && !isset($allPixels[hashh($east[0],$east[1])]))
							 array_push($stack, $east);
 
				}
 
				  if (count($localArray) > 3500) {
 
					$rand = imagecolorallocate($image, 255, 255, 255); 
 
					for ($n=0; $n<count($localArray); $n++) {
 
						imagesetpixel($image, $localArray[$n][0], $localArray[$n][1], $rand);
					}
				}
			}
		}
	}
 
 
	imagepng($image, './result.png');
 
 
?>
Je ne me représente pas bien ce que consomme ce code en terme de mémoire, m'enfin je me dis qu'un VPS de 2 Go de ram peut quand même exécuter ces quelques lignes. Pourtant j'obtiens de manière aléatoire une erreur mémoire :

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 83886080 bytes) in bla bla bla
Le problème, c'est que c'est aléatoire Y a-t-il des fuites de mémoire dans ce code, et si oui, où ? Si non, comment se passer de ces erreurs intempestives ?

Merci