Jump to content

Having trouble with a class script


jeff5656

Recommended Posts

I downloaded a class to generate graphs but it's not working.

 

Here is the script that genrates the graph:

 

<?php
require('chart.php');
require('data.php');

$chart = new chart(300, 200, "example1");
$chart->plot($data);
$chart->stroke();
?>

 

When I run this I get:

Fatal error: Call to undefined function imagecreate() in C:\wamp\www\chart\chart.php on line 316

 

Here is chart.php (sorry it's long) :

<?php



require('rgb.php');

// The following variables should be edited to fit your 
// installation.

// If debugging is switched on, caching is switched off.
$chart_debug = false;

// The directory where Chart will store cached images. 
// Make sure this exists.
$chart_cache_directory = "../chart/tmp";

// The default is to generate PNG images.  If you set
// this to false, GIF images will be generated instead.
$chart_use_png = true;

// If you want to use Type1 fonts, PHP has to be told
// where the IsoLatin1.enc file is.  Such a file is included
// in the Chart distribution.
$type1_font_encoding = "/home/httpd/html/circus/chart/IsoLatin1.enc";

// If your PHP is compiled with gd2, set this variable to true.
$gd2 = false;

function mod ($n, $m) {
  $n1 = (int)($n*1000);
  $m1 = (int)($m*1000);
  return ($n1 % $m1);
}

class chart {
  var $background_color = "white";
  var $x_size, $y_size;
  var $output_x_size = false, $output_y_size;
  var $plots = array();
  var $image;
  var $left_margin = 30, $right_margin = 10, 
    $top_margin = 20, $bottom_margin = 23;
  var $margin_color = "white";
  var $border_color = "black", $border_width = 1;
  var $title_text = array(), $title_where = array(), $title_color = array();
  var $legends = array(), $legend_background_color = "white", 
    $legend_margin = 8, $legend_border_color = "black";
  var $legend_placement = "r"; // left or right
  var $axes = "xy", $axes_color = "black";
  var $grid_color = array(230, 230, 230), $grid_under_plot = 1;
  var $tick_distance = 25;
  var $x_ticks = false, $x_ticks_format;
  var $scale = "linear";
  var $cache = false;
  var $x_label = false, $y_label = false;
  var $y_format = false;
  var $font = 2, $font_type = "internal", $font_name = 2, $font_size = 10;
  var $y_min = array(false), $y_max = array(false),
    $x_min = array(false), $x_max = array(false);
  var $frame = false;
  var $expired = false;
  var $marked_grid_point = false, $marked_grid_color = false;
  var $lockfd = -1, $lockfile = 0;
  var $tick_whole_x = false;
  var $months = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  var $cleanup_after_plotting = true;

  function mark_grid ($point = 0, $color = "red") {
    $this->marked_grid_point = $point;
    $this->marked_grid_color = $color;
  }

  function set_grid_color ($color = false, $grid_under = true) {
      if ($color)
$this->grid_color = $color;
      $this->grid_under_plot = $grid_under;
  }

  function set_output_size ($width, $height) {
    $this->output_x_size = $width;
    $this->output_y_size = $height;
  }

  function set_expired ($expired) {
    $this->expired = $expired;
  }

  function set_margins ($left = 30, $right = 10, 
		$top = 20, $bottom = 23) {
    $this->left_margin = $left;
    $this->right_margin = $right;
    $this->top_margin = $top;
    $this->bottom_margin = $bottom;
  }

  function set_tick_distance ($distance) {
    $this->tick_distance = $distance;
  }

  function set_labels ($x = false, $y = false) {
    $this->x_label = $x;
    $this->y_label = $y;
  }

  function set_extrema ($y_min = array(), $y_max = array(),
		$x_min = array(), $x_max = array()) {
    if (! is_array($y_min))
      $this->y_min = $y_min;
    if (! is_array($y_max))
      $this->y_max = $y_max;
    if (! is_array($x_min))
      $this->x_min = $x_min;
    if (! is_array($x_max))
      $this->x_max = $x_max;
  }

  function chart ($x, $y, $cache = false) {
    error_reporting(1);
    // If this image has already been cached, then we just spew
    // it out and exit.
    if ($cache)
      $this->get_cache($cache);
    // If not, we initialize this object and allow execution to continue.
    $this->x_size = $x;
    $this->y_size = $y;
  }

  function get_cache ($file) {
    global $chart_debug, $chart_cache_directory, $chart_use_png;
    $file = $chart_cache_directory . "/" . $file;
    // There probably is a security problem hereabouts.  Just
    // transforming all ".."'s into "__" and "//"'s into "/_" will 
    // probably help, though.
    while (ereg("[.][.]", $file)) 
      $file = ereg_replace("[.][.]", "__", $file);
    while (ereg("//", $file)) 
      $file = ereg_replace("//", "/_", $file);
    $this->cache = $file;
    if (! $chart_debug) {
while (true) {
    if (file_exists($file)) {
	// The chart is already in the cache, so we just serve
	// it out.
	if ($file = fopen($file, "rb")) {
	    $this->headers();
	    fpassthru($file);
	    exit;
	} 
    } else {
	// The idea here is to obtain a lock on the file to be
	// written before starting to write it.  That way we
	// can ensure that no chart is ever generated more
	// than once, which can be very important if the chart
	// is CPU intensive.
	$this->make_directory(dirname($file));
	$this->lockfile = "$file.lock";
	$lockfd = fopen($this->lockfile, "a");
	$tries = 0;
	while (! flock($lockfd, 2+4) && $tries++ < 200) {
	    sleep(1);
	}
	if ($tries >= 200) {
	    // We tried more than 20 seconds, so we delete the
	    // lock file, and try again.
	    fclose($lockfd);
	    unlink($this->lockfile);
	} else {
	    // We got the lock, so we break from the loop and 
	    // return, and generate the chart ourselves.
	    $this->lockfd = $lockfd;
	    break;
	}
    }
}
    }
    return false;
  }
  
  function put_cache ($image) {
    global $chart_use_png;
    $file = $this->cache;
    if (file_exists($file))
      unlink($file);
    $this->make_directory(dirname($file));
    // Writing to a tmp file, and then renaming the tmp file to the
    // real file gives us some atomicity on most sensible file systems.
    // That is, the real file will never exist in a half-written state.
    if ($chart_use_png)
      imagepng($image, "$file.tmp");
    else
      imagegif($image, "$file.tmp");
    rename("$file.tmp", "$file");
    imagedestroy($image);
    
    // Remove the lock file.
    if ($this->lockfd != -1) {
fclose($this->lockfd);
unlink($this->lockfile);
    }

    if ($file = fopen($file, "rb")) {
      $this->headers();
      fpassthru($file);
      exit;
    } 
    return true;
  }
  
  function make_directory ($file) {
    while (! (file_exists($file))) {
      $dirs[] = $file;
      $file = dirname($file);
    }
    for ($i = sizeof($dirs)-1; $i>=0; $i--) 
      mkdir($dirs[$i], 0777);
  }

  function set_border ($color = "black", $width = 1) {
    $this->border_color = $color;
    $this->border_width = $width;
  }

  function set_background_color ($color, $margin_color = false) {
    $this->background_color = $color;
    if ($margin_color)
      $this->margin_color = $margin_color;
  }

  function set_x_ticks ($ticks, $format = "date") {
    $this->x_ticks = $ticks;
    $this->x_ticks_format = $format;
  }

  function set_frame ($frame = true) {
    $this->frame = $frame;
  }

  function set_font ($font, $type = 0, $size = false) {
    $this->font_name = $font;
    $this->font_type = $type;
    if ($size)
      $this->font_size = $size;
  }

  function set_title ($title, $color = "black", $where = "center") {
    $this->title_text[] = $title;
    $this->title_where[] = $where;
    $this->title_color[] = $color;
  }

  function add_legend($string, $color = "black") {
    $this->legends[] = array($string, $color);
  }

  function set_axes ($which = "xy", $color = "black") {
    $this->axes = $which;
    $this->axes_color = $color;
  }

  function plot ($c1, $c2 = false, $color = false, $style = false,
	 $to_color = false, $param = false,
	 $texts = false) {
    $plot = new plot($c1, $c2);
    if ($color)
      $plot->set_color($color);
    if ($to_color)
      $plot->set_gradient_color($to_color, $param);
    if ($style)
      $plot->set_style($style);
    if ($texts)
      $plot->set_texts($texts);
    if ($param)
      $plot->set_param($param);
    $this->plots[] = &$plot;
    return $plot;
  }

  function splot ($plot) {
    $this->plots[] = &$plot;
  }

  function stroke ($callback = false) {
    global $chart_use_png, $type1_font_encoding;
    $xs = $this->x_size;
    $ys = $this->y_size;

    // Load the font for this chart.
    if ($this->font_type == "type1") {
$this->font = imagepsloadfont($this->font_name);
imagepsencodefont($this->font, $type1_font_encoding);
    } elseif ($this->font_type == "ttf") {
$this->font = imagettfloadfont($this->font_name);
    } else {
$this->font = $this->font_name;
    }

    if ($xs == 0 || $ys == 0) {
      php3_error(E_ERROR, "Invalid X or Y sizes: (%s, %s)", 
	 $xs, $ys);
    }
    $im = imagecreate($xs, $ys);
    $this->image = $im;
    $bgcolor = $this->allocate_color($this->background_color);
    imagefilledrectangle($im, 0, 0, $xs, $ys, $bgcolor);

    list ($xmin, $xmax) = $this->get_extrema(2);
    list ($ymin, $ymax) = $this->get_extrema(1);
    $grace = ($ymax-$ymin)*0.01;
    $ymin -= $grace;
    $ymax += $grace;

    if (! is_array($this->y_min))
      $ymin = $this->y_min;
    if (! is_array($this->y_max))
      $ymax = $this->y_max;
    if (! is_array($this->x_min))
      $xmin = $this->x_min;
    if (! is_array($this->x_max))
      $xmax = $this->x_max;

    if ($ymax == $ymin) {
      $ymax *= 1.01;
      $ymin *= 0.99;
    }
    if ($xmax == $xmin) 
      $xmax++;
    if ($ymax == $ymin) 
      $ymax++;

    $xoff = $this->left_margin;
    $yoff = $this->top_margin;
    $width = $xs - $this->left_margin - $this->right_margin;
    $height = $ys - $this->top_margin - $this->bottom_margin;

    $axes_color = $this->allocate_color($this->axes_color);

    if ($this->grid_under_plot) {
        // Draw the grid and the axes.
$this->draw_y_axis($im, $ymin, $ymax, $xs, $ys, $height, $yoff, false,
		   $axes_color);
$this->draw_x_axis($im, $xmin, $xmax, $xs, $ys, $width, $xoff, false,
		   $axes_color);
    }

    if (! $this->cleanup_after_plotting) {
      $margin = $this->allocate_color($this->margin_color);
      imagefilledrectangle($im, 0, 0, $xs, $this->top_margin-1, $margin);
      imagefilledrectangle($im, $xs-$this->right_margin+1, $this->top_margin-1,
		   $xs, $ys, $margin);
      imagefilledrectangle($im, 0, $ys-$this->bottom_margin+1, $xs, $ys, 
		   $margin);
      imagefilledrectangle($im, 0, 0, $this->left_margin-1, $ys, $margin);
    }
    
    // Go through all the plots and stroke them.
    if ($callback != false) {
      $callback($im, $xmin, $xmax, $ymin, $ymax,
	$xoff, $yoff, $width, $height);
    } else {
      for ($i = 0; $i < sizeof($this->plots); $i++) {
$plot = $this->plots[$i];
$plot->stroke($im, $xmin, $xmax, $ymin, $ymax,
	      $xoff, $yoff, $width, $height, &$this);
      }
    }

    if (! $this->grid_under_plot) {
        // Draw the grid and the axes.
$this->draw_y_axis($im, $ymin, $ymax, $xs, $ys, $height, $yoff, false,
		   $axes_color);
$this->draw_x_axis($im, $xmin, $xmax, $xs, $ys, $width, $xoff, false,
		   $axes_color);
    }

    // The plotting may have plotted outside of the allocated
    // "framed" area (if autoscaling is not in use), so we
    // blank out the surrounding area.
    if ($this->cleanup_after_plotting) {
      $margin = $this->allocate_color($this->margin_color);
      imagefilledrectangle($im, 0, 0, $xs, $this->top_margin-1, $margin);
      imagefilledrectangle($im, $xs-$this->right_margin+1, $this->top_margin-1,
		   $xs, $ys, $margin);
      imagefilledrectangle($im, 0, $ys-$this->bottom_margin+1, $xs, $ys, 
		   $margin);
      imagefilledrectangle($im, 0, 0, $this->left_margin-1, $ys, $margin);
    }
    
    if (! $this->frame) {
      imageline($im, $this->left_margin, $this->top_margin, 
	$this->left_margin, $ys-$this->bottom_margin+3, $axes_color);
      imageline($im, $this->left_margin-3, $ys-$this->bottom_margin,
	$xs-$this->right_margin, $ys-$this->bottom_margin,
	$axes_color);
    } else {
      imagerectangle($im, $this->left_margin, $this->top_margin, 
	     $xs-$this->right_margin, $ys-$this->bottom_margin, 
	     $this->allocate_color($this->border_color));
    }

    // Put the text onto the axes.
    $this->draw_y_axis($im, $ymin, $ymax, $xs, $ys, $height, $yoff, true,
	       $axes_color);
    $this->draw_x_axis($im, $xmin, $xmax, $xs, $ys, $width, $xoff, true,
	       $axes_color);

    $title_color = $this->allocate_color("black");

    // Draw the labels, if any.
    if ($this->y_label) {
      if ($this->font_type == "type1") {
imagepstext ($im, $this->y_label, $this->font, $this->font_size, 
	     $this->allocate_color($title_color),
	     $this->allocate_color("white"),
	     15, (int)($ys/2+$this->string_pixels($this->y_label)/2),
	     0, 0, 90, 16);
      } else {
imagestringup($im, $this->font, 5,
	      $ys/2+$this->string_pixels($this->y_label)/2,
	      $this->y_label, $title_color);
      }
    }
    if ($this->x_label) 
      imagestring($im, $this->font,
	  $xs/2-$this->string_pixels($this->x_label)/2,
	  $ys-20, $this->x_label, $title_color);

    // Draw the boorder.
    if ($this->border_color) 
      imagerectangle($im, 0, 0, $xs-1, $ys-1, 
	     $this->allocate_color($this->border_color));

    // Draw the title.
    $tx = "noval";
    for ($i=0; $i<sizeof($this->title_text); $i++) {
      if ($this->font_type == "type1") {
if ($tx == "noval") {
  if (!strcmp($this->title_where[$i], "center")) {
    list ($llx, $lly, $urx, $ury) = imagepsbbox($this->title_text[$i],
						$this->font, 
						$this->font_size);
    $tx = $xs/2 - ($urx-$llx)/2;
  } else 
    $tx = 0;
}

imagepstext ($im, $this->title_text[$i], $this->font, 
	     $this->font_size, 
	     $this->allocate_color($this->title_color[$i]),
	     $this->allocate_color("white"),
	     (int)$tx, 15,
	     0, 0, 0, 16);
      } elseif ($this->font_type == "internal") {
if (!strcmp($this->title_where[$i], "center")) 
  $tx = $xs/2 - $this->string_pixels($this->title_text[$i])/2;
else 
  $tx = 0;

imagestring($im, $this->font, $tx, 5, $this->title_text[$i], 
	    $this->allocate_color($this->title_color[$i]));
      }
    }

    // Draw the legend.
    if (sizeof($this->legends) != 0) {
      $maxlength = 0;
      foreach ($this->legends as $legend) {
$length = $this->real_string_pixels($legend[0]);
if ($length > $maxlength)
  $maxlength = $length;
      }

      if ($this->legend_placement == "r") {
$x = (int)($this->x_size - $this->right_margin - $maxlength - 20);
$y = (int)($this->top_margin + 20);
      } else {
$x = (int)($this->right_margin + 40);
$y = (int)($this->top_margin + 20);
      }
      $lmargin = $this->legend_margin;
      // Draw a box behind the legend.
      if ($this->legend_background_color) {
imagefilledrectangle($im, $x-$lmargin, $y-$lmargin,
		     $x+$lmargin+$maxlength, 
		     $y+$lmargin+
		     (($this->font_size+2)*sizeof($this->legends)),
		     $this->allocate_color($this->legend_background_color));
      }
      if ($this->legend_border_color) {
imagerectangle($im, $x-$lmargin, $y-$lmargin,
	       $x+$lmargin+$maxlength, 
	       $y+$lmargin+
	       (($this->font_size+2)*sizeof($this->legends)),
	       $this->allocate_color($this->legend_border_color));
      }
      
      foreach ($this->legends as $legend) {
$this->draw_text($legend[0], $legend[1], $x, $y);
$y += $this->font_size+2;
      }
    }

    // Rescale the image before outputting, if requested.
    if ($this->output_x_size) {
      global $gd2;
      $owidth = $this->output_x_size;
      $oheight = $this->output_x_size;
      $om = imagecreate($owidth, $oheight);
      if ($gd2)
imagecopyresampled($om, $im, 0, 0, 0, 0,
		   $owidth, $oheight, $xs, $ys);
      else
imagecopyresized($om, $im, 0, 0, 0, 0,
		 $owidth, $oheight, $xs, $ys);
      $im = $om;
    }

    

?>

I had to Snip because this forum said I exceeded mximum characters.  but the imagecreate term does not appear in what I snipped.  Thanks for any help!

 

Link to comment
https://forums.phpfreaks.com/topic/182409-having-trouble-with-a-class-script/
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.