2015-01-26 331 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;