Jump to content

OOP imagepng issues


Jeffrey87

Recommended Posts

Hi,

I'm trying to convert a charting app to  OOP.

So here I've simplified it down to just the bare bones issue of drawing a line to a png image and displaying it.

 


<?php
class chart {
  var $im;
  var $backgroundColor;
  var $textColor;
  function chart() {
    $this->im= @ImageCreate (100, 200) or die ("Cannot Initialize new GD image stream");
    $this->backgroundColor = ImageColorAllocate ($this->im, 224, 234, 234);
    $this->textColor = ImageColorAllocate($this->im, 233, 14, 91);
  }

  function display() {
    imageline ($this->im,0,0,100,200,$this->backgroundColor);
    imageline ($this->im,100,0,0,200,$this->textColor);

    ImagePng ($this->im);
  }
}
?>

 

Here is the code which instantiates the class.

 

<?php
  include "simple_draw.php";

$mychart = new chart();
$mychart->chart();
$mychart->display();

?>


 

Basically depending on how I rework it, one of two things happens: a)it generates a bunch of little boxes which say fffd, or b) I get a message saying the resource is invalid or nonexistent (this error isn't coming up right now so I can't be precise.)

 

If this is run outside the OOP environment it works fine. No sure. I'm really trying to work with classes but this kind of problem is frustrating and admittedly makes it tempting to just go back to regular functions.

 

Any ideas??

 

Thanks,

Jeff

Link to comment
https://forums.phpfreaks.com/topic/219254-oop-imagepng-issues/
Share on other sites

also if you want to display it do something like

 

return $this->im;

Or:

echo $this->im;

 

 

fixed code:

 

<?php
class chart {
  var $im;
  var $backgroundColor;
  var $textColor;
  function chart() {
    $this->im= @ImageCreate (100, 200) or die ("Cannot Initialize new GD image stream");
    $this->backgroundColor = ImageColorAllocate ($this->im, 224, 234, 234);
    $this->textColor = ImageColorAllocate($this->im, 233, 14, 91);
  }

  function display() {
    imageline ($this->im,0,0,100,200,$this->backgroundColor); 
    imageline ($this->im,100,0,0,200,$this->textColor); 
    ImagePng ($this->im);
   return this->im;

   
  }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/219254-oop-imagepng-issues/#findComment-1137019
Share on other sites

Thanks for the replies.

 

Hey, I found the error! It was actually on the instantiation page. I had forgotten to include the header statement. The corrected version reads like this:


<?php
header ("Content-type: image/png");  
include "simple_draw.php";

$mychart = new chart();
$mychart->chart();
$mychart->display();

?>

 

Obvious now looking back on it :-\

Thanks again for the quick replies.

Link to comment
https://forums.phpfreaks.com/topic/219254-oop-imagepng-issues/#findComment-1137040
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.