Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Created July 7, 2012 07:16
Show Gist options
  • Select an option

  • Save ScottPhillips/3065208 to your computer and use it in GitHub Desktop.

Select an option

Save ScottPhillips/3065208 to your computer and use it in GitHub Desktop.

Revisions

  1. ScottPhillips revised this gist Jul 10, 2012. 1 changed file with 7 additions and 9 deletions.
    16 changes: 7 additions & 9 deletions remote-image-cache.php
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,22 @@
    <?php
    function cache_image($image_url){
    //replace with your cache directory
    $image_path = path/to/cache/dir/;
    $image_path = 'path/to/cache/dir/';
    //get the name of the file
    $exploded_image_url = explode(“/”,$image_url);
    $exploded_image_url = explode("/",$image_url);
    $image_filename = end($exploded_image_url);
    $exploded_image_filename = explode(“.”,$image_filename);
    $exploded_image_filename = explode(".",$image_filename);
    $extension = end($exploded_image_filename);
    //make sure its an image
    if($extension==“gif”||$extension==“jpg”||$extension==“png”){
    //make sure its an image
    if($extension == "gif" || $extension == "jpg" || $extension == "jpeg" || $extension == "png") {
    //get the remote image
    $image_to_fetch = file_get_contents($image_url);
    //save it
    $local_image_file = fopen($image_path.$image_filename, ‘w+’);
    $local_image_file = fopen($image_path.$image_filename, 'w+');
    chmod($image_path.$image_filename,0755);
    fwrite($local_image_file, $image_to_fetch);
    fclose($local_image_file);
    }
    }
    //usage
    //cache_image(“http://www.flickr.com/someimage.jpg”);

    ?>
    //cache_image(“http://www.flickr.com/someimage.jpg”);
  2. ScottPhillips created this gist Jul 7, 2012.
    24 changes: 24 additions & 0 deletions remote-image-cache.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <?php
    function cache_image($image_url){
    //replace with your cache directory
    $image_path = ‘path/to/cache/dir/’;
    //get the name of the file
    $exploded_image_url = explode(“/”,$image_url);
    $image_filename = end($exploded_image_url);
    $exploded_image_filename = explode(“.”,$image_filename);
    $extension = end($exploded_image_filename);
    //make sure its an image
    if($extension==“gif”||$extension==“jpg”||$extension==“png”){
    //get the remote image
    $image_to_fetch = file_get_contents($image_url);
    //save it
    $local_image_file = fopen($image_path.$image_filename, ‘w+’);
    chmod($image_path.$image_filename,0755);
    fwrite($local_image_file, $image_to_fetch);
    fclose($local_image_file);
    }
    }
    //usage
    //cache_image(“http://www.flickr.com/someimage.jpg”);

    ?>