From 4fe4a221895f416c0e714a1dec48b73b74c8e46b Mon Sep 17 00:00:00 2001 From: Jason Schuller Date: Fri, 9 Aug 2013 08:15:28 -0700 Subject: [PATCH] Twitter Profile Image Cache --- .gitignore | 3 +++ cache/index.php | 5 +++++ dropplets/functions.php | 28 ++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 cache/index.php diff --git a/.gitignore b/.gitignore index ae9393d9..03c7e95b 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ templates/typographic/ templates/cards/ plugins/switcher.php + +cache/*.png +cache/*.jpeg diff --git a/cache/index.php b/cache/index.php new file mode 100644 index 00000000..2a09b0d2 --- /dev/null +++ b/cache/index.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/dropplets/functions.php b/dropplets/functions.php index 67010a34..15f91fa7 100644 --- a/dropplets/functions.php +++ b/dropplets/functions.php @@ -296,13 +296,37 @@ function count_premium_templates($type = 'all') { define('IS_HOME', $is_home); /*-----------------------------------------------------------------------------------*/ -/* Get Twitter Profile Image (This Needs to be Cached) +/* Get Profile Image (This Needs to be Cached) /*-----------------------------------------------------------------------------------*/ function get_twitter_profile_img($username, $size = '') { $api_call = 'https://twitter.com/users/'.$username.'.json'; $results = json_decode(file_get_contents($api_call)); - return str_replace('_normal', $size, $results->profile_image_url); + $image_url = str_replace('_normal', $size, $results->profile_image_url); + + // Replace with your cache directory. + $image_path = './cache/'; + + // 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); + } + + return $image_path.$image_filename; } /*-----------------------------------------------------------------------------------*/