Jump to content

Strange issue with custom function and image drawing


patrick89

Recommended Posts

Hi everyone,

 

I'm playing around with PHP at the moment to make a graph (first time really using the image drawing functions in PHP, so it's a bit of an experiment at the moment).

 

All was going well until I tried to use a function within the code. If I try to call a function which isn't one of the PHP image functions, the image doesn't display?

 

For example, including drawTimeLine() stops the image from displaying (simplified version of the code i've been working on but has the same problem...):

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

$timelineGraph = ImageCreate(300,300) or die("Cannot Initialize new GD image stream");

// Formatting
$black = ImageColorAllocate($timelineGraph, 0, 0, 0);
$red = ImageColorAllocate($timelineGraph, 255, 0, 0);

function drawTimeline($startWeek, $startDay, $endWeek, $endDay) {
ImageLine($timelineGraph, 0, 0, 299, 299, $red);
}

drawTimeline(1,1,1,1);

ImagePng($timelineGraph);

ImageDestroy($timelineGraph);

?>

 

While if you comment drawTimeline() out, the image will still appear (just as a black background, but still appears on screen).

 

I'm inserting it using the following code which I believe is right?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Timeline Graph</title>
</head>
<body>

<p><img src="test.php" alt="Graph" /></p>

</body>
</html>

 

Can't work out what the problem is? Does PHP not allow for custom functions within image creation?

 

Any help is much appreciated... I can't work out why this isn't working! I was going well up until now!

Link to comment
Share on other sites

When working with images if you can't figure out the problem it's usually a good idea to comment out the Content-type header so that you can see the error you're getting. In your case you have a scope problem. Neither $timelineGraph nor $red is set within the scope of that function. You can either make them globals (which is a bad practice), or preferably pass those variable as parameters.

 

Ex:

 

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

$timelineGraph = ImageCreate(300,300) or die("Cannot Initialize new GD image stream");

// Formatting
$black = ImageColorAllocate($timelineGraph, 0, 0, 0);
$red = ImageColorAllocate($timelineGraph, 255, 0, 0);

function drawTimeline($im, $color, $startWeek, $startDay, $endWeek, $endDay) {
	ImageLine($im, 0, 0, 299, 299, $color);
}
drawTimeline($timelineGraph, $red, 1,1,1,1);

ImagePng($timelineGraph);

ImageDestroy($timelineGraph);

?>

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.