You are here

function tweet_feed_process_twitter_image in Tweet Feed 7.3

Same name and namespace in other branches
  1. 7.2 tweet_feed.module \tweet_feed_process_twitter_image()

Process Images from URL

Allows the passage of a URL and a saves the resulting image in that URL to a file that can be attached to our node. These are mostly used in user profiles and avatars associated with user tweets.

Parameters

string url: The twitte.com url of the image being retrieved

string type: The node type (feed item or user profile item)

int tid: The tweet id associated with this image

Return value

object file The file object for the retrieved image or NULL if unable to retrieve

1 call to tweet_feed_process_twitter_image()
tweet_feed_save_tweet in ./tweet_feed.module
Save The Tweet (and profile)

File

./tweet_feed.module, line 1243

Code

function tweet_feed_process_twitter_image($url, $type, $tid = NULL) {

  // If there is no URL, then there is no image and we must skip.
  if (!isset($url)) {
    return NULL;
  }

  // Exit if the user doesn't need images.
  if (variable_get('tweet_feed_fetch_images', 1) == 0) {
    return NULL;
  }

  // If the folder for this type of file does not exist, then create it.
  if (!file_exists('public://' . $type)) {
    drupal_mkdir('public://' . $type);
  }

  // Get the contents of the file for processing. I hate this (@)
  $contents = @file_get_contents($url);

  // If there are no contents, then go back.
  if (empty($contents)) {
    return NULL;
  }

  // Check the path and create the directory if it does not exist.
  tweet_feed_check_path('public://' . $type);

  // Save the contents of the file to the file system and create the filename and uri
  $file = file_save_data($contents, 'public://' . $type . '/' . md5($url) . '.jpg', FILE_EXISTS_REPLACE);

  // Sanity check to make sure the file saved
  if ($file === FALSE) {
    watchdog('tweet_feed', 'The :type for :tid could not be properly saved.', array(
      ':type' => $type,
      ':tid' => $tid,
    ), WATCHDOG_ERROR, NULL);
    return NULL;
  }

  // Update our file object and re-save the file object to the database to make sure we
  // have the right information
  $file->uid = 1;
  $file->status = 1;
  file_save($file);

  // Record the file's usgae so it is not deleted as a temporary file
  file_usage_add($file, 'tweet_feed', 'file', $file->fid);

  // Return the file object so we can save it to our node
  return $file;
}