Incorrect image and PHP error handling

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

imagepng($image);

Sometimes for some reason the image may not be displayed, as a rule, the error is not with the image, but with the actual php functions that are not executed successfully. However, this leads to the return of a blank image that does not help me.

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

For example, a successfully displayed image without 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 script, , PHP , ...

$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  
** 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);

, , , ! , .

+3
1

, PHP, " ". set_error_handler() , .

, , - , , ( , ).

+2

Source: https://habr.com/ru/post/1739762/


All Articles