Précédent   Forum des professionnels en informatique > PHP > Langage
Langage Forum sur le langage PHP, la POO, les conventions, la sécurité, etc. Avant de poster : FAQ Langage, toutes les FAQ PHP, cours langage et sources PHP
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 28/07/2011, 15h47   #1
Invité de passage
 
Homme Alexandre Louiset
Inscription : juillet 2011
Messages : 7
Détails du profil
Informations personnelles :
Nom : Homme Alexandre Louiset
Localisation : France, Calvados (Basse Normandie)

Informations forums :
Inscription : juillet 2011
Messages : 7
Points : 4
Points : 4
Par défaut [GD] Problème d'affichage de l'image

Voila depuis 2 jours j'ai un problème pour afficher un graphique en fonction de donnée dans une bdd voila mon problème:

je recupére les données d'une table que je met dans un tableau:

La fonction connect()

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
function connect() {
	$hote = 'localhost';
	$name = 'perso';
	$user = 'root';
	$pwd  =  '*********';
 
	try {
		$bdd = new PDO('mysql:host='.$hote.';dbname='.$name, $user, $pwd);
	}
	catch(Exception $e) {
	        echo 'Erreur : '.$e->getMessage().'<br />';
	        echo 'N° : '.$e->getCode();
	}
	return $bdd;
}


La fonction get_all_article()(Static dans la classe article)
Code :
1
2
3
4
5
<?php
static public function get_all() {
	$bdd=connect();
	return $bdd->query("SELECT * FROM articles")->fetchAll(PDO::FETCH_ASSOC);
}
Maintenant le code qui est censé générer l'image (dans un fichier histogramme.php a part du reste)


Code :
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
<?php
$i = 0;
$max = 0;
/*
*je récupère les valeur retourné dans ma requête
*et je détermine le max (pour déterminer la taille final de l'image)
*/
$all_values = Article::get_all();
foreach ($all_values as $value){
	$array_valeur[$i] = (int) $value['I_ARTICLE_STOCK'];
 
        //ici si je fait un echo $array_valeur[$i].'</br>';
        //j'ai bien les valeur dans mon tableau
 
	if ($max < $array_valeur[$i]) {
		$max = $array_valeur[$i];
	}
	$i++;
}
header('Content-Type: image/png');
 
$im = imagecreatetruecolor(600, 600); //normalement déterminer par $i et $max
$bleu = imagecolorallocate($im, 29, 56, 252);
$bleu2 = imagecolorallocate($im, 134, 149, 253);
$gris = imagecolorallocate($im, 193, 193, 193);
 
// ariére plan gris
imagefilledrectangle($im, 0, 0, 599, 599, $gris);
 
 
$echelle = 400/$max; //nombre d'unité par pixel taille du graph en lui même 400px
//initialisation des coordonnées
$x2 = 10;
$y2 = 405; 
$x1 = 5;
 
$i = count($array_valeur);
for ($j = 0; $j < $i; $j++) {
	$y1 =  ceil($y2 - $echelle*$array_valeur[$j]); //taille de la barre
	imagefilledrectangle($im, $x1,$y1,$x2,$y2, $bleu); //affichage 
	$x2 += 10;
	$x1 = $x2 - 5;
        //si je fait:
	//echo 'imagefilledrectangle($im, '.$x1.','.$y1.','.$x2.','.$y2.', $bleu);<br>';
        // le code est bon pas de float a la place de int ect... 
}
 
imagepng($im); //affichage de l'image
imagedestroy($im);
?>
J'appel l'image :

Code :
<img src="display/image_histo.php" />
Et j'obtiens : voir la première image ci-dessous


Maintenant en bidouillant je remplace:


Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$all_values = Article::get_all();
foreach ($all_values as $value){
	$array_valeur[$i] = (int) $value['I_ARTICLE_STOCK'];
 
        //ici si je fait un echo $array_valeur[$i].'</br>';
        //j'ai bien les valeur dans mon tableau
 
	if ($max < $array_valeur[$i]) {
		$max = $array_valeur[$i];
	}
	$i++;
}
?>
Par:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// 
//array(....) généré en fesant un echo dans le foreach précédent 
//puis copié collé pour pas d'erreur :)
//
$all_values = array(639, 164, 1654, 159, 755, 3573, 3830, 203, 272, 103, 0, 46, 2502, 2312, 509, 2829, 1419, 2359, 482, 3461, 106, 3288, 2881, 3455, 974, 2130, 62, 104, 216, 1321, 64, 23, 101, 43, 59, 362, 802, 172, 206, 146, 212, 88, 0, 143, 80, 123, 241, 288, 123, 139, 384);
foreach ($all_values as $value){
	$array_valeur[$i] = $value;
	if ($max < $array_valeur[$i]) {
		$max = $array_valeur[$i];
	}
	$i++;
}
?>
Et j'obtiens bien l'image mais les données ne viennent de la bdd:
(voir i'image en pj)

Avec le code de remplacement j'ajoute juste ce code:

Code :
$var = Articles::get_all();
Avec ça sa marche plus , je pense que ça viens de ma fonction get_all ou connect() comme si il y avait un "echo" dans ces fonctions.


Merci d'avance
Alexandre
Images attachées
Type de fichier : png 329317.png (344 octets, 1 affichages)
Type de fichier : png 329318.png (8,9 Ko, 0 affichages)
bigzzzzz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/07/2011, 18h06   #2
Invité de passage
 
Homme Alexandre Louiset
Inscription : juillet 2011
Messages : 7
Détails du profil
Informations personnelles :
Nom : Homme Alexandre Louiset
Localisation : France, Calvados (Basse Normandie)

Informations forums :
Inscription : juillet 2011
Messages : 7
Points : 4
Points : 4
Re, voila je reviens pour dire que j'ai testé un truc qui fonctionne:

J'ai mit ma fonction connect() dans le fichier image_histo.php (celui qui génére l'image) et j'ai mit aussi ma requête dans le même fichier aussi:

Code :
1
2
 
$all_values = $bdd->query('SELECT I_ARTICLE_STOCK FROM articles')->fetchAll();
Ce qui est pas super compréhensible sachant que en laissant comme je veux faire (CAD appeler la méthode static get_all) et en faisant un var_dump j'ai bien les valeurs de ma requête return par get_all().

Je pense que c'est connect qui fait bug l'image (comme corrompu)
bigzzzzz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 09h22   #3
Rédacteur/Modérateur
 
Avatar de andry.aime
 
Homme Andry Aimé
Inscription : septembre 2007
Messages : 5 129
Détails du profil
Informations personnelles :
Nom : Homme Andry Aimé
Localisation : Ile Maurice

Informations forums :
Inscription : septembre 2007
Messages : 5 129
Points : 7 260
Points : 7 260
Bonjour,

Reprend ton ancien code et commente ces lignes pour afficher l'erreur s'il y a.
Citation:
//header('Content-Type: image/png');

....
//imagepng($im); //affichage de l'image
//imagedestroy($im);
Aucun caractère ne doit être envoyer vers le navigateur et ouvre directement le fichier qui génère l'image mais ne l'appelle pas dans une balise <img>.

A+.
andry.aime est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 10h14   #4
Invité de passage
 
Homme Alexandre Louiset
Inscription : juillet 2011
Messages : 7
Détails du profil
Informations personnelles :
Nom : Homme Alexandre Louiset
Localisation : France, Calvados (Basse Normandie)

Informations forums :
Inscription : juillet 2011
Messages : 7
Points : 4
Points : 4
Merci d'avoir répondu, alors dans mon test.php j'ai fait:

Code :
1
2
3
4
5
6
<!--<img src="../Perso/display/image_histo.php" />-->
<?php 
  include '../src/functions.php';
  require_once  '../class/class_articles.php';
  include 'image_histo.php';
?>
Je vais a l'url test.php et le code source généré est:
Code :
<!--<img src="../Perso/display/image_histo.php" />-->
ensuite j'essais l'url image_histo.php directement (il manque les include de functions.php et de la class donc je les insére au début de image_histo.php) et j'obtient cette fois-ci:
( c'est vide, logique mais moins logique quand on sais que l'image ne se génère pas)
bigzzzzz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 12h14   #5
Rédacteur/Modérateur
 
Avatar de andry.aime
 
Homme Andry Aimé
Inscription : septembre 2007
Messages : 5 129
Détails du profil
Informations personnelles :
Nom : Homme Andry Aimé
Localisation : Ile Maurice

