ultimix
graph_core.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
27 
70  function filledrectangle( $Image , $x1 , $y1 , $x2 , $y2 , $color , $thick = 1 )
71  {
72  $t = $thick / 2 - 0.5;
73 
74  return(
75  imagefilledrectangle(
76  $Image , round( min( $x1 , $x2 ) - $t ) , round( min( $y1 , $y2 ) - $t ) ,
77  round( max( $x1 , $x2 ) + $t ) , round( max( $y1 , $y2 ) + $t ) , $color
78  )
79  );
80  }
81 
124  private function commonimagelinethick( $Image , $x1 , $y1 , $x2 , $y2 , $Color , $Thick = 1 )
125  {
126  $t = $Thick / 2 - 0.5;
127  $k = ( $y2 - $y1 ) / ( $x2 - $x1 );
128  $a = $t / sqrt( 1 + pow( $k , 2 ) );
129  $points = array(
130  round( $x1 - ( 1 + $k ) * $a ) , round( $y1 + ( 1 - $k ) * $a ) , round( $x1 - ( 1 - $k ) * $a ) ,
131  round( $y1 - ( 1 + $k ) * $a ) , round( $x2 + ( 1 + $k ) * $a ) , round( $y2 - ( 1 - $k ) * $a ) ,
132  round( $x2 + ( 1 - $k ) * $a ) , round( $y2 + ( 1 + $k ) * $a ) ,
133  );
134 
135  imagefilledpolygon( $Image , $points , 4 , $Color );
136  return( imagepolygon( $Image , $points , 4 , $Color ) );
137  }
138 
181  function imagelinethick( $Image , $x1 , $y1 , $x2 , $y2 , $color , $thick = 1 )
182  {
183  if( $thick == 1 )
184  {
185  return( imageline( $Image , $x1 , $y1 , $x2 , $y2 , $color ) );
186  }
187  elseif( $x1 == $x2 || $y1 == $y2 )
188  {
189  return( $this->filledrectangle( $Image , $x1 , $y1 , $x2 , $y2 , $color , $thick ) );
190  }
191 
192  return( $this->commonimagelinethick( $Image , $x1 , $y1 , $x2 , $y2 , $color , $thick ) );
193  }
194 
221  function get_color_from_hex( $Image , $Color )
222  {
223  $Color = intval( $Color );
224 
225  $ColorObject = imagecolorallocate(
226  $Image , ( $Color & ( 255 << 16 ) ) >> 16 ,
227  ( $Color & ( 255 << 8 ) ) >> 8 , $Color & 255
228  );
229 
230  return( $ColorObject );
231  }
232 
255  function fill_background( &$Img , $BackgroundColor )
256  {
257  try
258  {
259  $BackgroundColor = intval( $BackgroundColor );
260 
261  $BackgroundColor = $this->get_color_from_hex( $Img , $BackgroundColor );
262 
263  $Res = imagefill( $Img , 0 , 0 , $BackgroundColor );
264 
265  if( $Res === false )
266  {
267  throw( new Exception( 'An error occured while setting graph background' ) );
268  }
269  }
270  catch( Exception $e )
271  {
272  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
273  }
274  }
275 
306  function string_width( $FontFile , $Size , $String )
307  {
308  try
309  {
310  $Box = imagettfbbox( $Size , 0 , $FontFile , $String );
311 
312  return( $Box[ 2 ] - $Box[ 0 ] );
313  }
314  catch( Exception $e )
315  {
316  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
317  }
318  }
319 
350  function string_height( $FontFile , $Size , $String )
351  {
352  try
353  {
354  $Box = imagettfbbox( $Size , 0 , $FontFile , $String );
355 
356  return( abs( $Box[ 5 ] - $Box[ 1 ] ) );
357  }
358  catch( Exception $e )
359  {
360  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
361  }
362  }
363 
394  function string_width_height( $FontFile , $Size , $String )
395  {
396  try
397  {
398  $Box = imagettfbbox( $Size , 0 , $FontFile , $String );
399 
400  return( array( $Box[ 2 ] - $Box[ 0 ] , abs( $Box[ 5 ] - $Box[ 1 ] ) ) );
401  }
402  catch( Exception $e )
403  {
404  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
405  }
406  }
407 
434  function compile_labels_from_data( $DataY , $StepsY )
435  {
436  try
437  {
438  $Labels = array();
439 
440  $MaxY = max( $DataY );
441  $MinY = min( $DataY );
442  $dY = ( $MaxY - $MinY ) / $StepsY;
443 
444  for( $i = 0 ; $i <= $StepsY ; $i++ )
445  {
446  $Labels [] = sprintf( '%.2f' , $MinY + $i * $dY );
447  }
448 
449  return( $Labels );
450  }
451  catch( Exception $e )
452  {
453  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
454  }
455  }
456 
479  function output( &$Options , &$Img )
480  {
481  try
482  {
483  header( 'Content-type: image/png' );
484  imagepng( $Img );
485  imagedestroy( $Img );
486  exit( 0 );
487  }
488  catch( Exception $e )
489  {
490  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
491  }
492  }
493  }
494 
495 ?>