想象一下如果 open() 方法抛出异常忽略然后继续下一个循环
Imagine if open() method threw exception ignore then proceed to the next loop
我正在尝试使用 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 图像库,我可以实用地批量调整大小吗?
谢谢
处理异常只需使用try-catch
。
来自图书馆documentation
The ImagineInterface::open() method may throw one of the following exceptions:
Imagine\Exception\InvalidArgumentException
Imagine\Exception\RuntimeException
试试这样:
$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);
}
我正在尝试使用 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 图像库,我可以实用地批量调整大小吗?
谢谢
处理异常只需使用try-catch
。
来自图书馆documentation
The ImagineInterface::open() method may throw one of the following exceptions:
Imagine\Exception\InvalidArgumentException
Imagine\Exception\RuntimeException
试试这样:
$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);
}