source: trunk/xgraph/jpgraph/Examples/mkgrad.php @ 42

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