3

My site is running in LAMP, my image CDN is in nginx.

I want to do is: Check if a requested image has a copy in CDN server, if yes then loan the copy in cdn server, otherwise, load the local copy for user.

Is there a programmatically way to check whether the remote CDN image is exist?

(perhaps determine the header? as I notice that if request image isn't exist, it returns 404)

enter image description here

gilzero
  • 1,832
  • 4
  • 19
  • 26
  • possible duplicate of [Check whether image exists on remote URL](http://stackoverflow.com/questions/1363925/check-whether-image-exists-on-remote-url) – Gordon Jan 19 '12 at 12:47
  • What do you mean by load.. do you want to display the image using ? – Ninja Jan 19 '12 at 12:56

5 Answers5

3

I use this method to ping distant files:

  /**
   * Use HTTP GET to ping an url
   *
   * /!\ Warning, the return value is always true, you must use === to test the response type too.
   * 
   * @param string $url
   * @return boolean true or the error message
   */
  public static function pingDistantFile($url)
  {
    $options = array(
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_URL => $url,
      CURLOPT_FAILONERROR => true, // HTTP code > 400 will throw curl error
    );

    $ch = curl_init();
    curl_setopt_array($ch, $options);
    $return = curl_exec($ch);

    if ($return === false)
    {
      return curl_error($ch);
    }
    else
    {
      return true;
    }
  }

You can also use the HEAD method but maybe your CDN as disabled it.

Damien
  • 5,872
  • 2
  • 29
  • 35
2

So long as the copy is public, you could just check for a 404 with cURL. See this question detailing how to do it.

Community
  • 1
  • 1
Zoe Edwards
  • 12,999
  • 3
  • 24
  • 43
2

You can use file_get_contents for this:

 $content = file_get_contents("path_to_your_remote_img_file");
 if ($content === FALSE)
 {
     /*Load local copy*/
 }
 else
 {
     /*Load $content*/
 }

Oh and one more thing- if you only want to display the image with an img tag, you can simply do this- using img tags onerror attribute- if the image does not exist on the server, the onerror attribute will display the local file:

<img src="path_to_your_remote_img_file" onerror='this.src="path_to_your_local_img_file"'>

You can read a similar question on this here: detect broken image using php

Community
  • 1
  • 1
Ninja
  • 5,082
  • 6
  • 37
  • 59
  • Yes, but it depends on what he wants to do with the image. If he just wants to display it, I have updated my answer with a way to do it using simple html, without having to do any header or file exists checks – Ninja Jan 19 '12 at 13:33
1

Another easier way – without cURL:

$headers = get_headers('http://example.com/image.jpg', 1);
if($headers[0] == 'HTTP/1.1 200 OK')
{
  //image exist
}
else
{
  //some kind of error
}
kuboslav
  • 1,430
  • 5
  • 16
  • 31
-1
<?php
if (is_array(getimagesize("http://www.imagelocation.com/image.png"))){
   // Image ok
} else {
   // Image not ok
}
?>
GrooveNow
  • 44
  • 3
  • This would download the whole image to the webserver from the CDN, unnecessarily adding load to both servers. – WesleyE Feb 14 '13 at 16:51