LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2009, 08:38 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
PRINTing images in CGI script (HTML tags reqd.)


Hello,
I have an image (a photo) in a variable (obtained from a MySQL database), eg. $ary[4]. I wish to display it in my Perl CGI script. My question is, th normal image tag ie. <IMG> takes a SRC parameter of a *file*, whereas I just want to PRINT the image in the variable. What tags must I surround it with?

Thanks.
 
Old 02-09-2009, 02:19 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your SRC parameter can be of the form "http://servename.whatever/cgi-bin/yourCgi.pl?theNameOfTheImageFile". Your perl script would then just print the file to it standard output.
Code:
#! /bin/perl -w
#
use strict;
	print "Content-Type: image/png\n\n";
	open( IMAGEFILE, $ARGV[0] ) || die "Cannot open '$ARGV[0]' : $!\n";
	while( my $imageData = <IMAGEFILE> ){
		print $imageData;
	}
	close IMAGEFILE;
	exit;
You will probably want to do something more graceful for error exit, and also modify the mime-type according to the type of file you are dumping.

--- rod.

Last edited by theNbomr; 02-09-2009 at 02:22 PM.
 
Old 02-22-2009, 09:36 AM   #3
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
I can't do that as the script outputs a lot of things besides the photo, the photo is just one of them.
Any more suggestions?

Thanks.
 
Old 02-22-2009, 10:38 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
HTML does not provide for what you are attempting to do. Inline images are fetched by the browser as separate HTTP requests, and not as image data embedded in the HTML document. An HTML document is always formed from non-binary 'human-readable' text, and image data would not fit this criteria.
If your photo image is a static image, and not generated by code on-the-fly, and if it is stored somewhere in a server's document root, simply embedding the image URL in the HTML <IMG> tag generated by your CGI script should suffice. Otherwise, the method I gave can be used, but rather than simply opening the static image file, your second CGI can generate the image as necessary for your application, and print it with the appropriate Content-Type header.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script for adding HTML tags deesto Linux - Newbie 9 03-12-2008 06:40 PM
perl cgi script and images. sholah Linux - Software 2 03-02-2007 11:20 AM
script to grab html content from between specific tags sonicthehedgehog Programming 6 01-30-2007 01:14 PM
cgi/html/php/something script paul_mat Linux - Software 0 01-23-2006 04:23 PM
Bash script for correcting HTML tags hq4ever Programming 4 11-08-2004 04:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration