Search code examples
htmlcgraphicsserver-side

Can we output a picture in C?


can we output a .jpg image or .gif image in C?

I mean can we print a picture as output with the help of a C program?

Aslo can we write a script in C language for HTML pages as can be written in JavaScript?

Can the browsers operate on it?

If not possible is there any plugin for any of the browsers?

Any example code or links please?


Solution

  • You can generate a web page from a C program by using the Common Gateway Interface (CGI). The C program is compiled and runs on the server, this is different from Javascript which runs on the browser.

    You can also generate images via CGI too. Just set the content type appropriately, eg. image/jpeg for a JPEG image. You can use libjpeg to generate the actual image.


    Here is an example C program to generate a page with a JPEG image:

    #include <stdio.h>
    
    main()
    {
       char *pageTitle = "Look, a JPEG!";
       char *urlImage  = "/myimage.jpeg";
    
    // Send HTTP header.
       printf("Content-type: text/html\r\n\r\n");
    
    // Send the generated HTML.
       printf("<html><head><title>%s</title></head>\r\n"
              "<body>\r\n"
              "<h1>%s</h1>\r\n"
              "<img src=\"%s\">\r\n"
              "</body></html>\r\n",
              pageTitle, pageTitle, urlImage);
    }