如何在PHP中捕获imagick致命错误 [英] How do I catch an imagick fatal error in PHP
问题描述
我正在使用imagick为pdf文档创建缩略图.我在这条线上收到致命错误.
i am using imagick to create thumbnails for pdf documents. i get a fatal error on this line..
$imagick->readImage($file .'[0]');
我尝试用try catch进行换行,但据我所知这是行不通的,因为这是一个致命错误,不是一个例外.我将如何优雅地捕获此错误?
I tried wrapping in a try catch but as i learned that doesn't work because it's a fatal error, not an exception. How would I gracefully catch this error?
与解决imagick问题相比,我更关心使用PHP检测错误,因为用户pdf文件可能会出现许多错误.谢谢!
I am more concerned about using PHP to detect the error than solving the imagick problem, since any number of errors might come up with user pdf files. thanks!
推荐答案
不幸的是,不可能在php中捕获致命错误.这是给定的.您仍然可以做几件事:
Unfortunately it's not possible to catch fatal errors in php. This is given. There are still couple of things you can do:
-
使用
register_shutdown_function()
注册的功能仍将执行.您可以将错误处理放入该函数中,如果readImage()
成功,则注册空函数.
Function registered using
register_shutdown_function()
is still executed. You can put your error hadling into such function, and if thereadImage()
succeeds, register empty function.
您可以将缩略图生成放入php命令行脚本并使用exec('php thngenerate.php ' . escapeshellarg($file .'[0]'), $out, $return_var);
执行.如果是$return_var != 0
,则出现错误.
You can put thumbnail generation into php command line script and execute it using exec('php thngenerate.php ' . escapeshellarg($file .'[0]'), $out, $return_var);
. If $return_var != 0
, there was an error.
类似于#2,但是使用http调用脚本,这次您注意内部服务器错误.
Similar to #2, but script is called using http, this time you watch for internal server error.
这篇关于如何在PHP中捕获imagick致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!