Perl CGI从文件向浏览器显示图像 [英] Perl CGI display image to browser from file

查看:159
本文介绍了Perl CGI从文件向浏览器显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我一直在网上寻找一种使用Perl在Web浏览器上显示图像的解决方案,但没有找到适合我的方法。

我尝试过可能的解决方案,例如:

如何使用Perl

输出图像数据

从脚本中返回图像

,但都不适合我在做什么。我想拒绝客户端访问图像(或者甚至只是将图像文件放到www根目录之外),然后将其放入服务器端。

这是我在做什么的示例:


在我的主要perl文件中:

  ... 
my $ query = CGI-> new();

子主程序{
###这根据页面名称从模块中获取页面内容。
###模块返回HTML。
我的$ html = get_page('page','session');

###在打印标题之前在此处执行任何特殊条件...

print $ query-> header(此处有一些cookie /会话数据);
打印$ html

}

在其中一个模块中:

  sub return_page_content {

return<< HTML;
< html>
< body>
< img src =在此处获取图像... />
< / body>
< / html>
HTML
}

我想过只创建一个副本image放在临时目录位置,但这似乎会破坏使该映像不受客户端访问的全部目的。

可能的解决方案不会生成该映像。我不确定从这里去哪里,所以我希望这里的人有一个主意。非常感谢!

如果需要提供其他信息,请告诉我。我觉得这可能对很多人都有益。我希望至少;-)

解决方案

好吧,事实证明这行不通是因为我完全忘记了我不要使用标准cgi_bin目录之外的CGI进行操作。相反,我使用.htaccess文件告诉服务器如何以及何时将根目录中的文件解释为cgi。



因此,这是我在图像投放脚本中最终使用了什么:



imagedish.pm

 使用CGI; 

我的$ cgi =新的CGI;

开(IMAGE, logo.png);
my $ size = -s logo.png;
我的$ data;
读取IMAGE,$ data,$ size;
关闭(IMAGE);

打印$ cgi-> header(-type =>’image / png’),$ data;

这确实可以解决问题,因为它从一开始就应该这样做,但是在我的.htaccess文件中,我补充说:

 < files imagedish.pm> 
SetHandler cgi脚本
< / files>

然后就成功了(嗯,那以及进入Terminal并运行chmod + x imagedish.pm以使其可执行)!当然,接下来的步骤是其他安全措施,但至少现在可以了! :-)


完整解决方案:
主文件:

 !/ usr / bin / perl 

使用CGI;
使用CGI :: Carp qw(fatalsToBrowser);
使用CGI :: Session qw / -ip-match /;
使用DBI;
使用严格;
使用警告;

#变量
my $ query = CGI-> new();
我的%vars = $ query-> Vars();

sub main {

my $ p1 = $ vars {p1};

$ p1 =(Home)if(!$ p1);


my $ html = get_page();

#如果有多个会话,我会使用此方法
#我已经省略了如何获取会话,因为这不是解决方案的一部分;-)
打印$ query-> header(-cookie => [$ query-> cookie($ vars {p1} => $ session-> id)]);
打印$ html;
}

sub get_page {
return<< HTML;
<!DOCTYPE HTML>
< html>
< head>
< title>图片展示< / title>
< link rel =快捷方式图标 href = images / favicon.ico />
< link href = css / style.css rel = stylesheet type = text / css />
< / head>
< body>
< div class = container>
< div class = addentry>
< div class = iaddentry>
< form name = client action = method = POST>
< div class = form-header action =>
< center>
< img src = imagedish.pm width = 305 alt = />< br>
< / center>
< br>
< / div>
< / form>
< / div>
< / div>
< / div>
< / body>
< / html>
HTML
}


main();

在这里,img标签查找源 imagedish.pm,一旦找到它,.htaccess文件告诉它作为CGI脚本执行。到那时,它可以适当地分发信息,而不像以前那样。


请注意,这不是最安全的方式,但是它可以使我朝正确的方向前进。 / p>

Friends,
I have been scouring the web for a solution to displaying images to a web browser with Perl and have found nothing that works for me.
I've tried possible solutions such as:
How To Display an Image with Perl
Outputting Image Data
Return an Image From a Script
and none of it works for what I'm doing. I want to deny client access to the image (Or even simply place the image file out of the www root) and dish it out server-side.
Here is an example of what I'm doing:

In my main perl file:

...
my $query = CGI->new();

sub main {
### This grabs page content from a module, depending on the page name.
### The module returns the HTML.
my $html = get_page('page', 'session');

### Perform any special conditionals here before printing the header...

print $query->header(some cookie/session data here);
print $html

}

In one of the modules:

sub return_page_content {

return <<HTML;
<html>
  <body>
    <img src="GET IMAGE HERE..." />
  </body>
</html>
HTML
}

I've thought about just creating a copy of the image in a temp directory location, but that seems like it would defeat the entire purpose of keeping the image out of client-side access.
The probable solutions do not generate the image. I'm not sure where to go from here, so I am hoping someone here has an idea. Thank you so much!
Please let me know if I need to provide additional information. I feel like this could be beneficial to a lot of people. I hope at least ;-)

解决方案

Well, it turns out the reason this wasn't working is because I completely forgot I do not operate with CGI out of the standard cgi_bin directory. Instead, I use an .htaccess file to tell the server how and when to interpret files from the root directory as cgi.

So, this is what I ended up using in my image dishing script:

imagedish.pm

use CGI;

my $cgi = new CGI;

open (IMAGE, 'logo.png');
my $size = -s "logo.png";
my $data;
read IMAGE, $data, $size;
close (IMAGE);

print $cgi->header(-type=>'image/png'), $data;

This did the trick, as it should have been doing from the beginning, but in my .htaccess file, I added:

<files imagedish.pm>
SetHandler cgi-script
</files>

And that did the trick (Well, that, as well as going into Terminal and running chmod +x imagedish.pm to make it executable)! Of course the next steps are additional security measures, but at least it's working now! :-)

The full solution: mainfile:

!/usr/bin/perl 

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Session qw/-ip-match/;
use DBI;
use strict;
use warnings;

# Variables
my $query = CGI->new();
my %vars = $query->Vars();

sub main {

    my $p1 = $vars{p1};

    $p1 = 'Home' if (!$p1);


    my $html = get_page();

    #I use this method in case we have multiple sessions
    #I've omitted how I acquire the session, as this is not part of the solution ;-)
    print $query->header(-cookie=>[$query->cookie($vars{p1}=>$session->id)]);
    print $html;
}

sub get_page {
    return <<HTML;
<!DOCTYPE HTML>
<html>
    <head>
        <title>Image Disher</title>
        <link rel="shortcut icon" href="images/favicon.ico" />
        <link href="css/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div class="container">
            <div class="addentry">
                <div class="iaddentry">
                    <form name="client" action="" method="POST">
                        <div class="form-header" action="">
                            <center>
                                <img src="imagedish.pm" width="305" alt=""/><br>
                            </center>
                            <br>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
HTML
}


main();

In here, the img tag looks for the source "imagedish.pm", and once it finds it, the .htaccess file tells it to execute as a CGI script. At that point, it dishes out information appropriately, not like before.

Please note, this is not the most-secure way to do it, but it gets me going in the right direction.

这篇关于Perl CGI从文件向浏览器显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