PHP GD question

Started by
4 comments, last by jjmontes 19 years, 8 months ago
I'm creating a dynamic graph in PHP, and, dependant upon who is logged in, I'd like to be able to display a chart that is unique to the user. So, here is my problem. My code for makeing the chart works, but I don't know how to tell PHP who is logged in. To display the chart, i'm useing: img src="Chart.php" where Chart.php has all the coded needed to stream a .png image to the screen. I 'need' to do it this way because there is more information on the screen than just the chart. But, the problem is, I have no way of passing in variables to make the code user specific. I've tried: img src="Chart.php?user=<?php echo $name; ?> But, that did me no good. Also, Chart.php doesn't seem to recognize session variables. I guess its because the server is creating the image before displaying to the PC, and the PC keeps track of your session. Any ideas on how to solve this problem would be much appreciated.
Advertisement

Why is this a GD question? Do you actually have problems creating the image?

Anyway, you should be able to read GET parameters:

// - - - - - - - - - - - - - - - - - -// This is the script that sends the image// which reads parameters// - - - - - - - - - - - - - - - - - -require_once SCF_BASEDIR . '/includes/system/clsImage.php';$img = & new clsImage();$img->LoadImage ($_GET["image"]);if (($_GET["width"])&&($_GET["height"])) {  $img->Resize ($_GET["width"],$_GET["height"]);  } else {  $img->Thumbnail ($_GET["width"],$_GET["height"]);}$img->Gamma(1.5);$img->SendToBrowser();// - - - - - - - - - - - - - - - - - -// Here is the Image class (clsImage)// - - - - - - - - - - - - - - - - - -class clsImage {  var $Type;  var $Image;  var $JPEGQuality;    function clsImage ($File = FALSE) {    $this->JPEGQuality = 75;    if ($File) $this->LoadImage ($File);  }    function LoadImage ($File) {    // Miramos el tipo y cargamos una imagen    // Otra opción: if (!($type = exif_imagetype ($File))) return FALSE;    if (!($size = getimagesize ($File))) return FALSE;    $type = $size[2];    switch ($type) {      case 1:        $this->Image = imagecreatefromgif ($File);        break;      case 2:        $this->Image = imagecreatefromjpeg ($File);        break;      case 3:        $this->Image = imagecreatefrompng ($File);        break;      default: return FALSE;    }    if (!$this->Image) return FALSE;    $this->Type = $type;     return TRUE;  }    function WriteFile ($Filename) {    switch ($this->Type) {      case 1: imagegif ($this->Image, $Filename); break;      case 2: imagejpeg ($this->Image, $Filename, $this->JPEGQuality); break;      case 3: imagepng ($this->Image, $Filename); break;    }   }    function SendToBrowser ($DestroyIt = TRUE) {    switch ($this->Type) {      case 1: $mime = "image/gif"; break;      case 2: $mime = "image/jpeg"; break;      case 3: $mime = "image/png"; break;    }     header ("Content-type: " . $mime);     switch ($this->Type) {      case 1: imagegif ($this->Image); break;      case 2: imagejpeg ($this->Image, '', $this->JPEGQuality); break;      case 3: imagepng ($this->Image); break;    }    if ($DestroyIt) $this->Destroy();  }  function SetFormat ($Format) {    switch (strtolower($Format)) {      case "gif": $this->Type = 1; break;      case "jpg": case "jpeg": $this->Type = 2; break;      case "png": $this->Type = 3; break;    }  }   function Destroy() {    imagedestroy ($this->Image);    $this->Type = FALSE;  }  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    function Width() {    return (imagesx($this->Image));  }    function Height() {    return (imagesy($this->Image));  }    function Resize ($Width, $Height) {    $col = imagecolortransparent ($this->Image);    $nueva = imagecreate($Width,$Height);    imagecopyresized($nueva,$this->Image,0,0,0,0,$Width,$Height,imagesx($this->Image),imagesy($this->Image));    imagedestroy ($this->Image);    $this->Image = $nueva;    imagecolortransparent ($this->Image,$col);  }    function Resample ($Width, $Height) {    $nueva = imagecreate($Width,$Height);    imagecopyresampled($nueva,$this->Image,0,0,0,0,$Width,$Height,imagesx($this->Image),imagesy($this->Image));    imagedestroy ($this->Image);    $this->Image = $nueva;  }    function Gamma ($Gamma) {    // Valores mayores que 1 volverán la imagen más clara    imagegammacorrect ($this->Image, 1.0, $Gamma);  }    function Thumbnail ($Width, $Height, $Resample = FALSE) {    // Reduce una imagen a un tamaño fijo, pero manteniendo el ratio    // constante. El ancho o el alto deben ser 0 (o falso).    $imgwidth = imagesx($this->Image);    $imgheight = imagesy($this->Image);    $ratio = $imgwidth / $imgheight;    if (($Width) && ($Height)) return;    if ((!$imgwidth) || (!$imgheight)) return;    if (($Width) && ($imgwidth > $Width)) {      $newwidth = $Width;      $newheight = $Width / $ratio;    }     if (($Height) && ($imgheight > $Height)) {      $newheight = $Height;      $newwidth = $Height * $ratio;    }    if ((!$newwidth) || (!$newheight)) return;    if (!$Resample) {      $this->Resize ($newwidth,$newheight);    } else {      $this->Resample ($newwidth,$newheight);    }  }


