Passed
Push — master ( 8f5e6a...061d3e )
by Felipe
03:26
created

Driver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 5
1
<?php // content="text/plain; charset=utf-8"
2
//=======================================================================
3
// File:	    MKGRAD.PHP
4
// Description:	Simple tool to create a gradient background
5
// Ver: 	    $Id$
6
//=======================================================================
7
8
// Basic library classes
9
require_once('jpgraph/jpgraph.php');
10
require_once('jpgraph/jpgraph_bar.php');
11
require_once('jpgraph/jpgraph_canvas.php');
12
  
13
14
// Must have a global comparison method for usort()
15
function _cmp($a, $b)
16
{
17
    return strcmp($a, $b);
18
}
19
20
// Generate the input form
21
class Form
22
{
23
    public $iColors;
24
    public $iGradstyles;
25
    public function Form()
26
    {
27
        $rgb           = new RGB();
28
        $this->iColors = array_keys($rgb->rgb_table);
29
        usort($this->iColors, '_cmp');
30
31
        $this->iGradstyles = array(
32
        "Vertical",2,
33
        "Horizontal",1,
34
        "Vertical from middle",3,
35
        "Horizontal from middle",4,
36
        "Horizontal wider middle",6,
37
        "Vertical wider middle",7,
38
        "Rectangle",5 );
39
    }
40
41
    public function Run()
42
    {
43
        echo '<h3>Generate gradient background</h3>';
44
        echo '<form METHOD=POST action=""><table style="border:blue solid 1;">';
45
        echo '<tr><td>Width:<br>'.$this->GenHTMLInput('w', 8, 4, 300).'</td>';
46
        echo "\n";
47
        echo '<td>Height:<br>'.$this->GenHTMLInput('h', 8, 4, 300).'</td></tr>';
48
        echo "\n";
49
        echo '<tr><td>From Color:<br>';
50
        echo $this->GenHTMLSelect('fc', $this->iColors);
51
        echo '</td><td>To Color:<br>';
52
        echo $this->GenHTMLSelect('tc', $this->iColors);
53
        echo '</td></tr>';
54
        echo '<tr><td colspan=2>Gradient style:<br>';
55
        echo $this->GenHTMLSelectCode('s', $this->iGradstyles);
56
        echo '</td></tr>';
57
        echo '<tr><td colspan=2>Filename: (empty to stream)<br>';
58
        echo $this->GenHTMLInput('fn', 55, 100);
59
        echo '</td></tr>';
60
        echo '<tr><td colspan=2 align=right>'.$this->GenHTMLSubmit('submit').'</td></tr>';
61
        echo '</table>';
62
        echo '</form>';
63
    }
64
65
    public function GenHTMLSubmit($name)
66
    {
67
        return '<INPUT TYPE=submit name="ok"  value=" Ok " >';
68
    }
69
70
71
    public function GenHTMLInput($name, $len, $maxlen=100, $val='')
72
    {
73
        return '<INPUT TYPE=TEXT NAME='.$name.' VALUE="'.$val.'" SIZE='.$len.' MAXLENGTH='.$maxlen.'>';
74
    }
75
76
    public function GenHTMLSelect($name, $option, $selected="", $size=0)
77
    {
78
        $txt="<select name=$name";
79
        if ($size > 0) {
80
            $txt .= " size=$size >";
81
        } else {
82
            $txt .= ">";
83
        }
84
        for ($i=0; $i < count($option); $i++) {
85
            if ($selected == $option[$i]) {
86
                $txt=$txt."<option selected value=\"$option[$i]\">$option[$i]</option>\n";
87
            } else {
88
                $txt=$txt."<option value=\"".$option[$i]."\">$option[$i]</option>\n";
89
            }
90
        }
91
        return $txt."</select>\n";
92
    }
93
    
94
    public function GenHTMLSelectCode($name, $option, $selected="", $size=0)
95
    {
96
        $txt="<select name=$name";
97
        if ($size > 0) {
98
            $txt .= " size=$size >";
99
        } else {
100
            $txt .= ">";
101
        }
102
        for ($i=0; $i < count($option); $i += 2) {
103
            if ($selected == $option[($i + 1)]) {
104
                $txt=$txt."<option selected value=".$option[($i + 1)].">$option[$i]</option>\n";
105
            } else {
106
                $txt=$txt."<option value=\"".$option[($i + 1)]."\">$option[$i]</option>\n";
107
            }
108
        }
109
        return $txt."</select>\n";
110
    }
111
}
112
113
// Basic application driver
114
115
class Driver
116
{
117
    public $iGraph;
118
    public $iGrad;
119
    public $iWidth;
120
    public $iHeight;
121
    public $iFromColor;
122
    public $iToColor;
123
    public $iStyle;
124
    public $iForm;
125
126
    public function Driver()
127
    {
128
        $this->iForm = new Form();
129
    }
130
131
    public function GenGradImage()
132
    {
133
        $aWidth     = (int)@$_POST['w'];
134
        $aHeight    = (int)@$_POST['h'];
135
        $aFrom      = @$_POST['fc'];
136
        $aTo        = @$_POST['tc'];
137
        $aStyle     = @$_POST['s'];
138
        $aFileName  = @$_POST['fn'];
139
140
        $this->iWidth     = $aWidth;
141
        $this->iHeight    = $aHeight;
142
        $this->iFromColor = $aFrom;
143
        $this->iToColor   = $aTo;
144
        $this->iStyle     = $aStyle;
145
146
        $this->graph = new CanvasGraph($aWidth, $aHeight);
147
        $this->grad  = new Gradient($this->graph->img);
148
        $this->grad->FilledRectangle(0,0,
149
                     $this->iWidth,$this->iHeight,
150
                     $this->iFromColor,
151
                     $this->iToColor,
152
                     $this->iStyle);
153
154
        if ($aFileName != "") {
155
            $this->graph->Stroke($aFileName);
156
            echo "Image file '$aFileName' created.";
157
        } else {
158
            $this->graph->Stroke();
159
        }
160
    }
161
162
163
    public function Run()
164
    {
165
        global $HTTP_POST_VARS;
166
167
        // Two modes:
168
        // 1) If the script is called with no posted arguments
169
        // we show the input form.
170
        // 2) If we have posted arguments we naivly assume that
171
        // we are called to do the image.
172
173
        if (@$_POST['ok'] === ' Ok ') {
174
            $this->GenGradImage();
175
        } else {
176
            $this->iForm->Run();
177
        }
178
    }
179
}
180
181
$driver = new Driver();
182
$driver->Run();
183