Informations forums :
Inscription : septembre 2007
Messages : 5 129
Points : 7 260
Points : 7 260
ok,
en mettant un var_dump alors pour
Code :
1
2
$all_values = Article::get_all();
var_dump($all_values);
en laissant les 3 lignes commentées.
andry.aime est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 12h37   #6
Membre Expert
 
Inscription : janvier 2007
Messages : 1 452
Détails du profil
Informations personnelles :
Âge : 27
Localisation : France, Paris (Île de France)

Informations forums :
Inscription : janvier 2007
Messages : 1 452
Points : 1 914
Points : 1 914
hello

si ce n'est pas un problème avec la méthode Article::getAll(), vérifie les encodages et les BOMS du fichier contenant cette méthode, et de tous les autres par ailleurs.
On peut avoir des surprises, un BOM par ci par là qui traîne, et hop on a un caractère non désiré qui s'affiche en début de réponse et qu'on comprend pas tout sa vient ce #"$µ££~"é&"'.
Pour vérifier utilise notepad++, il gère bien tous ces détails.

a+
kaymak est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 14h12   #7
Invité de passage
 
Homme Alexandre Louiset
Inscription : juillet 2011
Messages : 7
Détails du profil
Informations personnelles :
Nom : Homme Alexandre Louiset
Localisation : France, Calvados (Basse Normandie)

Informations forums :
Inscription : juillet 2011
Messages : 7
Points : 4
Points : 4
Mes fichier sont déjà en UTF-8 sans BOM d'aprés notepad++ :s

le vardump de all_values en passant par l'url de image_histo.php me donne:

Code :
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
array
  0 => 
    array
      'I_ARTICLE_STOCK' => string '639' (length=3)
      0 => string '639' (length=3)
  1 => 
    array
      'I_ARTICLE_STOCK' => string '164' (length=3)
      0 => string '164' (length=3)
  2 => 
    array
      'I_ARTICLE_STOCK' => string '1654' (length=4)
      0 => string '1654' (length=4)
  3 => 
    array
      'I_ARTICLE_STOCK' => string '159' (length=3)
      0 => string '159' (length=3)
  4 => 
    array
      'I_ARTICLE_STOCK' => string '755' (length=3)
      0 => string '755' (length=3)
  5 => 
    array
      'I_ARTICLE_STOCK' => string '3573' (length=4)
      0 => string '3573' (length=4)
  6 => 
    array
      'I_ARTICLE_STOCK' => string '3830' (length=4)
      0 => string '3830' (length=4)
  7 => 
    array
      'I_ARTICLE_STOCK' => string '203' (length=3)
      0 => string '203' (length=3)
  8 => 
    array
      'I_ARTICLE_STOCK' => string '272' (length=3)
      0 => string '272' (length=3)
  9 => 
    array
      'I_ARTICLE_STOCK' => string '103' (length=3)
      0 => string '103' (length=3)
  10 => 
    array
      'I_ARTICLE_STOCK' => string '0' (length=1)
      0 => string '0' (length=1)
  11 => 
    array
      'I_ARTICLE_STOCK' => string '46' (length=2)
      0 => string '46' (length=2)
  12 => 
    array
      'I_ARTICLE_STOCK' => string '2502' (length=4)
      0 => string '2502' (length=4)
  13 => 
    array
      'I_ARTICLE_STOCK' => string '2312' (length=4)
      0 => string '2312' (length=4)
  14 => 
    array
      'I_ARTICLE_STOCK' => string '509' (length=3)
      0 => string '509' (length=3)
  15 => 
    array
      'I_ARTICLE_STOCK' => string '2829' (length=4)
      0 => string '2829' (length=4)
  16 => 
    array
      'I_ARTICLE_STOCK' => string '1419' (length=4)
      0 => string '1419' (length=4)
  17 => 
    array
      'I_ARTICLE_STOCK' => string '2359' (length=4)
      0 => string '2359' (length=4)
  18 => 
    array
      'I_ARTICLE_STOCK' => string '482' (length=3)
      0 => string '482' (length=3)
  19 => 
    array
      'I_ARTICLE_STOCK' => string '3461' (length=4)
      0 => string '3461' (length=4)
  20 => 
    array
      'I_ARTICLE_STOCK' => string '106' (length=3)
      0 => string '106' (length=3)
  21 => 
    array
      'I_ARTICLE_STOCK' => string '3288' (length=4)
      0 => string '3288' (length=4)
  22 => 
    array
      'I_ARTICLE_STOCK' => string '2881' (length=4)
      0 => string '2881' (length=4)
  23 => 
    array
      'I_ARTICLE_STOCK' => string '3455' (length=4)
      0 => string '3455' (length=4)
  24 => 
    array
      'I_ARTICLE_STOCK' => string '974' (length=3)
      0 => string '974' (length=3)
  25 => 
    array
      'I_ARTICLE_STOCK' => string '2130' (length=4)
      0 => string '2130' (length=4)
  26 => 
    array
      'I_ARTICLE_STOCK' => string '62' (length=2)
      0 => string '62' (length=2)
  27 => 
    array
      'I_ARTICLE_STOCK' => string '104' (length=3)
      0 => string '104' (length=3)
  28 => 
    array
      'I_ARTICLE_STOCK' => string '216' (length=3)
      0 => string '216' (length=3)
  29 => 
    array
      'I_ARTICLE_STOCK' => string '1321' (length=4)
      0 => string '1321' (length=4)
  30 => 
    array
      'I_ARTICLE_STOCK' => string '64' (length=2)
      0 => string '64' (length=2)
  31 => 
    array
      'I_ARTICLE_STOCK' => string '23' (length=2)
      0 => string '23' (length=2)
  32 => 
    array
      'I_ARTICLE_STOCK' => string '101' (length=3)
      0 => string '101' (length=3)
  33 => 
    array
      'I_ARTICLE_STOCK' => string '43' (length=2)
      0 => string '43' (length=2)
  34 => 
    array
      'I_ARTICLE_STOCK' => string '59' (length=2)
      0 => string '59' (length=2)
  35 => 
    array
      'I_ARTICLE_STOCK' => string '362' (length=3)
      0 => string '362' (length=3)
  36 => 
    array
      'I_ARTICLE_STOCK' => string '802' (length=3)
      0 => string '802' (length=3)
  37 => 
    array
      'I_ARTICLE_STOCK' => string '172' (length=3)
      0 => string '172' (length=3)
  38 => 
    array
      'I_ARTICLE_STOCK' => string '206' (length=3)
      0 => string '206' (length=3)
  39 => 
    array
      'I_ARTICLE_STOCK' => string '146' (length=3)
      0 => string '146' (length=3)
  40 => 
    array
      'I_ARTICLE_STOCK' => string '212' (length=3)
      0 => string '212' (length=3)
  41 => 
    array
      'I_ARTICLE_STOCK' => string '88' (length=2)
      0 => string '88' (length=2)
  42 => 
    array
      'I_ARTICLE_STOCK' => string '0' (length=1)
      0 => string '0' (length=1)
  43 => 
    array
      'I_ARTICLE_STOCK' => string '143' (length=3)
      0 => string '143' (length=3)
  44 => 
    array
      'I_ARTICLE_STOCK' => string '80' (length=2)
      0 => string '80' (length=2)
  45 => 
    array
      'I_ARTICLE_STOCK' => string '123' (length=3)
      0 => string '123' (length=3)
  46 => 
    array
      'I_ARTICLE_STOCK' => string '241' (length=3)
      0 => string '241' (length=3)
  47 => 
    array
      'I_ARTICLE_STOCK' => string '288' (length=3)
      0 => string '288' (length=3)
  48 => 
    array
      'I_ARTICLE_STOCK' => string '123' (length=3)
      0 => string '123' (length=3)
  49 => 
    array
      'I_ARTICLE_STOCK' => string '139' (length=3)
      0 => string '139' (length=3)
  50 => 
    array
      'I_ARTICLE_STOCK' => string '384' (length=3)
      0 => string '384' (length=3)
bigzzzzz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 14h31   #8
Rédacteur/Modérateur
 
Avatar de andry.aime
 
Homme Andry Aimé
Inscription : septembre 2007
Messages : 5 129
Détails du profil
Informations personnelles :
Nom : Homme Andry Aimé
Localisation : Ile Maurice

