PHP无效图像和错误处理 [英] PHP invalid image's and error handling

查看:125
本文介绍了PHP无效图像和错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP的Image和GD函数,您可以使用以下方法最终输出php图像

Using PHP's Image and GD functions you can use the following method to finally output the php image

imagepng($image);

有时,无论出于何种原因,图像可能无法显示,通常错误不是图像,而是实际的php函数没有成功执行。然而,这会导致返回空白图像,这对我没有帮助。

Sometimes, for whatever reason the image may not be displayed typically the error is not with the image but with the actual php functions not executing successfully. However this causes a blank image to be returned which doesn't help me.

我想知道的是,有没有办法检测空白或无效的图像并创建一个新图像,使用imagestring()将错误写入新图像,然后显示这个新的(调试)图像。

What I want to know is, is there a way to detect a blank or an invalid image and create a new image, write the errors to the new image using imagestring() and then display this new (debug) image instead.

例如,成功显示的图像没有错误:

for example, a successfully displayed image with no errors:

$image  = imagecreate(256, 256); //create image
imagecolortransparent($image, $BLUE); //set transparent
imagefilledrectangle($image, 0, 0, 256, 256, $BLUE); //fill with 'transparent colour'

//Draw a border round the image
imageline($image, 0, 0, 0, 255, $Black);
imageline($image, 0, 0, 255, 0, $Black);
imageline($image, 255, 0, 255, 255, $Black);
imageline($image, 0, 255, 255, 255, $Black);

imagestring($image, 1, 10, 10, "I am an image!", $Black);

imagepng($image);
imagedestroy($image);

但如果我在PHP脚本中引入一些错误,可能与也可能没有实际的图像创建然后PHP脚本失败,图像将不可见...

but if I then introduce some errors in the php script that may or may not be to do with the actual image creation then the php script fails and the image will not be visible...

$image  = imagecreate(256, 256); //create image
imagecolortransparent($image, $BLUE); //set transparent
imagefilledrectangle($image, 0, 0, 256, 256, $BLUE); //fill with 'transparent colour'

//Draw a border round the image
imageline($image, 0, 0, 0, 255, $Black);
imageline($image, 0, 0, 255, 0, $Black);
imageline($image, 255, 0, 255, 255, $Black);
imageline($image, 0, 255, 255, 255, $Black);

imagestring($image, 1, 10, 10, "I am an image!", $Black);

/* I am here to cause problems with the php script 
** and cause the execution to fail, I am a function 
** that does't exist...
**
** and I am missing a semi colon! ;)*/
non_existant_function() 

imagepng($image);
imagedestroy($image);

此时我想创建一个像上面这样的新图像但是要取代我是一个图像!文本我会把发生的实际错误。

At this point I want to create a new image like above but in replacement of the I am an image! text I would put the actual error that has occured.

推荐答案

你要做的是捕捉PHP错误,而不是检测空白图片。您可以使用 set_error_handler()来定义自定义发生错误时调用的回调。

What you want to do is catch PHP errors, not detect a "blank image". You can use set_error_handler() to define a custom callback that's called when an error occurs.

解析错误之类的东西应该在发布代码之前进行调试,但这应该可以帮助您检测随机错误(数据库连接死亡) ,诸如此类。)

Things such as parse errors are something you should debug before publishing your code, but this should help you detect random errors (database connections dying, whatnot).

这篇关于PHP无效图像和错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