我正在尝试使用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图像库,我可以按批处理实际调整大小吗?
谢谢
答案 0 :(得分:1)
要处理异常,只需使用try-catch
。
来自图书馆documentation
ImagineInterface :: open()方法可能会抛出以下异常之一:
<强>想象\异常\ InvalidArgumentException 强>
<强>想象\异常\ 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);
}