• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

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.
0
AndrewShanklin
Asked:
AndrewShanklin
  • 2
  • 2
2 Solutions
 
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now