0

我正在尝试使用Imagine批量制作超过 90k + 相对较小的移动图像的 250x250 缩略图。问题是,当我运行一个循环时,

foreach ($images as $c) {
  $imagine = new Imagine();
  $image = $imagine->open($c);
  $image->resize(new Box(250, 250))->save($outFolder);
}

有时,图像已损坏并且open()方法失败,抛出异常:

Unable to open image vendor/imagine/imagine/lib/Imagine/Gd/Imagine.php Line: 96

并完全打破循环。有没有办法检查是否open失败?就像是:

foreach ($images as $c) {
  $imagine = new Imagine();
  $image = $imagine->open($c);
  if ($image) {
     $image->resize(new Box(250, 250))->save($outFolder);
  } else {
     echo 'corrupted: <br />';
  }
}

希望有人可以提供帮助。或者如果不可能,你能推荐一个我可以批量调整大小的 PHP 图像库吗?

谢谢

4

1 回答 1

1

要处理异常,只需使用try-catch.

从图书馆文档

ImagineInterface::open() 方法可能会抛出以下异常之一:

想象\异常\无效参数异常

想象\异常\运行时异常

试试这样:

$imagine = new Imagine(); // Probably no need to instantiate it in every loop
foreach ($images as $c) {
    try {
        $image = $imagine->open($c);
    } catch (\Exception $e) {
        echo 'corrupted: <br />';
        continue;
    }
    $image->resize(new Box(250, 250))->save($outFolder);
}
于 2016-10-08T20:08:51.580 回答