The code above uses my own image class, but as you see, I use $_GET["image"] and $_GET["height"] to read parameters passed to the script.

If this does not help, please explain in detail your problem.
-=[ J ]=-I always forget to change the tagline.
Well, I call it a GD problem because it involves image output. But let me see if I can be more specific.

My graph works fine. I can manually enter the username into the code (and, as a side note, I have $result = mysql_query("select * from table where username='name'").... this 'name' is what I'm trying to pass to the code), open Graph.php, and it works fine. The problem is, I don't know how to get that graph onto a webpage containing more information than just the graph. The big problem is, I have to pass the headers to the browser. On a page with more than just an image on it, it gets tricky. My previous solution was to put the .php file into the img src tag. In other words.... i'd have:

html

table

tr
td img src = Graph.php /td
/tr

/table

/html


I'd show the brackets, '< and '> but, it causes problems once I post the message.

Anyways, as you can see from the code, i get around the header output problem by including the code in the img src tag (this is the only way I know to do it). But, if I do this, the code I have written to make the graph can't have any variables passed to it (at least, as far as I know). If I were to type localhost/Graph.php?username=Bob, yes, I could display the information I needed for Bob. But, again, I want this chart to be in a web page containing more information than just the chart alone. If I were to make my graph a function, and call that function amongst my html/php code, i'd get a header output problem.


I hope this explains everything. If not, I can post the whole code... i'm just not quite sure how to add the scroll box like you did.
Ok. To post code put it between tags "source" and "/source" (they must be enclosed by brackets).

So you say that the following doesn't work?

<!-- This is HTML --><img src="chart.php?user=nick" />


It works for me... in fact I've seen this in many many many websites, and I've used it before. Why are you saying that it doesn't work? What do you see?


-=[ J ]=-I always forget to change the tagline.
jjmontes... I have fought with this thing all day long, and before going to all the trouble of posting my code, I wanted to try doing it one last time just to make sure I wasn't a moron...... and it turns out... I'm a moron. It works. I don't know what I was doing wrong before, but it works now, and you are the man.... or.... woman prehaps. I don't know... I can't tell by your name. But anyway, you're amazing... and thanks!

: )
Quote:Original post by Cygnus_X
I don't know what I was doing wrong before, but it works now, and you are the man.... or.... woman prehaps. I don't know... I can't tell by your name. But anyway, you're amazing... and thanks! : )


(Male) I'm glad to help. Every other people talks about vertexes and normals and reflections and can't help at all :-)
-=[ J ]=-I always forget to change the tagline.

This topic is closed to new replies.

Advertisement