I have a web application build on PHP that (does some processing and) displays images hosted on external servers. Some of these images (i.e. from Flickr) when they are deleted, leave a redirect behind that points to a "Missing Image" gif or something equivalent.
By using the get_headers()
function in php on each image, I was able to check server-side if an image was deleted or not (for example, by checking if the Content-Type
was image/gif
instead of image/jpg
, on Flickr images).
The problem is that each page returns 20-100 images on average (sometimes even more), and get_headers()
requires on average 0.3-0.5s for each image, thus making the wait time for the user too long.
I was wondering if this check could be done client-side, using javascript. The user will be waiting for the images to load (as with any page), I will be checking the images if they are valid, and set display property to none on the missing ones.
You can use curl_multi_exec()
to check multiple URLs at once. If you use the following cURL options:
CURLOPT_FOLLOWLOCATION = FALSE
CURLOPT_NOBODY = TRUE
CURLOPT_HEADER = TRUE
...this will prevent the following of redirects. You can then use this information (whether the response is a redirect or not, see CURLINFO_HTTP_CODE
in curl_get_info()
) to determine whether the file exists. By using curl_multi_exec()
you will perform multiple checks at once, which should drastically reduce the page load time.
You can use this method to determine the image type but it may cause some extra traffic to your servers.
var request;
request = $.ajax({
type: "HEAD",
url: 'your image url',
success: function () {
alert("Size is " + request.getResponseHeader("Content-Length"));
alert("Type is " + request.getResponseHeader("Content-Type"));
}
});
Found here: Can I get an image's file size and mime-type via JavaScript (FireFox/Gecko/Mozilla only)
Yes you can use javascript, for example Ajax to make your requests to a PHP program.
However, instead of using get_headers (), you might come better to use this feature: http://es.php.net/manual/es/function.file-exists.php#85246
Regards!