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

Not recognizing png files for image processing

我正在尝试将 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/jpeg\n";
  print "Content-Length: ".length($jpg)."\n\n";
  print $jpg;
} else {
  my $png=$img->png;
  print "Content-Type: image/png\n";
  print "Content-Length: ".length($png)."\n\n";
  print $png;
}

检查返回给 $ext 的值。

根据您的代码:

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

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