Skip to content

Instantly share code, notes, and snippets.

@kressly
Forked from ScottPhillips/remote-image-cache.php
Created October 29, 2020 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kressly/03ebe94371b698cc58a4526ca642e0d1 to your computer and use it in GitHub Desktop.
Save kressly/03ebe94371b698cc58a4526ca642e0d1 to your computer and use it in GitHub Desktop.
Cache remote image using PHP
<?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 == "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+');
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”);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment