Search code examples
phpazure-blob-storagefpdf

How to detect an invalid XML image blob with PHP?


I am using FPDF to generate a dynamic PDF document with PHP. In this PDF document, a number of images is included, which are extracted through an API from a CRM system.

These images are blobs stored in Microsoft Azure. Now the problem is that I have noticed that sometimes these image files return an invalid XML instead of the image, which throws a fatal PHP error when generating the PDF.

Example of such an invalid image URL: https://whisestorageprod.blob.core.windows.net/public/bouwen/Pictures/5226673/200/15f953d227594f2d95628b2975a1b7a9.jpg

I have already implemented some code to check if the image has valid dimensions, but still it seems to generate the error before it gets to this code. How can I check in PHP if the URL is a valid image or if it returns an XML like the URL above?


Solution

  • You can use getimagesize() function in PHP to check if the URL is a valid image. This function returns an array with the image information, or false if the file is not an image.

    You can use @ to suppress any warnings/errors generated by the function. Please refer to the code below:

    function is_valid_image($url) {
        $info = @getimagesize($url);
        return $info !== false;
    }