无法识别用于图像处理的 png 文件



我正在尝试将png分辨率添加到我们的图像处理代码中。我收到以下错误,我不知道如何解决。

[Tue Aug 27 15:32:03.968126 2019] [cgi:error] [pid 3206] [client] AH01215: Can't call method "getBounds" on an undefined value

这是我的代码。

my $file = "$datapath/$rs->{'path'}";
my ($ext) = $file =~ /(.[^.]+)$/;
if ($ext eq "jpg"){
my $src=GD::Image->newFromJpeg("$datapath/$rs->{'path'}",1);
} else {
my $src=GD::Image->newFromPng("$datapath/$rs->{'path'}",1);
}
my ($w,$h)=$src->getBounds();

当我有这个代码时,它只是jpg,它可以工作:

my $file = "$datapath/$rs->{'path'}";
my ($ext) = $file =~ /(.[^.]+)$/;
my $src=GD::Image->newFromJpeg("$datapath/$rs->{'path'}",1);
my ($w,$h)=$src->getBounds();

更新

现在jpegs没有显示。

my $file = "$datapath/$rs->{'path'}";
my ($ext) = $file =~ /(.[^.]+)$/;
my $src;
if ($ext eq "jpg"){
$src=GD::Image->newFromJpeg("$datapath/$rs->{'path'}",1);
} else {
$src=GD::Image->newFromPng("$datapath/$rs->{'path'}",1);
}
my $img=GD::Image->new($ow,$oh,1);
$img->copyResampled($src,$x,$y,0,0,$nw,$nh,$w,$h);
$img->edgeImageSharpen(9);
$img->edgeBrightnessContrast(8,1.1);
if($ext eq "jpg"){
my $jpg=$img->jpeg(90);
print "Content-Type: image/jpegn";
print "Content-Length: ".length($jpg)."nn";
print $jpg;
} else {
my $png=$img->png;
print "Content-Type: image/pngn";
print "Content-Length: ".length($png)."nn";
print $png;
}

检查返回给$ext的值。

根据您的代码:

my $file = "image_file.jpg";
my ($ext) = $file =~ /(.[^.]+)$/;
say $ext; 

这将打印".jpg",这就是为什么没有条件的代码工作,因为条件将失败,因为它是"jpg"而不是".jpg">

最新更新