Jump to content

Why won't this work arg! Dynamic PNG Graph


wantstolearnphp

Recommended Posts

Following Sams PHP 24hrs... Rewrote Example and it isn't working... I am not getting any syntax error just an error saying it cannot display the graph because there are errors in the image.  I have went through it line for line looking for mispellings you name it making sure it was exactly as they wrote so I could follow along... but now its is not working and I believe its me.. not the book... can anyone help me out?

 

<?php

//Send content type header to browser so image will be rendered

header ("Content-type: image/png");

 

class SimpleBar

{

private $xgutter = 20;//left/right margin

 

private $ygutter = 20;//top/bottom margin

 

private $bottomspace = 30;//gap at the bottom

 

private $internalgap = 10;//gap between bars

 

private $cells = array();//labels/amounts for bar charts

 

private $totalwidth;

 

private $totalheight;

 

private $font;

 

 

function __construct( $width, $height, $font )

{

$this->totalwidth = $width;

$this->totalheight = $height;

$this->font = $font;

}

 

//Used to allow user to populate $cells[] property array

function addBar( $label, $amount )

{

$this->cells[ $label ] = $amount;

}

 

private function _getTextSize( $cellwidth )

{

$textsize = (int)( $this->bottomspace );//Make $textsize equal to bottomspace

if ( $cellwidth < 10 )

{

$cellwidth = 10;

}

//Loop through the cells array and acquire dimension info for the labels using imageTTfbBox()

foreach ( $this->cells as $key=>$val )

{

while ( true )

{

$box = imageTTFbBox( $textsize, 0, $this->font, $key  );

$textwidth = abs ( $box[2] );

if ( $textwidth < $cellwidth )

{

break;

}

$textsize--;

}

}

return $textsize;

}

 

function draw()

{

$image = imagecreate( $this->totalwidth, $this->totalheight );//Create Image Resource

//Create Color resources

$red = imagecolorallocate( $image, 255,0,0 );//Red

$blue = imagecolorallocate( $image, 0,0,255 );//Blue

$black = imagecolorallocate( $image, 0,0,0 );//Black

 

$max = max( $this->cells );//Cache the maxium value the $cells[] array has

 

$total = count( $this->cells );//Cache the number of elements the $cells[] array contains

 

$graphCanX = ( $this->totalwidth - $this->xgutter*2 );//Set up the canvas space on the X-axis for the graph

 

$graphCanY = ( $this->totalheight - $this->ygutter*2 -  $this->bottomspace );//Set up the canvas space on the Y-axis for the graph ( Have to allow room for our labels and margins )

 

$posX = $this->xgutter;//Store the starting point for drawing our bars on the X-axis

 

$posY = $this->totalheight - $this->ygutter; - $this->bottomspace;//Store the starting point for drawing our bars on the Y-axis ( Have to allow room for our labels and margins )

 

$cellwidth = (int) ( ( $graphCanX -( $this->internalgap * ( $total - 1 ) ) ) / $total );//Calculate the cellwidth for each bar by taking the width of the graph canvas and subtracting the total distance bewteen the bars divided by the total amount of bars being used

 

$textsize = $this->_getTextSize( $cellwidth );

 

foreach( $this->cells as $key=>$val )

{

$cellheight = (int) ( ( $val/$max ) * $graphCanY );//Calculate height of each bars

 

$center = (int) ( $posX + ($cellwidth/2) );//Calculate cener point of bars

 

$imagefilledrectangle( $image, $posX, ($posY - $cellheight), ($posX+$cellwidth), $posY, $blue);//Draw the bars

 

$box = imageTTFbBox( $textsize, 0, $this->font, $key );

 

$tw = $box[2];

 

imageTTFtext( $image, $textsize, 0, ($center-($tw/2) ),

( $this->totalheight-$this->ygutter), $black, $this->font, $key );

$posX += ( $cellwidth + $this->internalgap);

}

 

imagepng( $image );

}

 

}

 

$graph = new SimpleBar( 500, 300, "league_gothic-webfont.ttf" );

$graph->addBar("Really Liked" , 200);

$graph->addBar("Liked" , 100);

$graph->addBar("Kinda Like" , 300);

 

$graph->draw();

 

 

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.