[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C; GD; image output not working

Posted on 2009-07-08
5
Medium Priority
?
214 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:BaconU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 24806010
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24806081
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 24806205
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
 

Author Closing Comment

by:BaconU
ID: 31601229
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24806304
Heh, that happens to the best of us ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
In this tutorial viewers will learn how to create blended and gradiated shapes in Illustrator using the blend tool Draw two shapes, one of them in a different color: Select both and create a blend by going to Object > Blend > Make: Blends can also b…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question