How to determine when an image link returns 404, BUT it is still a valid image

Youtube returns 404 for non-existing thumbnails, but also returns valid image data (broken video thumbnail), so checking it with Image does not work, onerror is not called:

var img = new Image(); img.onload = function() { alert("found")}; img.onerror = function() { alert("not found") }; img.src = "http://img.youtube.com/vi/aaaa/1.jpg"; 

At startup, it says "found." Is there a way to detect 404 if actual image data can be downloaded?

It’s also good if it can be found that the link returns a standard thumbnail image of a “broken video” image.

+6
source share
1 answer

As a rough but working workaround in this case, I would suggest checking the image size at boot time. Usually the thumbnails are larger, so if the returned image is 120x90px, you can consider it a 404 image.

The only way is to download it through a PHP script that can check HTTP headers before passing the image.

+2
source

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


All Articles