1 | <?php |
---|
2 | /*======================================================================= |
---|
3 | // File: JPGRAPH_MGRAPH.PHP |
---|
4 | // Description: Class to handle multiple graphs in the same image |
---|
5 | // Created: 2006-01-15 |
---|
6 | // Ver: $Id: jpgraph_mgraph.php 1770 2009-08-17 06:10:22Z ljp $ |
---|
7 | // |
---|
8 | // Copyright (c) Asial Corporation. All rights reserved. |
---|
9 | //======================================================================== |
---|
10 | */ |
---|
11 | |
---|
12 | //============================================================================= |
---|
13 | // CLASS MGraph |
---|
14 | // Description: Create a container image that can hold several graph |
---|
15 | //============================================================================= |
---|
16 | class MGraph { |
---|
17 | |
---|
18 | public $title = null, $subtitle = null, $subsubtitle = null; |
---|
19 | |
---|
20 | protected $img=NULL; |
---|
21 | protected $iCnt=0,$iGraphs = array(); // image_handle, x, y, fx, fy, sizex, sizey |
---|
22 | protected $iFillColor='white', $iCurrentColor=0; |
---|
23 | protected $lm=4,$rm=4,$tm=4,$bm=4; |
---|
24 | protected $iDoFrame = FALSE, $iFrameColor = 'black', $iFrameWeight = 1; |
---|
25 | protected $iLineWeight = 1; |
---|
26 | protected $expired=false; |
---|
27 | protected $cache=null,$cache_name = '',$inline=true; |
---|
28 | protected $image_format='png',$image_quality=75; |
---|
29 | protected $iWidth=NULL,$iHeight=NULL; |
---|
30 | protected $background_image='',$background_image_center=true, |
---|
31 | $backround_image_format='',$background_image_mix=100, |
---|
32 | $background_image_y=NULL, $background_image_x=NULL; |
---|
33 | private $doshadow=false, $shadow_width=4, $shadow_color='gray@0.5'; |
---|
34 | public $footer; |
---|
35 | |
---|
36 | |
---|
37 | // Create a new instane of the combined graph |
---|
38 | function __construct($aWidth=NULL,$aHeight=NULL,$aCachedName='',$aTimeOut=0,$aInline=true) { |
---|
39 | $this->iWidth = $aWidth; |
---|
40 | $this->iHeight = $aHeight; |
---|
41 | |
---|
42 | // If the cached version exist just read it directly from the |
---|
43 | // cache, stream it back to browser and exit |
---|
44 | if( $aCachedName!='' && READ_CACHE && $aInline ) { |
---|
45 | $this->cache = new ImgStreamCache(); |
---|
46 | $this->cache->SetTimeOut($aTimeOut); |
---|
47 | $image = new Image(); |
---|
48 | if( $this->cache->GetAndStream($image,$aCachedName) ) { |
---|
49 | exit(); |
---|
50 | } |
---|
51 | } |
---|
52 | $this->inline = $aInline; |
---|
53 | $this->cache_name = $aCachedName; |
---|
54 | |
---|
55 | $this->title = new Text(); |
---|
56 | $this->title->ParagraphAlign('center'); |
---|
57 | $this->title->SetFont(FF_FONT2,FS_BOLD); |
---|
58 | $this->title->SetMargin(3); |
---|
59 | $this->title->SetAlign('center'); |
---|
60 | |
---|
61 | $this->subtitle = new Text(); |
---|
62 | $this->subtitle->ParagraphAlign('center'); |
---|
63 | $this->subtitle->SetFont(FF_FONT1,FS_BOLD); |
---|
64 | $this->subtitle->SetMargin(3); |
---|
65 | $this->subtitle->SetAlign('center'); |
---|
66 | |
---|
67 | $this->subsubtitle = new Text(); |
---|
68 | $this->subsubtitle->ParagraphAlign('center'); |
---|
69 | $this->subsubtitle->SetFont(FF_FONT1,FS_NORMAL); |
---|
70 | $this->subsubtitle->SetMargin(3); |
---|
71 | $this->subsubtitle->SetAlign('center'); |
---|
72 | |
---|
73 | $this->footer = new Footer(); |
---|
74 | |
---|
75 | } |
---|
76 | |
---|
77 | // Specify background fill color for the combined graph |
---|
78 | function SetFillColor($aColor) { |
---|
79 | $this->iFillColor = $aColor; |
---|
80 | } |
---|
81 | |
---|
82 | // Add a frame around the combined graph |
---|
83 | function SetFrame($aFlg,$aColor='black',$aWeight=1) { |
---|
84 | $this->iDoFrame = $aFlg; |
---|
85 | $this->iFrameColor = $aColor; |
---|
86 | $this->iFrameWeight = $aWeight; |
---|
87 | } |
---|
88 | |
---|
89 | // Specify a background image blend |
---|
90 | function SetBackgroundImageMix($aMix) { |
---|
91 | $this->background_image_mix = $aMix ; |
---|
92 | } |
---|
93 | |
---|
94 | // Specify a background image |
---|
95 | function SetBackgroundImage($aFileName,$aCenter_aX=NULL,$aY=NULL) { |
---|
96 | // Second argument can be either a boolean value or |
---|
97 | // a numeric |
---|
98 | $aCenter=TRUE; |
---|
99 | $aX=NULL; |
---|
100 | |
---|
101 | if( is_numeric($aCenter_aX) ) { |
---|
102 | $aX=$aCenter_aX; |
---|
103 | } |
---|
104 | |
---|
105 | // Get extension to determine image type |
---|
106 | $e = explode('.',$aFileName); |
---|
107 | if( !$e ) { |
---|
108 | JpGraphError::RaiseL(12002,$aFileName); |
---|
109 | //('Incorrect file name for MGraph::SetBackgroundImage() : '.$aFileName.' Must have a valid image extension (jpg,gif,png) when using autodetection of image type'); |
---|
110 | } |
---|
111 | |
---|
112 | $valid_formats = array('png', 'jpg', 'gif'); |
---|
113 | $aImgFormat = strtolower($e[count($e)-1]); |
---|
114 | if ($aImgFormat == 'jpeg') { |
---|
115 | $aImgFormat = 'jpg'; |
---|
116 | } |
---|
117 | elseif (!in_array($aImgFormat, $valid_formats) ) { |
---|
118 | JpGraphError::RaiseL(12003,$aImgFormat,$aFileName); |
---|
119 | //('Unknown file extension ($aImgFormat) in MGraph::SetBackgroundImage() for filename: '.$aFileName); |
---|
120 | } |
---|
121 | |
---|
122 | $this->background_image = $aFileName; |
---|
123 | $this->background_image_center=$aCenter; |
---|
124 | $this->background_image_format=$aImgFormat; |
---|
125 | $this->background_image_x = $aX; |
---|
126 | $this->background_image_y = $aY; |
---|
127 | } |
---|
128 | |
---|
129 | function _strokeBackgroundImage() { |
---|
130 | if( $this->background_image == '' ) return; |
---|
131 | |
---|
132 | $bkgimg = Graph::LoadBkgImage('',$this->background_image); |
---|
133 | |
---|
134 | // Background width & Heoght |
---|
135 | $bw = imagesx($bkgimg); |
---|
136 | $bh = imagesy($bkgimg); |
---|
137 | |
---|
138 | // Canvas width and height |
---|
139 | $cw = imagesx($this->img); |
---|
140 | $ch = imagesy($this->img); |
---|
141 | |
---|
142 | if( $this->doshadow ) { |
---|
143 | $cw -= $this->shadow_width; |
---|
144 | $ch -= $this->shadow_width; |
---|
145 | } |
---|
146 | |
---|
147 | if( $this->background_image_x === NULL || $this->background_image_y === NULL ) { |
---|
148 | if( $this->background_image_center ) { |
---|
149 | // Center original image in the plot area |
---|
150 | $x = round($cw/2-$bw/2); $y = round($ch/2-$bh/2); |
---|
151 | } |
---|
152 | else { |
---|
153 | // Just copy the image from left corner, no resizing |
---|
154 | $x=0; $y=0; |
---|
155 | } |
---|
156 | } |
---|
157 | else { |
---|
158 | $x = $this->background_image_x; |
---|
159 | $y = $this->background_image_y; |
---|
160 | } |
---|
161 | imagecopymerge($this->img,$bkgimg,$x,$y,0,0,$bw,$bh,$this->background_image_mix); |
---|
162 | } |
---|
163 | |
---|
164 | function AddMix($aGraph,$x=0,$y=0,$mix=100,$fx=0,$fy=0,$w=0,$h=0) { |
---|
165 | $this->_gdImgHandle($aGraph->Stroke( _IMG_HANDLER),$x,$y,$fx=0,$fy=0,$w,$h,$mix); |
---|
166 | } |
---|
167 | |
---|
168 | function Add($aGraph,$x=0,$y=0,$fx=0,$fy=0,$w=0,$h=0) { |
---|
169 | $this->_gdImgHandle($aGraph->Stroke( _IMG_HANDLER),$x,$y,$fx=0,$fy=0,$w,$h); |
---|
170 | } |
---|
171 | |
---|
172 | function _gdImgHandle($agdCanvas,$x,$y,$fx=0,$fy=0,$w=0,$h=0,$mix=100) { |
---|
173 | if( $w == 0 ) { |
---|
174 | $w = @imagesx($agdCanvas); |
---|
175 | } |
---|
176 | if( $w === NULL ) { |
---|
177 | JpGraphError::RaiseL(12007); |
---|
178 | //('Argument to MGraph::Add() is not a valid GD image handle.'); |
---|
179 | return; |
---|
180 | } |
---|
181 | if( $h == 0 ) { |
---|
182 | $h = @imagesy($agdCanvas); |
---|
183 | } |
---|
184 | $this->iGraphs[$this->iCnt++] = array($agdCanvas,$x,$y,$fx,$fy,$w,$h,$mix); |
---|
185 | } |
---|
186 | |
---|
187 | function SetMargin($lm,$rm,$tm,$bm) { |
---|
188 | $this->lm = $lm; |
---|
189 | $this->rm = $rm; |
---|
190 | $this->tm = $tm; |
---|
191 | $this->bm = $bm; |
---|
192 | } |
---|
193 | |
---|
194 | function SetExpired($aFlg=true) { |
---|
195 | $this->expired = $aFlg; |
---|
196 | } |
---|
197 | |
---|
198 | function SetImgFormat($aFormat,$aQuality=75) { |
---|
199 | $this->image_format = $aFormat; |
---|
200 | $this->image_quality = $aQuality; |
---|
201 | } |
---|
202 | |
---|
203 | // Set the shadow around the whole image |
---|
204 | function SetShadow($aShowShadow=true,$aShadowWidth=4,$aShadowColor='gray@0.3') { |
---|
205 | $this->doshadow = $aShowShadow; |
---|
206 | $this->shadow_color = $aShadowColor; |
---|
207 | $this->shadow_width = $aShadowWidth; |
---|
208 | $this->footer->iBottomMargin += $aShadowWidth; |
---|
209 | $this->footer->iRightMargin += $aShadowWidth; |
---|
210 | } |
---|
211 | |
---|
212 | function StrokeTitle($image,$w,$h) { |
---|
213 | // Stroke title |
---|
214 | if( $this->title->t !== '' ) { |
---|
215 | |
---|
216 | $margin = 3; |
---|
217 | |
---|
218 | $y = $this->title->margin; |
---|
219 | if( $this->title->halign == 'center' ) { |
---|
220 | $this->title->Center(0,$w,$y); |
---|
221 | } |
---|
222 | elseif( $this->title->halign == 'left' ) { |
---|
223 | $this->title->SetPos($this->title->margin+2,$y); |
---|
224 | } |
---|
225 | elseif( $this->title->halign == 'right' ) { |
---|
226 | $indent = 0; |
---|
227 | if( $this->doshadow ) { |
---|
228 | $indent = $this->shadow_width+2; |
---|
229 | } |
---|
230 | $this->title->SetPos($w-$this->title->margin-$indent,$y,'right'); |
---|
231 | } |
---|
232 | $this->title->Stroke($image); |
---|
233 | |
---|
234 | // ... and subtitle |
---|
235 | $y += $this->title->GetTextHeight($image) + $margin + $this->subtitle->margin; |
---|
236 | if( $this->subtitle->halign == 'center' ) { |
---|
237 | $this->subtitle->Center(0,$w,$y); |
---|
238 | } |
---|
239 | elseif( $this->subtitle->halign == 'left' ) { |
---|
240 | $this->subtitle->SetPos($this->subtitle->margin+2,$y); |
---|
241 | } |
---|
242 | elseif( $this->subtitle->halign == 'right' ) { |
---|
243 | $indent = 0; |
---|
244 | if( $this->doshadow ) { |
---|
245 | $indent = $this->shadow_width+2; |
---|
246 | } |
---|
247 | $this->subtitle->SetPos($this->img->width-$this->subtitle->margin-$indent,$y,'right'); |
---|
248 | } |
---|
249 | $this->subtitle->Stroke($image); |
---|
250 | |
---|
251 | // ... and subsubtitle |
---|
252 | $y += $this->subtitle->GetTextHeight($image) + $margin + $this->subsubtitle->margin; |
---|
253 | if( $this->subsubtitle->halign == 'center' ) { |
---|
254 | $this->subsubtitle->Center(0,$w,$y); |
---|
255 | } |
---|
256 | elseif( $this->subsubtitle->halign == 'left' ) { |
---|
257 | $this->subsubtitle->SetPos($this->subsubtitle->margin+2,$y); |
---|
258 | } |
---|
259 | elseif( $this->subsubtitle->halign == 'right' ) { |
---|
260 | $indent = 0; |
---|
261 | if( $this->doshadow ) { |
---|
262 | $indent = $this->shadow_width+2; |
---|
263 | } |
---|
264 | $this->subsubtitle->SetPos($w-$this->subsubtitle->margin-$indent,$y,'right'); |
---|
265 | } |
---|
266 | $this->subsubtitle->Stroke($image); |
---|
267 | |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | function Stroke($aFileName='') { |
---|
272 | // Find out the necessary size for the container image |
---|
273 | $w=0; $h=0; |
---|
274 | for($i=0; $i < $this->iCnt; ++$i ) { |
---|
275 | $maxw = $this->iGraphs[$i][1]+$this->iGraphs[$i][5]; |
---|
276 | $maxh = $this->iGraphs[$i][2]+$this->iGraphs[$i][6]; |
---|
277 | $w = max( $w, $maxw ); |
---|
278 | $h = max( $h, $maxh ); |
---|
279 | } |
---|
280 | $w += $this->lm+$this->rm; |
---|
281 | $h += $this->tm+$this->bm; |
---|
282 | |
---|
283 | // User specified width,height overrides |
---|
284 | if( $this->iWidth !== NULL && $this->iWidth !== 0 ) $w = $this->iWidth; |
---|
285 | if( $this->iHeight!== NULL && $this->iHeight !== 0) $h = $this->iHeight; |
---|
286 | |
---|
287 | if( $this->doshadow ) { |
---|
288 | $w += $this->shadow_width; |
---|
289 | $h += $this->shadow_width; |
---|
290 | } |
---|
291 | |
---|
292 | $image = new Image($w,$h); |
---|
293 | $image->SetImgFormat( $this->image_format,$this->image_quality); |
---|
294 | |
---|
295 | if( $this->doshadow ) { |
---|
296 | $image->SetColor($this->iFrameColor); |
---|
297 | $image->ShadowRectangle(0,0,$w-1,$h-1,$this->iFillColor,$this->shadow_width,$this->shadow_color); |
---|
298 | $w -= $this->shadow_width; |
---|
299 | $h -= $this->shadow_width; |
---|
300 | } |
---|
301 | else { |
---|
302 | $image->SetColor($this->iFillColor); |
---|
303 | $image->FilledRectangle(0,0,$w-1,$h-1); |
---|
304 | } |
---|
305 | $image->SetExpired($this->expired); |
---|
306 | |
---|
307 | $this->img = $image->img; |
---|
308 | $this->_strokeBackgroundImage(); |
---|
309 | |
---|
310 | if( $this->iDoFrame && ! $this->doshadow ) { |
---|
311 | $image->SetColor($this->iFrameColor); |
---|
312 | $image->SetLineWeight($this->iFrameWeight); |
---|
313 | $image->Rectangle(0,0,$w-1,$h-1); |
---|
314 | } |
---|
315 | |
---|
316 | // Copy all sub graphs to the container |
---|
317 | for($i=0; $i < $this->iCnt; ++$i ) { |
---|
318 | $image->CopyMerge($this->iGraphs[$i][0], |
---|
319 | $this->iGraphs[$i][1]+$this->lm,$this->iGraphs[$i][2]+$this->tm, |
---|
320 | $this->iGraphs[$i][3],$this->iGraphs[$i][4], |
---|
321 | $this->iGraphs[$i][5],$this->iGraphs[$i][6], |
---|
322 | -1,-1, /* Full from width and height */ |
---|
323 | $this->iGraphs[$i][7]); |
---|
324 | |
---|
325 | |
---|
326 | } |
---|
327 | |
---|
328 | $this->StrokeTitle($image,$w,$h); |
---|
329 | $this->footer->Stroke($image); |
---|
330 | |
---|
331 | // Output image |
---|
332 | if( $aFileName == _IMG_HANDLER ) { |
---|
333 | return $image->img; |
---|
334 | } |
---|
335 | else { |
---|
336 | //Finally stream the generated picture |
---|
337 | $this->cache = new ImgStreamCache(); |
---|
338 | $this->cache->PutAndStream($image,$this->cache_name,$this->inline,$aFileName); |
---|
339 | } |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | // EOF |
---|
344 | |
---|
345 | ?> |
---|