Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-23-2008, 01:17 AM   PM User | #1
shadkeene
Regular Coder

 
Join Date: May 2007
Posts: 148
Thanks: 6
Thanked 0 Times in 0 Posts
shadkeene is an unknown quantity at this point
gd displaying random symbols instead of png

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 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 ($image25500);



$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($image0, (800-$a), 10800$red);
imagefilledrectangle($image10, (800-$b), 20800$red);

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


mysql_close($link);
?>
And here's my output:
‰PNG  IHDR  á¾”+PLTEÿâ 7eIDATxœíÁ1 õOm x ;¯@Ó%‡IEND®B`‚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
shadkeene is offline   Reply With Quote
Old 05-23-2008, 01:49 AM   PM User | #2
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
Remove the // from //header("Content-type: image/png");, and remove the var_dump
Mwnciau is offline   Reply With Quote
Old 05-23-2008, 01:50 AM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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

Edit:
Ah beaten to the punch!
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 05-23-2008, 01:56 AM   PM User | #4
shadkeene
Regular Coder

 
Join Date: May 2007
Posts: 148
Thanks: 6
Thanked 0 Times in 0 Posts
shadkeene is an unknown quantity at this point
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
shadkeene is offline   Reply With Quote
Old 05-23-2008, 02:01 AM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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:
PHP Code:
$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.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 05-23-2008, 02:02 AM   PM User | #6
shadkeene
Regular Coder

 
Join Date: May 2007
Posts: 148
Thanks: 6
Thanked 0 Times in 0 Posts
shadkeene is an unknown quantity at this point
And I did get rid of the vardump as well and still box with red x.
Shad
shadkeene is offline   Reply With Quote
Old 05-23-2008, 02:03 AM   PM User | #7
shadkeene
Regular Coder

 
Join Date: May 2007
Posts: 148
Thanks: 6
Thanked 0 Times in 0 Posts
shadkeene is an unknown quantity at this point
Wow, you're quick! Thanks...I'll try your buffer suggestions above.
Shad
shadkeene is offline   Reply With Quote
Old 05-23-2008, 02:03 AM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Ok good good, did the buffering help at all or no?
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 05-23-2008, 02:11 AM   PM User | #9
shadkeene
Regular Coder

 
Join Date: May 2007
Posts: 148
Thanks: 6
Thanked 0 Times in 0 Posts
shadkeene is an unknown quantity at this point
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
shadkeene is offline   Reply With Quote
Old 05-23-2008, 02:22 AM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Hmm, I'm wondering if you're getting an error from your mysql queries. I tried this code:
PHP 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 ($image25500);
$white imagecolorallocate($image255255255);


imagefilledrectangle($image0, (800-(rand(75150))), 10800$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!
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 05-23-2008, 03:51 AM   PM User | #11
shadkeene
Regular Coder

 
Join Date: May 2007
Posts: 148
Thanks: 6
Thanked 0 Times in 0 Posts
shadkeene is an unknown quantity at this point
Its working!! Thanks for all the help...
shadkeene is offline   Reply With Quote
Old 05-23-2008, 05:21 AM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Your welcome, glad it helped!
Out of curiosity, what did the problem end up being?
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:38 AM.


Advertisement
Log in to turn off these ads.