Informations forums :
Inscription : septembre 2007
Messages : 5 129
Points : 7 260
Points : 7 260
Dans ton foreach, $value sera donc un array,
Essaie avec
Code :
1
2
foreach ($all_values as $value){
	$array_valeur[$i] = intval($value[0]);
andry.aime est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 29/07/2011, 14h33   #9
Membre Expert
 
Inscription : janvier 2007
Messages : 1 452
Détails du profil
Informations personnelles :
Âge : 27
Localisation : France, Paris (Île de France)

Informations forums :
Inscription : janvier 2007
Messages : 1 452
Points : 1 914
Points : 1 914
Bah j'avoue que je sèche un peu. Même si j'ai un peu du mal à bien comprendre ton programme et sa structure, je vois pas ce qui cloche.

En dernier recours, utilise le contrôle des tampons de sorties.

http://www.php.net/manual/fr/ref.outcontrol.php
Au début de ton fichier, avant les includes etc, tu mets
ob_start();

Et juste avant le imagepgn tu colles un ob_end_clean();

Comme cela, si il y à des caractères affichés entre ces deux appels, bah se sera effacé, envoyé aux oubliettes, dans le /dev/null etc

Je t'invites à lire la doc

a+
kaymak est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 14h44   #10
Invité de passage
 
Homme Alexandre Louiset
Inscription : juillet 2011
Messages : 7
Détails du profil
Informations personnelles :
Nom : Homme Alexandre Louiset
Localisation : France, Calvados (Basse Normandie)

Informations forums :
Inscription : juillet 2011
Messages : 7
Points : 4
Points : 4
Bravo c'était bien ça!! Merci beaucoup a vous deux.

Par contre le comprend pas pourquoi ça marche avec:
Code :
$array_valeur[$i] = intval($value[0]);
Mais que ça marche pas avec:

Code :
$array_valeur[$i] = (int) $value['I_ARTICLE_STOCK'];
Alors que leurs var_dump respectif sont exactement les même :o

En tout cas merci je vais pouvoir avancer :p

EDIT:

Code :
$array_valeur[$i] = intval($value['I_ARTICLE_STOCK']);
Marche aussi du coup :o
bigzzzzz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 14h50   #11
Rédacteur/Modérateur
 
Avatar de andry.aime
 
Homme Andry Aimé
Inscription : septembre 2007
Messages : 5 129
Détails du profil
Informations personnelles :
Nom : Homme Andry Aimé
Localisation : Ile Maurice

Informations forums :
Inscription : septembre 2007
Messages : 5 129
Points : 7 260
Points : 7 260
Sinon, il y a un tuto sur l'histogramme ici.
andry.aime est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 14h58   #12
Invité de passage
 
Homme Alexandre Louiset
Inscription : juillet 2011
Messages : 7
Détails du profil
Informations personnelles :
Nom : Homme Alexandre Louiset
Localisation : France, Calvados (Basse Normandie)

Informations forums :
Inscription : juillet 2011
Messages : 7
Points : 4
Points : 4
Merci je vais le lire, je vais surement apprendre beaucoup de chose dessus
Merci encore
bigzzzzz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/07/2011, 15h12   #13
Invité de passage
 
Homme Alexandre Louiset
Inscription : juillet 2011
Messages : 7
Détails du profil
Informations personnelles :
Nom : Homme Alexandre Louiset
Localisation : France, Calvados (Basse Normandie)

Informations forums :
Inscription : juillet 2011
Messages : 7
Points : 4
Points : 4
En faite je crois que le problème ne venez pas de la je m'explique:

Connect() et ma classe sont dans d'autre fichier et je n'avais pas d'include dans mon fichier image_histo.php (car je pensai qu'en les incluant dans la page ou j'ai ma balise img cela fonctionné)

Mais j'ai du les mettre pour test l'url image_histo.php en direct sur le navigateur.
Même avec mon ancien code cela marche.

Conclusion: Une génération d'image en php ou on utilise des fonctions situé dans d'autre fichier doivent être ré-inclue dans le fichier php.

J'ai mit a jour pour d'autre qui aurai le même problème

Alex
bigzzzzz est déconnecté   Envoyer un message privé Réponse avec citation 10
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 13h59.


 
 
 
 
Partenaires

Hébergement Web