Solved

SQL images to the Web with C++ CGI

Posted on 1998-07-01
4
169 Views
Last Modified: 2013-11-18
Ok, this is a pretty involved question, and can go in about 3 different groups, but here goes...

What I'm trying to do, is get an image from a MS-SQL 6.5 server, and display it on a web page. The web page is currently generated by a CGI program written in C++, and already interacts with the database, just not image data. Am I going to have to create a file everytime I want to display an image stored in the database, or is there some shortcut to dump this binary data straight to the web browser? All of the images will be stored in the database in the same format (.JPG) as thats what I seem to get the best compression with.
0
Comment
Question by:wrussell051197
[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
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 100 total points
ID: 1167012
You can't send the image to the browser from the same instance of the cgi that generates the html page. You have to generate the html with image tags which reference the desired image(s), and then the browser will turn around and request the image from you in a separate request. This request could be almost immediate, or slightly delayed (if the user has images turned off but chooses to load your image) or much later (if the user bookmarks the image).

There are two main ways to generate the images. The first is for the html-producing cgi to generate a temporary .jpg file which the IMG tag references. The second is for the IMG tag to reference a second cgi (or the same cgi with an additional parameter that indicates the cgi should produce the image not the html). The image-producing cgi needs enough information passed as parameters to be able to retrieve the image data from the database. It can return an image rather than html by producing the correct headers (i.e. Content-type: mage/jpeg instead of Content-type: text/html).

Each way has its pluses and minuses, and it depends on your requirements. The temporary file method is slightly more efficient since once the file has been created the server handles it directly with no cgi, but there is the overhead of having to do periodic cleanup of old files and ensuring that you have the necessary disk space. Images are also not bookmark-able which might be a plus or a minus depending on your application. Generating the image via cgi is slightly slower but an easier overall solution. It isn't practical if the information needed to fetch the image can't easily be included in the IMG parameters, though. It also generates more database accesses, which on a high-traffic or connection-limited server could be a problem.

That was the overview, let me know if you have any questions or need more detail. Good luck!
0
 

Author Comment

by:wrussell051197
ID: 1167013
Ok, I'd like to do it without generating a temporary file, would be much easier for what I need... let me see if I have this correct...

My first cgi program should generate something like:

<img src="\cgi-bin\getimage.exe?image=1035" Height = 100 Width = 100>

at which point the browser will then want getimage.exe which should send

Content type: image/jpeg

and then what? Just the raw binary data? Or does it need to be encoded or what?
0
 
LVL 6

Expert Comment

by:alamo
ID: 1167014
Yes, the img tag is correct, except you'd use / not \ in the URL.

Your getimage cgi first has to print the headers, which at a minimum is just
Content type: image/jpeg
followed by a blank line to signal the end of the headers and the beginning of the data (i.e. "Content type: image/jpeg\r\n\r\n"). You can include other headers, of course but the above will work by itself. It's a nice touch to include a "Content-length: xxxxx" header giving the size in  bytes of the image, so the user gets a % complete display as its loading.

After the headers, you just print the raw binary data, with no encoding at all. You may need to set STDOUT for binary access, rather than text, so that the system doesn't helpfully insert \n into your binary data wherever it sees \r :-)
0
 

Author Comment

by:wrussell051197
ID: 1167015
Thanks! I'll give it a try and see how it works :)
I always have trouble with slashes, I have to go back and change about half of them after I write anything :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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