Výstup grafů v PHP - Builder.cz - Informacni server o programovani

Odběr fotomagazínu

Fotografický magazín "iZIN IDIF" každý týden ve Vašem e-mailu.
Co nového ve světě fotografie!

 

Zadejte Vaši e-mailovou adresu:

Kamarád fotí rád?

Přihlas ho k odběru fotomagazínu!

 

Zadejte e-mailovou adresu kamaráda:



PHP

Výstup grafů v PHP

php

21. února 2002, 00.00 | Na svém webu jsem potřeboval graficky zobrazit počet návštěv. Proto jsem vytvořil třídu GudGraf.php, která toto usnadňuje. Výstup se provádí jako na virtuální plotr..

Na svém webu http://guderna.oceany.cz/ jsem potřeboval graficky zobrazit počet návštěv. Proto jsem vytvořil třídu GudGraf.php, která toto usnadňuje. Výstup se provádí jako na virtuální plotr. Ve třídě jsou implementovány tyto metody:

- konstruktor GudGraf($x,$y,$typ) : definice velikosti kreslící plochy
  • $x - šířka obrázku v pixlech
  • $y - výška obrázku v pixlech
  • $typ - formát obrázku: gif, jpg, png (string)

    - Scale($xmin,$ymin,$xmax,$ymax) : měřítko souřadnic
  • $xmin,$ymin - souřadnice levého spodního rohu
  • $xmax,$ymax - souřadnice pravého horního rohu

    - Move($x,$y) : přesun pisátka bez kreslení na novou pozici
  • $x,$y - souřadnice kam se přesune pisátko

    - Plot($x,$y) : spuštění pisátka a přesun na novou pozici
  • $x,$y - souřadnice kam se přesune pisátko

    - aMove($x,$y) : absolutní přesun pisátka bez kreslení na novou pozici, udává se v pixlech
  • $x,$y - souřadnice kam se přesune pisátko

    - aPlot($x,$y) : spuštění pisátka a přesun na novou absolutní pozici
  • $x,$y - souřadnice kam se přesune pisátko

    - PenColor($R,$G,$B) : barva pisátka v RGB
  • $x,$y - souřadnice kam se přesune pisátko

    - OutString($retez,$velikost) : výstup řetězce na aktuální pozici pisátka
  • $retez - vystupující znaky
  • $velikost - hodnota 1 - 5, udávající velikost písma

    - Paint() : ukončení kresby a zobrazení na strance HTML


    Modul testgud.php se vyvolá: <IMG src="testgud.php">

    
    <?
    include "GudGraf.php"; 
    $xmax=300; // velikost obrazku: 300x200 ( sirka x vyska )
    $ymax=200;
    
    // zvoleni grafickeho formatu podle nainstalovane knihovny v PHP
    if (ImageTypes() & IMG_PNG) 
    	$grafika=new GudGraf($xmax,$ymax,"png");
    else
    {
    	if (ImageTypes() & IMG_JPG) 
    		$grafika=new GudGraf($xmax,$ymax,"jpg");
    	else
    	{
    		if (ImageTypes() & IMG_GIF) 
    			$grafika=new GudGraf($xmax,$ymax,"gif");
    	}
    }
    $grafika->Scale(0,0,10,10); // urceni souradnic
    $grafika->PenColor(255,0,0); // cervene pisatko
    
    $grafika->Move(0,0);	// obdelnik 1
    $grafika->Plot(0,5);
    $grafika->Plot(5,5);
    $grafika->Plot(5,0);
    $grafika->Plot(0,0);
    
    $grafika->Move(5,5);	// obdelnik 2
    $grafika->Plot(5,10);
    $grafika->Plot(10,10);
    $grafika->Plot(10,5);
    $grafika->Plot(5,5);
    
    $grafika->Move(2,2.5); // popis
    $grafika->PenColor(0,0,200);
    $grafika->OutString("obdélník 1",2);
    
    $grafika->Move(7,7.5);
    $grafika->OutString("obdélník 2",2);
    
    // Vytvoreni barevnych sloupecku vlevo nahore
    imagefilledrectangle($grafika->im,10,10,20,30,
      ImageColorAllocate($grafika->im,255,0,0));
    imagefilledrectangle($grafika->im,20,10,30,30, 
      ImageColorAllocate($grafika->im,0,255,0));
    imagefilledrectangle($grafika->im,30,10,40,30, 
      ImageColorAllocate($grafika->im,0,0,255));
    
    $grafika->Paint();
    ?>
    

    Modul testsin.php se vyvolá: <IMG src="testsin.php">

    <?
    include "GudGraf.php"; 
    $xmax=300;
    $ymax=200;
    if (ImageTypes() & IMG_PNG) 
    	$grafika=new GudGraf($xmax,$ymax,"png");
    else
    {
    	if (ImageTypes() & IMG_JPG) 
    		$grafika=new GudGraf($xmax,$ymax,"jpg");
    	else
    	{
    	  if (ImageTypes() & IMG_GIF) 
    		$grafika=new GudGraf($xmax,$ymax,"gif");
    	}
    }
    $grafika->Scale(0,-1.1,6.28,1.1);
    $grafika->PenColor(0,0,0);
    $grafika->Move(6.3,0);
    $grafika->Plot(0,0);
    $grafika->PenColor(255,0,0);
    for ($i=0;$i<6.3;$i=$i+0.1) $grafika->Plot($i,sin($i));
    $grafika->aMove(40,50);
    $grafika->PenColor(0,0,200);
    $grafika->OutString("Funkce: y = sin(x)",2);
    
    $grafika->Paint();
    ?>

    Třída GudGraf.php

    <?
    class GudGraf{
    var $locx, $locy, $im, $loctyp,$xmin,$ymin,$xmax,$ymax,$kx,$ky,
     $lastx, $lasty, $R, $G, $B, $posunx, $posuny;
    function GudGraf($x,$y,$typ)
    {
    	$this->locx=$x;
    	$this->locy=$y;
    	$this->loctyp=$typ;
    	switch (StrToUpper($this->loctyp)) {
    	case "GIF":
    		header("Content-type: image/gif");
    		break;
    	case "JPG":
    		header("Content-type: image/jpeg");
    		break;
    	case "PNG":
    		header("Content-type: image/png");
    		break;
    	default:
    	}
    	$this->im = imagecreate($x,$y);	
    	imagefill($this->im, 0, $y-1,
              ImageColorAllocate ($this->im, 255,255,255));
    	$this->lastx=0;
    	$this->lasty=5;
    	$this->aPlot(0,$y);
    	$this->aPlot($x-5,$y);
    	$this->aPlot($x-5,5);
    	$this->aPlot(0,5);
    
    	$this->aMove(1,6);
    	$this->aPlot(1,$y-1);
    	$this->aPlot($x-6,$y-1);
    	$this->aPlot($x-6,6);
    	$this->aPlot(1,6);
    	imagerectangle($this->im, 5, $y-1,$x-1, $y-4, 
               ImageColorAllocate ($this->im, 170,170,170));
    	imagerectangle($this->im, 6, $y-2,$x-2, $y-3, 
               ImageColorAllocate ($this->im, 170,170,170));
    	ImageRectangle($this->im, $x-4, $y-1,$x-1, 5, 
               ImageColorAllocate ($this->im, 170,170,170));
    	ImageRectangle($this->im, $x-3, $y-2,$x-2, 6, 
               ImageColorAllocate ($this->im, 170,170,170));
    	$this->posunx=3;
    	$this->posuny=8;
    }
    
    function Scale($xmin,$ymin,$xmax,$ymax)
    {
    	$this->xmin=$xmin;
    	$this->ymin=$ymin;
    	$this->xmax=$xmax;
    	$this->ymax=$ymax;
    	$this->kx =
              ($this->locx-($this->posunx+$this->posuny)) / ($xmax - $xmin);
    	$this->ky = 
              ($this->locy-($this->posunx+$this->posuny)) / ($ymax - $ymin);
    	$this->lastx=(-$this->xmin)*$this->kx;
    	$this->lasty=(-$this->ymin)*$this->ky;
    }
    
    function Move($x,$y)
    {
    	$this->lastx=$this->posunx+($x-$this->xmin)*$this->kx;
    	$this->lasty=$this->posuny+($y-$this->ymin)*$this->ky;
    }
    
    function aMove($x,$y)
    {
    	$this->lastx=$x;
    	$this->lasty=$y;
    }
    
    function Plot($x,$y)
    {   
    	$xx=$this->posunx+($x-$this->xmin)*$this->kx;
    	$yy=$this->posuny+($y-$this->ymin)*$this->ky;
    	imageline($this->im,$this->lastx,$this->locy-$this->lasty,
             $xx,$this->locy-$yy, ImageColorAllocate ($this->im, $this->R, 
             $this->G, $this->B));
    	$this->lastx=$xx;
    	$this->lasty=$yy;
    }
    
    function aPlot($x,$y)
    {
    	imageline($this->im,$this->lastx,$this->locy-$this->lasty,
              $x,$this->locy-$y, ImageColorAllocate($this->im, $this->R,
              $this->G, $this->B));
    	$this->lastx=$x;
    	$this->lasty=$y;
    }
    
    function DejRozmer(&$x,&$y)
    {
    	$x=$this->locx;
    	$y=$this->locy;
    }
    
    function PenColor($R,$G,$B)
    {
    	$this->R=$R;
    	$this->G=$G;
    	$this->B=$B;
    }
    
    function OutString($retez,$velikost)
    {
    	ImageString($this->im,$velikost,$this->lastx,$this->locy-$this->
             lasty-imagefontheight($velikost), $retez,ImageColorAllocate($this->im, 
             $this->R, $this->G, $this->B));
    }
    
    function Paint()
    {
    	ImageString($this->im,1,$this->locx-130,$this->locy-15,
              "http://guderna.oceany.cz",ImageColorAllocate($this->im,100,150,250));
    	switch (StrToUpper($this->loctyp)) {
    	case "GIF":
    		imagegif($this->im);
    		break;
    	case "JPG":
    		imagejpeg($this->im);
    		break;
    	case "PNG":
    		imagepng($this->im);
    		break;
    	default:
    	}
    	ImageDestroy($this->im);
    }
    }?>

    Download skriptů a ukázek

    Více informací na http://guderna.oceany.cz

  • Tématické zařazení:

     » Rubriky  » PHP  

     » Rubriky  » Web  

    Poslat článek

    Nyní máte možnost poslat odkaz článku svým přátelům:

    Váš e-mail:

    (Není povinný)

    E-mail adresáta:

    Odkaz článku:

    Vzkaz:

    Kontrola:

    Do spodního pole opište z obrázku 5 znaků:

    Kód pro ověření

     

     

     

     

    Nejčtenější články
    Nejlépe hodnocené články

     

    Přihlášení k mému účtu

    Uživatelské jméno:

    Heslo: