2015-01-26 237 views
1

這Perl腳本上傳圖像到服務器,然後保存: - 適合在900x900像素 畫廊的形象 - 一個正方形的畫廊縮略圖140x140像素 - 在圖像和縮略圖名稱的js文件中添加一行內部服務器錯誤

問題是,該腳本有時有效,有時 - 不是。它每10次嘗試一次或兩次就能正常工作。當它不起作用時,它通常會返回「內部服務器錯誤」,並且不會創建兩個文件,也不會在js中添加一行。但在某些情況下,它會創建兩個jpg文件並且不會在js中添加一行(再次返回「內部服務器錯誤」)。非常奇怪的行爲 - 我嘗試了各種更改,但沒有結果。我做錯了什麼?

#!/usr/bin/perl -w 
## 
## 

use strict; 
use CGI; 
use CGI::Carp qw (fatalsToBrowser); 
use File::Basename; 
use Image::Magick; 

$CGI::POST_MAX = 1024 * 70000; 
my $safe_filename_characters = "a-zA-Z0-9_.-"; 
my $pic_upload_dir="../data/photos/gallery"; 
my $lst_upload_dir="../data"; 
my $lst_file=$lst_upload_dir."/gallery.js"; 

my $query=new CGI; 

my $PictureIndex=$query->param("Snd_AddPhoto_Idx"); 
my $photoname=$query->param("AddPhoto"); 

    #upload photo 
    if (!$photoname) { 
     print "Content-Type: text/plain\n\n"; 
     print "\n\nThere was a problem uploading your photo (try a smaller size).\n"; 
     exit; 
    } 

    my ($phname, $phpath, $phextension) = fileparse ($photoname, qr/\.[^.]*/); 
    $photoname = $phname . $phextension; 
    $photoname =~ tr/ /_/; 
    $photoname =~ s/[^$safe_filename_characters]//g; 

    if ($photoname =~ /^([$safe_filename_characters]+)$/) { 
     $photoname = $1; 
    } 
    else { 
     die "Filename contains invalid characters"; 
    } 

     # force correct filename for temporary file 
     $photoname="tempphoto_zmm_gallery_".$PictureIndex.$phextension; 

    my $upload_photohandle = $query->upload("AddPhoto"); 

    open (UPLOADPHOTO, ">$pic_upload_dir/$photoname") or die "$!"; 
    binmode UPLOADPHOTO; 
    while (<$upload_photohandle>) { 
     print UPLOADPHOTO; 
    } 
    close UPLOADPHOTO; 

    # resize photo 
    my($photoimage) = Image::Magick->new; 
    open(PHOTOIMAGE, "$pic_upload_dir/$photoname") or die "Unable to open temporary image file!\n"; 
    $photoimage->Read(file=>\*PHOTOIMAGE); 
    close(PHOTOIMAGE); 

    $photoimage->Resize(geometry=>'900x900', blur=>0.8); 
    $photoimage->Set(Quality=>'75%'); 

    # write ready photo as jpg 
    my $readyphotoname="pic".$PictureIndex.".jpg"; 
    open(READYIMAGE, ">$pic_upload_dir/$readyphotoname") or die "Unable to write ready image file!\n"; 
    $photoimage->Write(file=>\*READYIMAGE, filename=>$readyphotoname); 
    close(READYIMAGE); 
    system("chmod 777 $pic_upload_dir/$readyphotoname"); 

    # resize thumbnail 
    my($thumbimage) = Image::Magick->new; 
    open(THUMBIMAGE, "$pic_upload_dir/$photoname") or die "Unable to open temporary image file!\n"; 
    $thumbimage->Read(file=>\*THUMBIMAGE); 
    close(THUMBIMAGE); 

    $thumbimage->Resize(geometry=>'140x140^', blur=>0.8); 
    $thumbimage->Set(gravity=>'Center'); 
    $thumbimage->Crop(geometry=>'140x140+0+0'); 
    $thumbimage->Set(Quality=>'30%'); 

    # write ready thumbnail as jpg 
    my $readythumbname="tbn".$PictureIndex.".jpg"; 
    open(READYTHUMB, ">$pic_upload_dir/$readythumbname") or die "Unable to write ready image file!\n"; 
    $thumbimage->Write(file=>\*READYTHUMB, filename=>$readythumbname); 
    close(READYTHUMB); 
    system("chmod 777 $pic_upload_dir/$readythumbname"); 

    # delete temporary file 
    my($temporary_file)=$pic_upload_dir."/".$photoname; 
    unlink($temporary_file) == 0; 


# add pic in js gallery list 

    # prepare new pic record 
    my $NewGalRecord="GalleryList.push(new Array(\"pic".$PictureIndex.".jpg\",\"tbn".$PictureIndex.".jpg\",\"\",\"\"));\n"; 

    # add to file 
    open(JS,">>$lst_file") || die "Failed to open $lst_file\n"; 
    printf JS $NewGalRecord; 
    close JS; 
    system("chmod 777 $lst_file"); 

# print confirmation 

... 
... 
... 


exit; 
+0

當上傳「大」只有圖像,沒有縮略圖(沒有「調整縮略圖」和「寫準備縮略圖爲jpg」部分)的腳本正常工作每次。 – byddles 2015-01-26 23:34:41

+1

服務器日誌中報告了什麼? – emcconville 2015-01-27 00:18:45

+0

@emcconville'code'admin_zmm_gallery_add.cgi:在/ home/zmm/public_html/cgi-bin/admin_zmm_gallery_add.cgi第108行void無效使用數字eq(==)。 'code'admin_zmm_gallery_add.cgi:Argument「 75%「在子程序條目中不是數字,在/home/zmm/public_html/cgi-bin/admin_zmm_gallery_add.cgi第78行,第6399行。 'code'admin_zmm_gallery_add.cgi:參數」30%「isn '在子例程條目中的數字位於/home/zmm/public_html/cgi-bin/admin_zmm_gallery_add.cgi第97行,第6399行。 'code'腳本標題的早期結束:admin_zmm_gallery_add.cgi – byddles 2015-01-27 08:27:25

回答

0

我想我已經解決了這個問題。顯然,不需要兩次讀取臨時文件。保存好'大'圖像後,我們可以繼續操作它,然後再以縮略圖形式保存。雙重讀取臨時文件顯然會導致衝突。像'共享違規' - 只是在黑暗中拍攝。但是現在這個腳本工作正常。 另外,遠離行設置(質量=>)。我不知道他們是否與這個問題有任何關係,但這將受到未來的測試。 下面是變化:

# resize photo 
    my($photoimage) = Image::Magick->new; 
    open(PHOTOIMAGE, "$pic_upload_dir/$photoname") or die "Unable to open temporary image file!\n"; 
    $photoimage->Read(file=>\*PHOTOIMAGE); 
    close(PHOTOIMAGE); 

    $photoimage->Resize(geometry=>'900x900', blur=>0.8); 

    # write ready photo as jpg 
    my $readyphotoname="pic".$PictureIndex.".jpg"; 
    open(READYIMAGE, ">$pic_upload_dir/$readyphotoname") or die "Unable to write ready image file!\n"; 
    $photoimage->Write(file=>\*READYIMAGE, filename=>$readyphotoname); 
    close(READYIMAGE); 
    system("chmod 777 $pic_upload_dir/$readyphotoname"); 

    # resize thumbnail 
    $photoimage->Resize(geometry=>'140x140^', blur=>0.8); 
    $photoimage->Set(gravity=>'Center'); 
    $photoimage->Crop(geometry=>'140x140+0+0'); 

    # write ready thumbnail as jpg 
    my $readythumbname="tbn".$PictureIndex.".jpg"; 
    open(READYTHUMB, ">$pic_upload_dir/$readythumbname") or die "Unable to write ready image file!\n"; 
    $photoimage->Write(file=>\*READYTHUMB, filename=>$readythumbname); 
    close(READYTHUMB); 
    system("chmod 777 $pic_upload_dir/$readythumbname"); 

    # delete temporary file 
    my($temporary_file)=$pic_upload_dir."/".$photoname; 
    unlink($temporary_file) == 0;