2009-05-02 8 views

答えて

7

あなたが行く:このような

#!/usr/bin/perl -w 
my $file = "inner-nav.gif"; 
my $length = (stat($file)) [10]; 
print "Content-type: image/gif\n"; 
print "Content-length: $length \n\n"; 
binmode STDOUT; 
open (FH,'<', $file) || die "Could not open $file: $!"; 
my $buffer = ""; 
while (read(FH, $buffer, 10240)) { 
    print $buffer; 
} 
close(FH); 
+1

非常にシンプルで、CGIを読み込む必要はありません。ありがとうございます。 – Nifle

+1

申し訳ありません - 私のPerlは非常に錆びています。それは私がネット上で見つけた(テストされた)例でした。 – RichieHindle

+0

何かがありますか? - 私のブラウザは、画像がブラウザに表示された後でさえも、読み込み中の状態(アニメーション化された読み込みシンボルで表される)にあることを示しています。ロードが完了したことを示すために、クライアントに返された応答の最後に何かが追加されるべきでしょうか? – therobyouknow

6

は何か...

#!/usr/bin/perl 

use strict; 
use warnings; 
use CGI; 

my $gfx=''; 
$gfx = makeImage(); 
print CGI::header(type=>'image/png', 
        expires=>'+1m', 
        content_length=>length($gfx)}); 
print $gfx; 
0

PNGまたはJPGファイルを処理するシンプルなソリューション。より多くのファイルタイプを実行する場合は、GDの最新バージョンを参照してください。

http://www.perlmonks.org/?node_id=18565

sub serveImage 
{ 
    use GD; 

    my ($localPath) = @_; 

    if($localPath =~ /\.png/i) 
    { 
     print "Content-type: image/png\n\n"; 
     binmode STDOUT; 
     my $image = GD::Image->newFromPng($localPath); 
     print $image->png; 
    } 
    else 
    { 
     print "Content-type: image/jpeg\n\n"; 
     binmode STDOUT; 
     my $image = GD::Image->newFromJpeg($localPath); 
     print $image->jpeg(100); 
    } 


} 
1

コードに小さな修正 - 提供statコマンドは、ファイルの長さを返しませんでした。一部のブラウザは気にしなかったが、他のブラウザは画像を読み込めなかった。 (stat($ file))[10]はファイルの長さではなく、 'ctime'です。

#!/usr/bin/perl -w 
my $file = "inner-nav.gif"; 
my $length = -s $file; 
print "Content-type: image/gif\n"; 
print "Content-length: $length \n\n"; 
binmode STDOUT; 
open (FH,'<', $file) || die "Could not open $file: $!"; 
my $buffer = ""; 
while (read(FH, $buffer, 10240)) { 
    print $buffer; 
} 
0

1人のユーザーが、不足しているものがあるかどうか尋ねました。私はそう思う。スクリプトの最後にexit 1;がありません。ここに私の改訂版(笑私はexit 1;で追加)

#!/usr/bin/perl -w 
my $file = "inner-nav.gif"; 
my $length = (stat($file)) [10]; 
print "Content-type: image/gif\n"; 
print "Content-length: $length \n\n"; 
binmode STDOUT; 
open (FH,'<', $file) || die "Could not open $file: $!"; 
my $buffer = ""; 
while (read(FH, $buffer, 10240)) { 
    print $buffer; 
} 
close(FH); 
exit 1; 

良い方法だ、私はとにかく考えて、これを行うことです。

#!/usr/bin/perl 

# must haves! 
use strict; 
use warnings; 

use CGI; 
my $cgi = new CGI; # used in 'getParam($)' for getting URL paramaters 

use lib "pm"; # my own perl modules library 
use user; # my user related functions 
use dir; # my directory handling functions 

# these will be used for $fn if $fn not found, read error, or no user 
my $file_not_found = "/img_srvr/error-file-not-found.jpg"; 
my $read_error = "/img_srvr/error-reading-image.jpg"; 
my $no_such_user = "/img_srvr/error-no-such-user.jpg"; 

# the premise of the following is to capture all input into separate vars 
# verify that each element is correct, and then spit out the image. 

# for my site. remove it if you like. see below for getParam($) definition 
my $uid = getParam("uid"); 
if (not userExists($uid)) { printImage($no_such_user); exit 1; } 

my $folder = "/img_srvr/$uid"; # the folder where the images are stored 

my $fn = getParam("img"); # see below for definition 
my $path = "$folder/$fn"; # this, too, _is_ better 

if (not fileExists($path)) 
    { printImage($file_not_found); exit 1; } else 
    { printImage($path); } 

exit 1; 


######################################################################### 


###################### 
sub printImage($) { 
    # be sure to do your error checking BEFORE calling this. it'll just 
    # blindly rip along. 
    my $fn = $_[0]; 
    my $type = getType($fn); # see sub below 
    my $buffer = ""; 

    print "content-type: image/$type\n"; # these are awful, but ok for now 
    print "\n"; # separate just in case we want to add more to the header. 

    binmode STDOUT; 

    open my $FH, "<", $fn or die "$!"; 
    while (read ($FH, $buffer, 10240)) { 
    print $buffer; # prefer NOT to print as I read... 
    } 
    close $FH; 

    # return $OUTPUT; # this would be better, no? 
} 

###################### 
# there's gotta be a better way, spock! 
sub getType($) { 
    my $f = $_[0]; 

    if ($f =~ /\.gif$/i) { return "gif"; } 
    if ($f =~ /\.jpg|\.jpeg$/i) { return "jpeg"; } 
    if ($f =~ /\.png$/i) { return "png"; } 

    return "bmp"; 
} 

sub getParam($) { 
    return $cgi->param($_[0]); 
} 

ああ!画像を「サイズを変更」することが可能である、私が作った上記printImage機能を使用して、最後に

====

:そして、これはa useful link(!mimeタイプ)であるかもしれませんか?もしそうなら、どうですか?私は別のパッケージなどをインストールしたくありません。それは単純でなければならない。

関連する問題