PHP Detecting if source image url link leads to a “broken” image?

前端 未结 8 1587
迷失自我
迷失自我 2020-12-30 03:56

Suppose you have a thumbnail generator script that accepts source images in the form of a URL. Is there a way to detect if the source URL is \"broken\" - whether nonexistent

8条回答
  •  -上瘾入骨i
    2020-12-30 04:35

    The only really reliable way is to request the image using file_get_contents(), and finding out its image type using getimagesize().

    Only if getimagesize() returns a valid file type, can you rely that it is in fact a valid image.

    This is quite resource heavy, though.

    You could consider not doing any server-side checks at all, and adding an onerror JavaScript event to the finished image resource:

    
    

提交回复
热议问题