Perl image link?

Hi All,

I need a really simple perl script which shows "prints" one image when you execute it.

This is how it should work.

Before you upload the script you should be able to configure a path to a specific directory:

e.g. /home/yoursite.com/public_html/imagefolder

The user will already know what the image name will be so...

when you execute the script

e.g. www.yoursite.com/View.pl?image=Image1.gif

The Image1.gif (stored in the /home/yoursite.com/public_html/imagefolder) directory is displayed.


Basically i am trying to hide the path (which effectively would be: www.yoursite.com/imagefolder/Image1.gif) from view.

Obviously their will be hundreds of images in the "imagefolder" so it will always be:

www.yoursite.com/View.pl?image= + Image Name

Hope this makes sense.

If you have a solution can you post the full code.

Many thanks

Andy.
AndrewShanklinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ahoffmannCommented:
depending on your web server use the rewriting modules to do that
0
TintinCommented:

#!/usr/bin/perl
use strict;
use CGI;

my $img_dir='/home/yoursite.com/public_html/imagefolder';

my $q = new CGI;
my $image = $q->param('image');
my $image_type;
($image_type = $image) = s/.*\.(\w+)$/$1/;

open IMAGE, "$img_dir/$image" or die "Can not open $img_dir/$image $!\n";
binmode(IMAGE); # Only needed on older versions of Perl
print $q->header("image/$image_type");
print <IMAGE>;
close IMAGE;

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndrewShanklinAuthor Commented:
Hi Tintin,

Thanks for your post.

I have tested it on my server and it seems a bit buggy.

The path i was using (the perl script being called view.pl) was

http://www.mysite.com/cgi-bin/view.pl?image=Microsoft.gif

as the Microsoft.gif logo is stored in the /home/myusername/public_html/Clients/ dir.

What happens whe then the script is executed is IE or Firefox trys to download the view.pl script and when you open it it has printed: GIF87a^

This is not a script execution problem of permissions issue in the cgi-bin.

The code i used was:


#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $img_dir='/home/myusername/public_html/Clients/';
my $q = new CGI;
my $image = $q->param('image');
my $image_type;
($image_type = $image) = s/.*\.(\w+)$/$1/;
open IMAGE, "$img_dir/$image" or die "Can not open $img_dir/$image $!\n";
binmode(IMAGE); # Only needed on older versions of Perl
print $q->header("image/$image_type");
print <IMAGE>;
close IMAGE;

Any ideas?

Many thanks

Andy.
0
ahoffmannCommented:
> ($image_type = $image) = s/.*\.(\w+)$/$1/;
are you sure you want rely on the given parameter to identify the image type?
I'd use:

my $image = $q->param('image');
   $image=~s/[^a-zA-Z0-9_.-]//g;
my $image_type = 'gif';
0
AndrewShanklinAuthor Commented:
Hi All,

Thanks for your solutions.

The solution supplied by Tintin (and then the fix by ahoffmann) solved the problem.

However in implementing it in my system i realsised that potentially it wouldnt only be images which users would try and hotlink.

So i think the perl script would need to be two scripts.

Script 1: view.pl

When you execute view.pl the script will print:

<img src="http://www.mysite.com/cgi-bin/Hotlink.pl?file=Microsoft.gif">

But if the extension of the File Name is "exe" or "zip" etc...filenames which aren't images. then a generic "NoImageAvailable.gif" picture will be shown instead.

The Script 2: Hotlink.pl which "view.pl" links to will still have the above code:

#!/usr/bin/perl
use strict;
use CGI;

my $img_dir='/home/yoursite.com/public_html/imagefolder';

my $q = new CGI;
my $image = $q->param('file');
my $image_type;
($image_type = $image) = s/.*\.(\w+)$/$1/;

open IMAGE, "$img_dir/$image" or die "Can not open $img_dir/$image $!\n";
binmode(IMAGE); # Only needed on older versions of Perl
print $q->header("image/$image_type");
print <IMAGE>;
close IMAGE;


But if the file is an "exe" or "zip" file then if the user puts that url into a link:

<a href="http://www.mysite.com/cgi-bin/Hotlink.pl?file=Windows.exe">This text</a>

the link will cause the file to be downloaded when clicked on and not shown like image files.

However i have no idea how to do any of this.

I have increased the points, but if you could provide code for both scripts i would be very grateful.

Best regards

Andy.

















0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.