0

Suppose there exists a simple website that hosts user images on multiple image sharing websites to ensure that one server going down will not damage the integrity of the user's page.

Is it possible using php (perhaps using Thread, curl, or proc_open?) to determine which image hosting sites are up and to display the image using the host which produces the fastest image delivery?

bvpx
  • 1,227
  • 2
  • 16
  • 33

2 Answers2

0

It will be of limited use to know, which image hoster is reachable from your server, if you want the client to directly request the image from the hoster.

This implies you have two reliable choices:

  • Load the image to your server (via cURL or http protocol wrapers), then deliver it to the client from there
  • Use a client-sided script to load the image into the client.

I recommend you look into the second option, delivering a list of possible image URLs via PHP, then using JS on the client to try-load until success (or list exhausted). A combination of random value and propability threshold could additionally make your client script "phone home" for failed loads, so you can reorder your URL list server-side.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • I understand that using server-side checks for image integrity might not be a great idea because the client may have different results, however I feel that having images loaded in sequence using Javascript could cause undesired effects. Namely, if the first-choice host is suddenly down, how long does javascript take to determine that image won't load and move on to the next one? Is it possible the user experience could suffer from this? I think the best solution is one that uses php to check for available images then uses javascript to attempt to load each one in sequence. – bvpx Jul 24 '13 at 17:04
  • Those two choices @Eugene presents define purpose: Is your website providing image services as a main activity? Then you you should manage which sites to use server-side. Do you own all image sites? Then you should be using load-balancing routers, not client-side code for better performance. What is your website's goal? – Papasmile Jul 24 '13 at 20:37
  • The website would be a community where users create an image micro-blog (sort of like tumblr). The images are uploaded to external free image sites using APIs (imgur, flickr, yfrog, etc). What does "load-balancing routers" mean? – bvpx Jul 24 '13 at 21:06
  • The user experience will benefit from client loading, as the window between the server checking the image (hoster still up) and the client loading it (hoster now down) closes. Phoning home from the script allows you, to give the next client another primary source. – Eugen Rieck Jul 24 '13 at 21:16
0

I use php's fsockopen() function to read the first line (the header) and check for error status like 404 or 302 to determine if the file exists. It's really quick and efficient. There is great documentation at php.net on how to use fsockopen. $path contains includes the filename of the image.

$fp = @fsockopen($domain, 80, $errno, $errstr, 1);
if ($fp) {
    $out = "GET $path HTTP/1.1\r\n";
    $out .= "Host: $domain\r\n";
    $out .= "Connection: Close\r\n\r\n";
    ...
}
The Debi
  • 157
  • 6
  • Thanks, this seems like exactly what I want. I'm just wondering about the difference between this approach and the JavaScript solution proposed here (http://stackoverflow.com/a/1630859/2576956). Javascript is client-side, so the user will have to deal with attempting to load each of these images, I assume. Does the `fsockopen` command incur any additional costs on my server that would better be left for the client to deal with? – bvpx Jul 24 '13 at 16:54
  • The javascript is actually loading the image data so that it is visible on the webpage. The fsockopen example I gave above is to simply check to see if the image exists at a website. And a method to compare which website, that offers the same image, is faster. No, fsockopen does not add any significant additional costs, with the exception of factors that you cannot control such as network latency or if the remote website is accessible. I remember that my testing last year showed that it only added only a few milliseconds to my script. – The Debi Aug 08 '13 at 08:00