Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Remove the // from //header("Content-type: image/png");, and remove the var_dump

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 

  • #4
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 

  • #6
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts
    And I did get rid of the vardump as well and still box with red x.
    Shad

  • #7
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Wow, you're quick! Thanks...I'll try your buffer suggestions above.
    Shad

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ok good good, did the buffering help at all or no?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 

  • #11
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Its working!! Thanks for all the help...

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •