Permalink
Browse files

How the fuck *does* this work?

  • Loading branch information...
1 parent 90706d9 commit a444600b64c7e7f3bb1dc2cc6c149d386cb07407 @mspreij committed Sep 1, 2011
Showing with 129 additions and 46 deletions.
  1. +37 −0 graph.class.php
  2. +8 −6 image.class.php
  3. +39 −14 imageplus.class.php
  4. +45 −26 recobject.class.php
View
@@ -17,6 +17,9 @@ class Graph extends ImagePlus {
var $offx2;
var $offy2;
var $img;
+ var $values = array();
+ var $line_margin_top = 5; // percentage from highest value of graph/line to top of graph bounding box
+ var $line_margin_bottom = 10;
function __construct($x, $y=0, $offx=10, $offy=null, $offx2=null, $offy2=null) {
parent::__construct($x, $y); // constructs the image
@@ -34,6 +37,39 @@ function draw_bounding_box($color=0) {
}
+ // -- plot_graph($color=0)
+ function plot_graph($color=0) {
+ if (! is_numeric($color)) $color = $this->intify($color);
+ $this->min_val = min($this->values);
+ $this->max_val = max($this->values);
+ $this->delta_val = ($this->max_val - $this->min_val) * (($this->line_margin_top + $this->line_margin_bottom + 100)/100); // height of graph in 'val', including margins
+ $this->graph_bottom_val = $this->max_val - (($this->max_val - $this->min_val) * (1 + ($this->line_margin_bottom/100))); // value of 'val' at graph bottom
+ $this->delta_px = $this->getxy('y') - ($this->offy + $this->offy2);
+ $this->graph_width_px = $this->getxy('x') - ($this->offx + $this->offx2);
+ $prev_vals = array($this->offx, $this->val2px($this->values[0])); // initial x,y
+ foreach($this->values as $i => $val) {
+ if (! $i) continue;
+ $val_x = $this->offx + ($i * $this->graph_width_px / (count($this->values)-1));
+ $val_y = $this->val2px($val);
+ $this->line($prev_vals[0], $prev_vals[1], $val_x, $val_y, $color);
+ $prev_vals = array($val_x, $val_y);
+ }
+ }
+
+ function val2px($val) {
+ $val_px = ($this->delta_px - ((($val - $this->graph_bottom_val) / $this->delta_val) * $this->delta_px)) + $this->offy;
+ return $val_px;
+ }
+
+
+ function set_values($values) {
+ if (! is_array($values)) {
+ $values = preg_split('/[^0-9.]/', $values); // who knows, it might work.
+ }
+ $this->values = $values;
+ }
+
+
function __destruct() {
parent::__destruct();
}
@@ -42,6 +78,7 @@ function __destruct() {
/* -- Log --------------------------------
+[2011-04-18 22:06:54] added set_values()
[2011-04-15 23:37:57] made second constructor argument optional in the silly case someone wants to use a background image for the graph.
(Also, been bloody ages..)
View
@@ -5,11 +5,10 @@
* Version 0.01a - Released May 5th, 2005
*
* If you make modifications that may be useful to others, please send
- * them to <j |at| jgdataworks.com>.
+ * them to <j |at| jgdataworks.com>. (<-- that is PROBABLY outdated.)
* --
* [2006-08-28 23:39:54]
- * Modified by Maarten Spreij (gmail.com@mspreij) - http://mechintosh.com/
- * Version 0.1a (why not)
+ * Modified by Maarten Spreij (gmail.com@mspreij) - http://mech.cx/
* Last update: [2009-04-06 14:12:10]
* ---------------------------------------
*
@@ -194,8 +193,8 @@ function scale_image($size, $stretch=false) {
return true;
}
- #__________________
- # getxy($side='') /
+ //__________________
+ // getxy($side='') /
function getxy($side='') {
if (strtolower($side) == 'x') return imagesx($this->img);
if (strtolower($side) == 'y') return imagesy($this->img);
@@ -284,6 +283,7 @@ function destroy() {
}
/* -- Log --------------------------------
+
[2009-04-06 14:12:10] added __clone() method.
[2006-12-12 14:23:37] getxy() -> getxy($side=''), so it can return either x or y as well
[2006-09-01 00:56:45] w00t. scaling and stretching and such worked out.
@@ -292,8 +292,10 @@ function destroy() {
for it if both width and height were specified, which atm only the calc_sizes function can "know". So it might
just do $this->stretch_check = true;
-Todo: Might want to clean up code a little, rename variables, // instead of # and such.
+Todo: the constructor throws warnings before trying to interpret the input as image data. do something intelligent with
+ is_readable, keeping http links in mind (will it download the image twice? pls check)
Todo: cache x,y dimensions of image in getxy(), reset after scaling.
+Todo: Might want to clean up code a little, rename variables, // instead of # and such.
------------------------------------------
Done:
- Return statements in constructor, those should not be needed/wanted, right? Right
View
@@ -38,11 +38,14 @@ class ImagePlus extends Image {
var $img; # image resource
var $x = 100;
var $y = 100;
- var $bgcolor = 0xFFFFFF;
- var $antialias = true;
- var $fontfile = 'fonts/arialblack.ttf';
- var $selected = false; // active selection flag
+ var $antialias = true;
+ var $fontfile = 'fonts/arialblack.ttf';
+ var $size = 12;
+ var $line_height = 14;
+ var $selected = false; // active selection flag
var $copy;
+ var $color = 0; // black.
+ var $bgcolor = 0xFFFFFF; // white.
var $color_list = array(
'black' => 0x000000,
'silver' => 0xC0C0C0,
@@ -96,14 +99,34 @@ function tttext($text, $x, $y, $color=0, $size=12, $angle=0) {
imagettftext($this->img, (float) $size, $angle, (int) $x, (int) $y, $color, $this->fontfile, $text);
}
- // -- textbox($size=12, $angle=0, $text) Todo: merge this with ttsize() below
- function textbox($size=12, $angle=0, $text) {
- return imageftbbox($size, $angle, $this->fontfile, $text);
+ // -- textbox($text, $x, $y, $width, $size=12, $color=0)
+ function textbox($text, $x, $y, $width, $size=12, $color=0) {
+ $lines = explode("\n", trim($text));
+ $line_nr = 0;
+ foreach ($lines as $i => $line) {
+ $subline = $subline_new = '';
+ foreach (explode(' ', $line) as $word) {
+ $subline_new .= ($subline?' ':'') . $word;
+ $res = imagettfbbox((float) $size, 0, $this->fontfile, $subline_new);
+ $subline_width = $res[2]-$res[0];
+ if ($subline_width >= $width) { // derp!
+ $this->tttext($subline, $x, $y + $line_nr * $this->line_height, $color, $size);
+ $line_nr++;
+ $subline_new = $word;
+ }else{
+ $subline = $subline_new;
+ }
+ }
+ // trailing word(s).
+ $this->tttext($subline_new, $x, $y + $line_nr * $this->line_height, $color, $size);
+ $line_nr++;
+ }
+ return $line_nr * $this->line_height;
}
- // -- ttsize ($text, $size=12, $full=0)
- function ttsize ($text, $size=12, $full=0) {
- $res = imagettfbbox((float) $size, 0, $this->fontfile, $text);
+ // -- ttsize ($text, $size=12, $full=0, $angle=0)
+ function ttsize ($text, $size=12, $full=0, $angle=0) {
+ $res = imagettfbbox((float) $size, $angle, $this->fontfile, $text);
if ($full) return $res;
return array($res[2]-$res[0], $res[1]-$res[7]);
}
@@ -250,13 +273,15 @@ function __destruct() {
[2008-12-17 11:42:36] experimenting with $coords vs "$x, $y, $x2, $y2, .." [*1]
[2008-12-17 11:38:42] set alpha blending is true by default
+Todo: draw($format) for gif, jpg etc? Set default jpeg compression property.
+Todo: add possibility to use different types of fonts (postscript, freetype, ..)
Todo: add function head comments, update f list, etc
Todo: add some helper methods to deal with alpha stuff ("60% red")
-Todo: some of these methods are getting too many optional arguments.. pass assoc array with defaults ('foo'=>$this->foo, or 'foo'=>$this->settings['foo']) in the function body?
+Todo: some of these methods are getting too many optional arguments.. pass assoc array with defaults ('foo'=>$this->foo, or
+ 'foo'=>$this->settings['foo']) in the function body?
Todo: select/deselect is nice. Next up: layers?
-Todo: draw($format) for gif, jpg etc?
-Todo: Catch errors, print them as text in the image. If no image can be created, create one anyway large enough to hold the error text. If the error-image can't be
- created, cry and stamp your feet.
+Todo: Catch errors, print them as text in the image. If no image can be created, create one anyway large enough to hold the error text. If the
+ error-image can't be created, cry and stamp your feet.
------------------------------------------
Oops, something went wrong.

0 comments on commit a444600

Please sign in to comment.