Subversion Repositories Sites.obs-saisons.fr

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 aurelien 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 1011 2008-06-23 15:01:51Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. 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
    var $img=NULL;
18
    var $iCnt=0,$iGraphs = array(); // image_handle, x, y, fx, fy, sizex, sizey
19
    var $iFillColor='white', $iCurrentColor=0;
20
    var $lm=0,$rm=0,$tm=0,$bm=0;
21
    var $iDoFrame = FALSE, $iFrameColor = 'black', $iFrameWeight = 1;
22
    var $iLineWeight = 1;
23
    var $expired=false;
24
    var $img_format='png',$image_quality=75;
25
    var $iWidth=NULL,$iHeight=NULL;
26
    var $background_image='',$background_image_center=true,
27
	$backround_image_format='',$background_image_mix=100,
28
	$background_image_y=NULL, $background_image_x=NULL;
29
 
30
 
31
    // Create a new instane of the combined graph
32
    function MGraph($aWidth=NULL,$aHeight=NULL) {
33
	$this->iWidth = $aWidth;
34
	$this->iHeight = $aHeight;
35
    }
36
 
37
    // Specify background fill color for the combined graph
38
    function SetFillColor($aColor) {
39
	$this->iFillColor = $aColor;
40
    }
41
 
42
    // Add a frame around the combined graph
43
    function SetFrame($aFlg,$aColor='black',$aWeight=1) {
44
	$this->iDoFrame = $aFlg;
45
	$this->iFrameColor = $aColor;
46
	$this->iFrameWeight = $aWeight;
47
    }
48
 
49
    // Specify a background image blend
50
    function SetBackgroundImageMix($aMix) {
51
	$this->background_image_mix = $aMix ;
52
    }
53
 
54
    // Specify a background image
55
    function SetBackgroundImage($aFileName,$aCenter_aX=NULL,$aY=NULL) {
56
	// Second argument can be either a boolean value or
57
	// a numeric
58
	$aCenter=TRUE;
59
	$aX=NULL;
60
 
61
	if( is_numeric($aCenter_aX) ) {
62
	    $aX=$aCenter_aX;
63
	}
64
 
65
	// Get extension to determine image type
66
	$e = explode('.',$aFileName);
67
	if( !$e ) {
68
	    JpGraphError::RaiseL(12002,$aFileName);
69
//('Incorrect file name for MGraph::SetBackgroundImage() : '.$aFileName.' Must have a valid image extension (jpg,gif,png) when using autodetection of image type');
70
	}
71
 
72
	$valid_formats = array('png', 'jpg', 'gif');
73
	$aImgFormat = strtolower($e[count($e)-1]);
74
	if ($aImgFormat == 'jpeg')  {
75
	    $aImgFormat = 'jpg';
76
	}
77
	elseif (!in_array($aImgFormat, $valid_formats) )  {
78
	    JpGraphError::RaiseL(12003,$aImgFormat,$aFileName);
79
//('Unknown file extension ($aImgFormat) in MGraph::SetBackgroundImage() for filename: '.$aFileName);
80
	}
81
 
82
	$this->background_image = $aFileName;
83
	$this->background_image_center=$aCenter;
84
	$this->background_image_format=$aImgFormat;
85
	$this->background_image_x = $aX;
86
	$this->background_image_y = $aY;
87
    }
88
 
89
 
90
    // Private helper function for backgound image
91
    function _loadBkgImage($aFile='') {
92
	if( $aFile == '' )
93
	    $aFile = $this->background_image;
94
 
95
	// Remove case sensitivity and setup appropriate function to create image
96
	// Get file extension. This should be the LAST '.' separated part of the filename
97
	$e = explode('.',$aFile);
98
	$ext = strtolower($e[count($e)-1]);
99
	if ($ext == "jpeg")  {
100
	    $ext = "jpg";
101
	}
102
 
103
	if( trim($ext) == '' )
104
	    $ext = 'png';  // Assume PNG if no extension specified
105
 
106
	$supported = imagetypes();
107
	if( ( $ext == 'jpg' && !($supported & IMG_JPG) ) ||
108
	    ( $ext == 'gif' && !($supported & IMG_GIF) ) ||
109
	    ( $ext == 'png' && !($supported & IMG_PNG) ) ) {
110
	    JpGraphError::RaiseL(12004,$aFile);//('The image format of your background image ('.$aFile.') is not supported in your system configuration. ');
111
	}
112
 
113
	if( $ext == "jpg" || $ext == "jpeg") {
114
	    $f = "imagecreatefromjpeg";
115
	    $ext = "jpg";
116
	}
117
	else {
118
	    $f = "imagecreatefrom".$ext;
119
	}
120
 
121
	$img = @$f($aFile);
122
	if( !$img ) {
123
	    JpGraphError::RaiseL(12005,$aFile);
124
//(" Can't read background image: '".$aFile."'");
125
	}
126
	return $img;
127
    }
128
 
129
    function _strokeBackgroundImage() {
130
	if( $this->background_image == '' )
131
	    return;
132
 
133
	$bkgimg = $this->_loadBkgImage();
134
	// Background width & Heoght
135
	$bw = imagesx($bkgimg);
136
	$bh = imagesy($bkgimg);
137
	// Canvas width and height
138
	$cw = imagesx($this->img);
139
	$ch = imagesy($this->img);
140
 
141
	if( $this->background_image_x === NULL || $this->background_image_y === NULL ) {
142
	    if( $this->background_image_center ) {
143
		// Center original image in the plot area
144
		$x = round($cw/2-$bw/2); $y = round($ch/2-$bh/2);
145
	    }
146
	    else {
147
		// Just copy the image from left corner, no resizing
148
		$x=0; $y=0;
149
	    }
150
	}
151
	else {
152
	    $x = $this->background_image_x;
153
	    $y = $this->background_image_y;
154
	}
155
	$this->_imageCp($bkgimg,$x,$y,0,0,$bw,$bh,$this->background_image_mix);
156
    }
157
 
158
    function _imageCp($aSrcImg,$x,$y,$fx,$fy,$w,$h,$mix=100) {
159
	imagecopymerge($this->img,$aSrcImg,$x,$y,$fx,$fy,$w,$h,$mix);
160
    }
161
 
162
    function _imageCreate($aWidth,$aHeight) {
163
	if( $aWidth <= 1 || $aHeight <= 1 ) {
164
	    JpGraphError::RaiseL(12006,$aWidth,$aHeight);
165
//("Illegal sizes specified for width or height when creating an image, (width=$aWidth, height=$aHeight)");
166
	}
167
	$this->img = @imagecreatetruecolor($aWidth, $aHeight);
168
	if( $this->img < 1 ) {
169
	    JpGraphError::RaiseL(12011);
170
// die("<b>JpGraph Error:</b> Can't create truecolor image. Check that you really have GD2 library installed.");
171
	}
172
	ImageAlphaBlending($this->img,true);
173
    }
174
 
175
    function _polygon($p,$closed=FALSE) {
176
	if( $this->iLineWeight==0 ) return;
177
	$n=count($p);
178
	$oldx = $p[0];
179
	$oldy = $p[1];
180
	for( $i=2; $i < $n; $i+=2 ) {
181
	    imageline($this->img,$oldx,$oldy,$p[$i],$p[$i+1],$this->iCurrentColor);
182
	    $oldx = $p[$i];
183
	    $oldy = $p[$i+1];
184
	}
185
	if( $closed ) {
186
	    imageline($this->img,$p[$n*2-2],$p[$n*2-1],$p[0],$p[1],$this->iCurrentColor);
187
	}
188
    }
189
 
190
    function _filledPolygon($pts) {
191
	$n=count($pts);
192
	for($i=0; $i < $n; ++$i)
193
	    $pts[$i] = round($pts[$i]);
194
	imagefilledpolygon($this->img,$pts,count($pts)/2,$this->iCurrentColor);
195
    }
196
 
197
    function _rectangle($xl,$yu,$xr,$yl) {
198
	for($i=0; $i < $this->iLineWeight; ++$i )
199
	    $this->_polygon(array($xl+$i,$yu+$i,$xr-$i,$yu+$i,
200
				  $xr-$i,$yl-$i,$xl+$i,$yl-$i,
201
				  $xl+$i,$yu+$i));
202
    }
203
 
204
    function _filledRectangle($xl,$yu,$xr,$yl) {
205
	$this->_filledPolygon(array($xl,$yu,$xr,$yu,$xr,$yl,$xl,$yl));
206
    }
207
 
208
    function _setColor($aColor) {
209
	$this->iCurrentColor = $this->iRGB->Allocate($aColor);
210
    }
211
 
212
    function AddMix($aGraph,$x=0,$y=0,$mix=100,$fx=0,$fy=0,$w=0,$h=0) {
213
	$this->_gdImgHandle($aGraph->Stroke( _IMG_HANDLER),$x,$y,$fx=0,$fy=0,$w,$h,$mix);
214
   }
215
 
216
   function Add($aGraph,$x=0,$y=0,$fx=0,$fy=0,$w=0,$h=0) {
217
	$this->_gdImgHandle($aGraph->Stroke( _IMG_HANDLER),$x,$y,$fx=0,$fy=0,$w,$h);
218
   }
219
 
220
    function _gdImgHandle($agdCanvas,$x,$y,$fx=0,$fy=0,$w=0,$h=0,$mix=100) {
221
	if( $w == 0 )  $w = @imagesx($agdCanvas);
222
	if( $w === NULL ) {
223
	    JpGraphError::RaiseL(12007);
224
//('Argument to MGraph::Add() is not a valid GD image handle.');
225
	    return;
226
	}
227
	if( $h == 0 )  $h = @imagesy($agdCanvas);
228
	$this->iGraphs[$this->iCnt++] = array($agdCanvas,$x,$y,$fx,$fy,$w,$h,$mix);
229
    }
230
 
231
    function SetMargin($lm,$rm,$tm,$bm) {
232
	$this->lm = $lm;
233
	$this->rm = $rm;
234
	$this->tm = $tm;
235
	$this->bm = $bm;
236
    }
237
 
238
    function SetExpired($aFlg=true) {
239
	$this->expired = $aFlg;
240
    }
241
 
242
    // Generate image header
243
    function Headers() {
244
 
245
	// In case we are running from the command line with the client version of
246
	// PHP we can't send any headers.
247
	$sapi = php_sapi_name();
248
	if( $sapi == 'cli' )
249
	    return;
250
 
251
	if( headers_sent() ) {
252
 
253
	    echo "<table border=1><tr><td><font color=darkred size=4><b>JpGraph Error:</b>
254
HTTP headers have already been sent.</font></td></tr><tr><td><b>Explanation:</b><br>HTTP headers have already been sent back to the browser indicating the data as text before the library got a chance to send it's image HTTP header to this browser. This makes it impossible for the library to send back image data to the browser (since that would be interpretated as text by the browser and show up as junk text).<p>Most likely you have some text in your script before the call to <i>Graph::Stroke()</i>. If this texts gets sent back to the browser the browser will assume that all data is plain text. Look for any text, even spaces and newlines, that might have been sent back to the browser. <p>For example it is a common mistake to leave a blank line before the opening \"<b>&lt;?php</b>\".</td></tr></table>";
255
 
256
	    die();
257
 
258
	}
259
 
260
 
261
	if ($this->expired) {
262
	    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
263
	    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
264
	    header("Cache-Control: no-cache, must-revalidate");
265
	    header("Pragma: no-cache");
266
	}
267
	header("Content-type: image/$this->img_format");
268
    }
269
 
270
    function SetImgFormat($aFormat,$aQuality=75) {
271
	$this->image_quality = $aQuality;
272
	$aFormat = strtolower($aFormat);
273
	$tst = true;
274
	$supported = imagetypes();
275
	if( $aFormat=="auto" ) {
276
	    if( $supported & IMG_PNG )
277
		$this->img_format="png";
278
	    elseif( $supported & IMG_JPG )
279
		$this->img_format="jpeg";
280
	    elseif( $supported & IMG_GIF )
281
		$this->img_format="gif";
282
	    else
283
		JpGraphError::RaiseL(12008);
284
//(" Your PHP (and GD-lib) installation does not appear to support any known graphic formats.".
285
	    return true;
286
	}
287
	else {
288
	    if( $aFormat=="jpeg" || $aFormat=="png" || $aFormat=="gif" ) {
289
		if( $aFormat=="jpeg" && !($supported & IMG_JPG) )
290
		    $tst=false;
291
		elseif( $aFormat=="png" && !($supported & IMG_PNG) )
292
		    $tst=false;
293
		elseif( $aFormat=="gif" && !($supported & IMG_GIF) )
294
		    $tst=false;
295
		else {
296
		    $this->img_format=$aFormat;
297
		    return true;
298
		}
299
	    }
300
	    else
301
		$tst=false;
302
	    if( !$tst )
303
		JpGraphError::RaiseL(12009,$aFormat);
304
//(" Your PHP installation does not support the chosen graphic format: $aFormat");
305
	}
306
    }
307
 
308
    // Stream image to browser or to file
309
    function Stream($aFile="") {
310
	$func="image".$this->img_format;
311
	if( $this->img_format=="jpeg" && $this->image_quality != null ) {
312
	    $res = @$func($this->img,$aFile,$this->image_quality);
313
	}
314
	else {
315
	    if( $aFile != "" ) {
316
		$res = @$func($this->img,$aFile);
317
	    }
318
	    else
319
		$res = @$func($this->img);
320
	}
321
	if( !$res )
322
	    JpGraphError::RaiseL(12010,$aFile);
323
//("Can't create or stream image to file $aFile Check that PHP has enough permission to write a file to the current directory.");
324
    }
325
 
326
    function Stroke($aFileName='') {
327
	// Find out the necessary size for the container image
328
	$w=0; $h=0;
329
	for($i=0; $i < $this->iCnt; ++$i ) {
330
	    $maxw = $this->iGraphs[$i][1]+$this->iGraphs[$i][5];
331
	    $maxh = $this->iGraphs[$i][2]+$this->iGraphs[$i][6];
332
	    $w = max( $w, $maxw );
333
	    $h = max( $h, $maxh );
334
	}
335
	$w += $this->lm+$this->rm;
336
	$h += $this->tm+$this->bm;
337
 
338
	// User specified width,height overrides
339
	if( $this->iWidth !== NULL ) $w = $this->iWidth;
340
	if( $this->iHeight!== NULL ) $h = $this->iHeight;
341
 
342
	$this->_imageCreate($w,$h);
343
	$this->iRGB = new RGB($this->img);
344
 
345
	$this->_setcolor($this->iFillColor);
346
	$this->_filledRectangle(0,0,$w-1,$h-1);
347
 
348
	$this->_strokeBackgroundImage();
349
 
350
	if( $this->iDoFrame ) {
351
	    $this->_setColor($this->iFrameColor);
352
	    $this->iLineWeight=$this->iFrameWeight;
353
	    $this->_rectangle(0,0,$w-1,$h-1);
354
	}
355
 
356
	// Copy all sub graphs to the container
357
	for($i=0; $i < $this->iCnt; ++$i ) {
358
	    $this->_imageCp($this->iGraphs[$i][0],
359
			    $this->iGraphs[$i][1]+$this->lm,$this->iGraphs[$i][2]+$this->tm,
360
			    $this->iGraphs[$i][3],$this->iGraphs[$i][4],
361
			    $this->iGraphs[$i][5],$this->iGraphs[$i][6],
362
			    $this->iGraphs[$i][7]);
363
	}
364
 
365
	// Output image
366
	if( $aFileName == _IMG_HANDLER ) {
367
	    return $this->img;
368
	}
369
	else {
370
	    if( $aFileName != '' ) {
371
		$this->Stream($aFileName);
372
	    }
373
	    else {
374
		$this->Headers();
375
		$this->Stream();
376
	    }
377
	}
378
    }
379
}
380
 
381
?>