Solved

SQL images to the Web with C++ CGI

Posted on 1998-07-01
4
160 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now