C; GD; image output not working

I'm using C to develop a dynamic image set with GD, and am having problems with the output of the image.  The source is simple right now so that I can just get the stupid thing working.  The provided code below compiles without error or problem (using gcc -Wall -lgd testimage.c -o testimage).  When I run it from the command line, the output appears as it should (the content type shows up and then the PNG header and then PNG data), but when I load it in a browser, it doesn't work.

Firefox keeps telling me "You have chosen to open 'testimage' which is an 'application/octet-stream'.  If I embed the image in HTML code using an <IMG> tag, I just get the broken image thing.  IE will not display it either.  I must be missing something.

Any ideas why it's not working?

#include <stdio.h>
#include "gd.h"
 
int main(void)
{
  gdImagePtr im;
  int        black;
  int        white;
 
  im    = gdImageCreate(64, 64);
  black = gdImageColorAllocate(im, 0, 0, 0);
  white = gdImageColorAllocate(im, 255, 255, 255);
 
  gdImageRectangle(im, 0, 0, 63, 63, black);
  gdImageLine(im, 0, 0, 63, 63, white);
 
  printf("Content-Type: image/jpeg\n\n");
  gdImagePng(im, stdout);
 
  gdImageDestroy(im);
  return 0;
}

Open in new window

BaconUAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
Don't you want to save the image to a file first ? Right now, you're just printing it to stdout, seemingly to try to construct an HTTP response, but that's not really how HTTP responses are constructed.

Try saving it to a file, then try opening the file, and see if that works.
0
Infinity08Commented:
Something like :
#include <stdio.h>
#include "gd.h"
 
int main(void)
{
  gdImagePtr im;
  int        black;
  int        white;
 
  im    = gdImageCreate(64, 64);
  black = gdImageColorAllocate(im, 0, 0, 0);
  white = gdImageColorAllocate(im, 255, 255, 255);
 
  gdImageRectangle(im, 0, 0, 63, 63, black);
  gdImageLine(im, 0, 0, 63, 63, white);
 
  {
    FILE *out = fopen("image.png", "wb");
    gdImagePng(im, out);
    fclose(out);
  }
 
  gdImageDestroy(im);
  return 0;
}

Open in new window

0
Infinity08Commented:
Maybe you are trying to make this work as CGI ? Is so, something like what you had should do the trick. After renaming the executable with an extension .cgi, you put it on your web server in the appropriate directory, and then browse to the cgi file using your browser.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BaconUAuthor Commented:
I don't want to save it to a file, because yes I am using it as a CGI.  I apparently have Mal's disease aka  a problem of my brain being missing.  I figured out about the time of your second post that it was a web server parse issue, because I didn't include an extension on the executable.  So, the code works perfectly as it should... just not the operator. :)
0
Infinity08Commented:
Heh, that happens to the best of us ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.