简体   繁体   English

如何在PHP中捕获imagick致命错误

[英]How do I catch an imagick fatal error in PHP

i am using imagick to create thumbnails for pdf documents. 我正在使用imagick为pdf文档创建缩略图。 i get a fatal error on this line.. 我在这条线上收到致命错误。

$imagick->readImage($file .'[0]');

I tried wrapping in a try catch but as i learned that doesn't work because it's a fatal error, not an exception. 我尝试用try catch进行换行,但是据我所知,这是无效的,因为这是一个致命错误,不是一个例外。 How would I gracefully catch this error? 我将如何优雅地捕获此错误?

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. 与解决imagick问题相比,我更关心使用PHP检测错误,因为用户pdf文件可能会出现许多错误。 thanks! 谢谢!

Unfortunately it's not possible to catch fatal errors in php. 不幸的是,不可能在php中捕获致命错误。 This is given. 这是给定的。 There are still couple of things you can do: 您仍然可以做几件事:

  1. Function registered using register_shutdown_function() is still executed. 使用register_shutdown_function()功能仍然会执行。 You can put your error hadling into such function, and if the readImage() succeeds, register empty function. 您可以将您的错误标记放入该函数中,如果readImage()成功,则注册空函数。

  2. You can put thumbnail generation into php command line script and execute it using exec('php thngenerate.php ' . escapeshellarg($file .'[0]'), $out, $return_var); 您可以将缩略图生成放入php命令行脚本中,并使用exec('php thngenerate.php ' . escapeshellarg($file .'[0]'), $out, $return_var); . If $return_var != 0 , there was an error. 如果$return_var != 0 ,则出现错误。

  3. Similar to #2, but script is called using http, this time you watch for internal server error. 与#2相似,但是使用http调用脚本,这次您注意内部服务器错误。

It should show up in the server error logs which can give you insight into why it fails and hopefully prevent it from happening in the first place. 它应该显示在服务器错误日志中,该日志可以让您深入了解失败的原因,并希望可以从一开始就阻止它发生。 Other than that, it depends on what you are trying to do. 除此之外,这取决于您要执行的操作。 I use mine for resizing images and creating thumbnails so my checks are related to that; 我使用我的图像来调整图像大小并创建缩略图,因此我的检查与此相关。 does imagick show the correct dimensions and is the thumbnail present and have a file size greater than 0. imagick会显示正确的尺寸吗,并且存在缩略图并且文件大小大于0。

You can set your custom error handler with set_error_handler : 您可以使用set_error_handler设置自定义错误处理程序:

function exceptionErrorHandler($errno, $errstr, $errfile, $errline ) {    
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

set_error_handler("exceptionErrorHandler");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM