...

View Full Version : gd displaying random symbols instead of png



shadkeene
05-23-2008, 01:17 AM
Hi,
I'm fairly new to php and gd, but I have created some graphs that take data from mysql and display it with a png. However, I'm now trying to use php and gd on a windows IIS server(other times have been using linux), and I'm now getting weird symbols when trying to display an image.

Here's my code:



<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
error_reporting(E_ALL);
error_reporting(E_ALL|E_STRICT);

// Define .PNG image
//header("Content-type: image/png");
$imgWidth=800;
$imgHeight=800;



// Create image and define colors
$image=imagecreate($imgWidth, $imgHeight)
or die("Cannot Initialize new GD image stream");

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



$link = mysql_connect('localhost', 'root', '*daver*') or die ("Could not connect: " . mysql_error());
mysql_selectdb('test',$link) or die ("Can\'t use dbmapserver : " . mysql_error());


$tslilt0 = mysql_query("select summer_am_models.fcli_sve, summer_am_models.date, summer_pm_work.date_time from summer_am_models, summer_pm_work
where day(summer_am_models.date)=day(summer_pm_work.date_time) and summer_pm_work.tsn='TS' and summer_am_models.fcli_sve<0",$link);
if (!$tslilt0)
{
echo "no results ";
}

$tsligt0 = mysql_query("select summer_am_models.fcli_sve, summer_am_models.date, summer_pm_work.date_time from summer_am_models, summer_pm_work
where day(summer_am_models.date)=day(summer_pm_work.date_time) and summer_pm_work.tsn='NOCU' and summer_am_models.fcli_sve<0",$link);
if (!$tsligt0)
{
echo "no results ";
}

$ntimestslilt0 = mysql_num_rows($tslilt0);
$ntimestsligt0 = mysql_num_rows($tsligt0);

$a=$ntimestslilt0*30;
$b=$ntimestsligt0*30;



imagefilledrectangle($image, 0, (800-$a), 10, 800, $red);
imagefilledrectangle($image, 10, (800-$b), 20, 800, $red);

imagepng($image);
var_dump(gd_info());


mysql_close($link);
?>

And here's my output:
PNG  IHDR  ᾔ+PLTE 7eIDATx1 Om x ;@%IENDB`array(12) { ["GD Version"]=> string(27) "bundled (2.0.34 compatible)" ["FreeType Support"]=> bool(true) ["FreeType Linkage"]=> string(13) "with freetype" ["T1Lib Support"]=> bool(true) ["GIF Read Support"]=> bool(true) ["GIF Create Support"]=> bool(true) ["JPG Support"]=> bool(true) ["PNG Support"]=> bool(true) ["WBMP Support"]=> bool(true) ["XPM Support"]=> bool(false) ["XBM Support"]=> bool(true) ["JIS-mapped Japanese Font Support"]=> bool(false) }

I've installed php_g2.dll in my extensions directory and it seems to be working correctly based on the test array output. I've tried other random imagestrings and I get the same weird symbols. Thanks for any help!
Shad

Mwnciau
05-23-2008, 01:49 AM
Remove the // from //header("Content-type: image/png");, and remove the var_dump

Fou-Lu
05-23-2008, 01:50 AM
Uncomment the header for the content type. That should fix it.
Oh, and one other comment. This relies on output buffering to be controlled by the server. If you don't get an image (which may happen if you switch servers), control it and capture it with an output buffering technique. Until that time, don't worry about it :thumbsup:


Ah beaten to the punch!

shadkeene
05-23-2008, 01:56 AM
Thanks for the quick replies, but still problems.

I forgot to uncomment the header before I sent the post...but when I uncomment it, all I get is a box with a red x. I even put a simple imageline as follows just to test in case something with the mysql data is wrong.
imageline($image, 0, 600, 800, 600, $red);

Any thoughts? Thanks!
Shad

Fou-Lu
05-23-2008, 02:01 AM
If thats from including the image in an actual <img> tag, the problem may be because you are not flushing / using your output buffer.

Remember as well, since this is an image use absolutly NO output to the browser. Disable your error reporting (or even better, log it in a file instead), and remove all the echo's present. Now, before you run the imagepng, try to buffer it instead:


$output = "";
ob_start();
imagepng($image);
// Remember, get rid of your var dump as well.
$output = ob_get_contents();
ob_end_clean();
print $output;

'Course, you can always flush it instead, but with the variables there you can kind of see what that does.
Try that out, see if its a problem with the buffering instead of the header.

If your script used to work, and now it doesn't, I'd put buffering as a high cause for many many php issues.

shadkeene
05-23-2008, 02:02 AM
And I did get rid of the vardump as well and still box with red x.
Shad

shadkeene
05-23-2008, 02:03 AM
Wow, you're quick! Thanks...I'll try your buffer suggestions above.
Shad

Fou-Lu
05-23-2008, 02:03 AM
Ok good good, did the buffering help at all or no?

shadkeene
05-23-2008, 02:11 AM
sorry, no luck. still the red x box, and I added your suggested code. Is this odd cosidering when I run phpinfo I see that png is enabled? Any other thoughts? Thanks for your time,

Shad

Fou-Lu
05-23-2008, 02:22 AM
Hmm, I'm wondering if you're getting an error from your mysql queries. I tried this code:


<?php

$imgWidth=800;
$imgHeight=800;

// Define image ob handler
function ob_handler($ob)
{
header('Content-type: image/png');
header('Content-length: ' . strlen($ob));
return $ob;
}

// Create image and define colors
$image=imagecreate($imgWidth, $imgHeight)
or die("Cannot Initialize new GD image stream");

$red = imagecolorallocate ($image, 255, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);


imagefilledrectangle($image, 0, (800-(rand(75, 150))), 10, 800, $white);


ob_start('ob_handler');
imagepng($image);
ob_end_flush();

?>

Yeah, cheezy image base. But it worked without a problem. The only real changes from your code are:
- removed any mysql functions
- Added white so I could add it to the original background. Not really necessary but still...
- Changed the subtraction base to just be a random number between 75 & 150
- Added the ob_handler function. This just lets me get away with controlling the output as an image. Could be handy if you wanted to do error reporting instead, but will fail in an <img> tag...
- Flushed the buffer instead of capturing the outputs.

So I'm not sure what the problem is exactly. Like I said, you're almost there, so just keep tweeking it and eventually you'll get it to go ;)
Also, keep in mind you may need to clear your browser's cache out, it may be trying to give a bad image that was previously loaded up.

Good luck mate, keep trying the little things, you're almost done!

shadkeene
05-23-2008, 03:51 AM
Its working!! Thanks for all the help...

Fou-Lu
05-23-2008, 05:21 AM
Your welcome, glad it helped!
Out of curiosity, what did the problem end up being?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum