Blame view

site/jpgraph/Examples/mkgrad.php 4.28 KB
8ec98c9f   Guillaume   MAJ
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
  <?php // content="text/plain; charset=utf-8"
  //=======================================================================
  // File:	    MKGRAD.PHP
  // Description:	Simple tool to create a gradient background
  // Ver: 	    $Id$
  //=======================================================================
  
  // Basic library classes
  require_once ('jpgraph/jpgraph.php');
  require_once ('jpgraph/jpgraph_bar.php');
  require_once ('jpgraph/jpgraph_canvas.php');
    
  
  // Must have a global comparison method for usort()
  function _cmp($a,$b) {
      return strcmp($a,$b);
  }
  
  // Generate the input form
  class Form {
      var $iColors;
      var $iGradstyles;
      function Form() {
  
  	$rgb = new RGB();
  	$this->iColors = array_keys($rgb->rgb_table);
  	usort($this->iColors,'_cmp');
  
  	$this->iGradstyles = array(
  	    "Vertical",2,
  	    "Horizontal",1,
  	    "Vertical from middle",3,
  	    "Horizontal from middle",4,
  	    "Horizontal wider middle",6,
  	    "Vertical wider middle",7,
  	    "Rectangle",5 );
      }
  
      function Run() {
  
  	echo '<h3>Generate gradient background</h3>';
  	echo '<form METHOD=POST action=""><table style="border:blue solid 1;">';
  	echo '<tr><td>Width:<br>'.$this->GenHTMLInput('w',8,4,300).'</td>';
  	echo "\n";
  	echo '<td>Height:<br>'.$this->GenHTMLInput('h',8,4,300).'</td></tr>';
  	echo "\n";
  	echo '<tr><td>From Color:<br>';
  	echo $this->GenHTMLSelect('fc',$this->iColors);
  	echo '</td><td>To Color:<br>';
  	echo $this->GenHTMLSelect('tc',$this->iColors);
  	echo '</td></tr>';
  	echo '<tr><td colspan=2>Gradient style:<br>';
  	echo $this->GenHTMLSelectCode('s',$this->iGradstyles);
  	echo '</td></tr>';
  	echo '<tr><td colspan=2>Filename: (empty to stream)<br>';
  	echo $this->GenHTMLInput('fn',55,100);
  	echo '</td></tr>';
  	echo '<tr><td colspan=2 align=right>'.$this->GenHTMLSubmit('submit').'</td></tr>';
  	echo '</table>';
  	echo '</form>';
  
      }
  
      function GenHTMLSubmit($name) {
  	return '<INPUT TYPE=submit name="ok"  value=" Ok " >';
      }
  
  
      function GenHTMLInput($name,$len,$maxlen=100,$val='') {
  	return '<INPUT TYPE=TEXT NAME='.$name.' VALUE="'.$val.'" SIZE='.$len.' MAXLENGTH='.$maxlen.'>';
      }
  
      function GenHTMLSelect($name,$option,$selected="",$size=0) {
  	$txt="<select name=$name";
  	if( $size > 0 )
  	    $txt .= " size=$size >";
  	else 
  	    $txt .= ">";
  	for($i=0; $i<count($option); $i++) {
  	    if( $selected==$option[$i] )
  		$txt=$txt."<option selected value=\"$option[$i]\">$option[$i]</option>\n";		
  	    else
  		$txt=$txt."<option value=\"".$option[$i]."\">$option[$i]</option>\n";
  	}
  	return $txt."</select>\n";
      }
      
      function GenHTMLSelectCode($name,$option,$selected="",$size=0) {
  	$txt="<select name=$name";
  	if( $size > 0 )
  	    $txt .= " size=$size >";
  	else 
  	    $txt .= ">";
  	for($i=0; $i<count($option); $i += 2) {
  	    if( $selected==$option[($i+1)] )
  		$txt=$txt."<option selected value=".$option[($i+1)].">$option[$i]</option>\n";		
  	    else
  		$txt=$txt."<option value=\"".$option[($i+1)]."\">$option[$i]</option>\n";
  	}
  	return $txt."</select>\n";
      }
  
  }
  
  // Basic application driver
  
  class Driver {
      var $iGraph, $iGrad;
      var $iWidth,$iHeight;
      var $iFromColor, $iToColor;
      var $iStyle;
      var $iForm;
  
      function Driver() {
  	$this->iForm = new Form();
      }
  
      function GenGradImage() {
  	
  	$aWidth	 = (int)@$_POST['w'];
  	$aHeight = (int)@$_POST['h'];
  	$aFrom   = @$_POST['fc'];
  	$aTo     = @$_POST['tc'];
  	$aStyle  = @$_POST['s'];
  	$aFileName  = @$_POST['fn'];
  
  	$this->iWidth     = $aWidth;
  	$this->iHeight    = $aHeight;
  	$this->iFromColor = $aFrom;
  	$this->iToColor   = $aTo;
  	$this->iStyle     = $aStyle;
  
  	$this->graph = new CanvasGraph($aWidth,$aHeight);
  	$this->grad  = new Gradient($this->graph->img);
  	$this->grad->FilledRectangle(0,0,
  				     $this->iWidth,$this->iHeight,
  				     $this->iFromColor,
  				     $this->iToColor,
  				     $this->iStyle);
  
  	if( $aFileName != "" ) {
  	    $this->graph->Stroke($aFileName);
  	    echo "Image file '$aFileName' created.";
  	}
  	else
  	    $this->graph->Stroke();
      }
  
  
      function Run() {
  	
  	global $HTTP_POST_VARS;
  
  	// Two modes:
  	// 1) If the script is called with no posted arguments
  	// we show the input form.
  	// 2) If we have posted arguments we naivly assume that
  	// we are called to do the image.
  
  	if( @$_POST['ok']===' Ok ' ) { 
  	    $this->GenGradImage();
  	}
  	else
  	    $this->iForm->Run();
      }
  }
  
  $driver = new Driver();				
  $driver->Run();
  
  ?>